Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Private role attributes are now visible in class
Thanks to extensive support by jnthn++.  Unfortunately, getting this to work
for private *native* role attributes, proved to be needing to go deeper into
the rabbit hole.  Which will be for another day.
  • Loading branch information
lizmat committed Dec 7, 2013
1 parent de5c8f8 commit 56ee1a5
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 11 deletions.
43 changes: 33 additions & 10 deletions src/Perl6/Actions.nqp
Expand Up @@ -1667,9 +1667,9 @@ class Perl6::Actions is HLL::Actions does STDActions {
unless $*HAS_SELF {
$*W.throw($/, ['X', 'Syntax', 'NoSelf'], variable => $past.name());
}
my $attr := get_attribute_meta_object($/, $past.name());
my $attr := get_attribute_meta_object($/, $past.name(), $past);
$past.returns($attr.type) if $attr;
$past.scope('attribute');
$past.returns($attr.type);
$past.unshift(instantiated_type(['$?CLASS'], $/));
$past.unshift(QAST::Var.new( :name('self'), :scope('lexical') ));
}
Expand Down Expand Up @@ -1759,7 +1759,7 @@ class Perl6::Actions is HLL::Actions does STDActions {
$past
}

sub get_attribute_meta_object($/, $name) {
sub get_attribute_meta_object($/, $name, $later?) {
unless nqp::can($*PACKAGE.HOW, 'get_attribute_for_usage') {
$/.CURSOR.panic("Cannot understand $name in this context");
}
Expand All @@ -1770,12 +1770,24 @@ class Perl6::Actions is HLL::Actions does STDActions {
$found := 1;
}
unless $found {
$*W.throw($/, ['X', 'Attribute', 'Undeclared'],
symbol => $name,
package-kind => $*PKGDECL,
package-name => $*PACKAGE.HOW.name($*PACKAGE),
what => 'attribute',
);

# need to check later
if $later {
my $seen := %*ATTR_USAGES{$name};
%*ATTR_USAGES{$name} := $seen := nqp::list() unless $seen;
$later.node($/);
$seen.push($later);
}

# now is later
else {
$*W.throw($/, ['X', 'Attribute', 'Undeclared'],
symbol => $name,
package-kind => $*PKGDECL,
package-name => $*PACKAGE.HOW.name($*PACKAGE),
what => 'attribute',
);
}
}
$attr
}
Expand Down Expand Up @@ -1902,6 +1914,17 @@ class Perl6::Actions is HLL::Actions does STDActions {
$*W.create_signature(nqp::hash('parameters', [])));
}

# check up any private attribute usage
for %*ATTR_USAGES {
my $name := $_.key;
my @usages := $_.value;
for @usages {
my $past := $_;
my $attr := get_attribute_meta_object($past.node, $name);
$past.returns($attr.type);
}
}

# Document
Perl6::Pod::document($/, $*PACKAGE, $*DOC);

Expand Down Expand Up @@ -3686,7 +3709,7 @@ class Perl6::Actions is HLL::Actions does STDActions {
# If it's !-twigil'd, ensure the attribute it mentions exists unless
# we're in a context where we should not do that.
if $_<bind_attr> && !$no_attr_check {
get_attribute_meta_object($/, $_<variable_name>);
get_attribute_meta_object($/, $_<variable_name>, QAST::Var.new);
}

# If we have a sub-signature, create that.
Expand Down
3 changes: 2 additions & 1 deletion src/Perl6/Grammar.nqp
Expand Up @@ -1891,9 +1891,10 @@ grammar Perl6::Grammar is HLL::Grammar does STD {
:my $*DOCEE;
<.attach_docs>

# Meta-object will live in here; also set default REPR (a trait
# Type-object will live in here; also set default REPR (a trait
# may override this, e.g. is repr('...')).
:my $*PACKAGE;
:my %*ATTR_USAGES;
:my $*REPR;

# Default to our scoped.
Expand Down

0 comments on commit 56ee1a5

Please sign in to comment.