Skip to content

Commit

Permalink
Support shapes for HAS scoped attributes
Browse files Browse the repository at this point in the history
The shape declaration on an attributes was parsed correctly since a while,
but now this information is passed to the repr composition. The following
is now supported fo CStructs and CPPStructs:

use NativeCall;
class Foo is repr<CStruct> {
  HAS sometype @.foo[32] is CArray;
}

The memory for the class Foo will be allocate to hold these 32 elements of
type sometype.
  • Loading branch information
FROGGS committed Apr 7, 2018
1 parent edbbc44 commit 36c92d5
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/Perl6/Actions.nqp
Original file line number Diff line number Diff line change
Expand Up @@ -3196,6 +3196,19 @@ class Perl6::Actions is HLL::Actions does STDActions {
if nqp::objprimspec($attr.type) != 0 {
$/.worry('Useless use of HAS scope on ' ~ $attr.type.HOW.name($attr.type) ~ ' typed attribute.');
}

if $attr.type.REPR eq 'CArray' {
if $<scoped><DECL><declarator><variable_declarator><semilist> -> $semilist {
my @dimensions := nqp::list_i();
for $semilist -> $dimension {
$dimension.ast.nosink(1);
my $elems := nqp::unbox_i($*W.compile_time_evaluate($/, $dimension.ast));
nqp::push_i(@dimensions, $elems);
}
nqp::bindattr($attr, $attr.WHAT, '$!dimensions', @dimensions);
}
}

# Mark $attr as inlined, that's why we do all this.
nqp::bindattr_i($attr, $attr.WHAT, '$!inlined', 1);
make $scoped;
Expand Down
5 changes: 5 additions & 0 deletions src/Perl6/Metamodel/BOOTSTRAP.nqp
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@ my class BOOTSTRAPATTR {
has $!box_target;
has $!package;
has $!inlined;
has $!dimensions;
method name() { $!name }
method type() { $!type }
method box_target() { $!box_target }
method package() { $!package }
method inlined() { $!inlined }
method dimensions() { $!dimensions }
method has_accessor() { 0 }
method positional_delegate() { 0 }
method associative_delegate() { 0 }
Expand Down Expand Up @@ -1137,6 +1139,8 @@ BEGIN {
# has Mu $!auto_viv_container;
# has Mu $!build_closure;
# has Mu $!package;
# has int $!inlined;
# has Mu $!dimensions;
# has int $!positional_delegate;
# has int $!associative_delegate;
# has Mu $!why;
Expand All @@ -1153,6 +1157,7 @@ BEGIN {
Attribute.HOW.add_attribute(Attribute, BOOTSTRAPATTR.new(:name<$!build_closure>, :type(Mu), :package(Attribute)));
Attribute.HOW.add_attribute(Attribute, BOOTSTRAPATTR.new(:name<$!package>, :type(Mu), :package(Attribute)));
Attribute.HOW.add_attribute(Attribute, BOOTSTRAPATTR.new(:name<$!inlined>, :type(int), :package(Attribute)));
Attribute.HOW.add_attribute(Attribute, BOOTSTRAPATTR.new(:name<$!dimensions>, :type(Mu), :package(Attribute)));
Attribute.HOW.add_attribute(Attribute, BOOTSTRAPATTR.new(:name<$!box_target>, :type(int), :package(Attribute)));
Attribute.HOW.add_attribute(Attribute, BOOTSTRAPATTR.new(:name<$!positional_delegate>, :type(int), :package(Attribute)));
Attribute.HOW.add_attribute(Attribute, BOOTSTRAPATTR.new(:name<$!associative_delegate>, :type(int), :package(Attribute)));
Expand Down
3 changes: 3 additions & 0 deletions src/Perl6/Metamodel/REPRComposeProtocol.nqp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ role Perl6::Metamodel::REPRComposeProtocol {
if nqp::can($attr, 'inlined') {
%attr_info<inlined> := $attr.inlined;
}
if nqp::can($attr, 'dimensions') {
%attr_info<dimensions> := $attr.dimensions;
}
nqp::push(@attrs, %attr_info);
}

Expand Down
2 changes: 2 additions & 0 deletions src/core/Attribute.pm6
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ my class Attribute { # declared in BOOTSTRAP
# has Mu $!build_closure;
# has Mu $!package;
# has int $!inlined;
# has Mu $!dimensions;
# has int $!positional_delegate;
# has int $!associative_delegate;
# has Mu $!why;
Expand Down Expand Up @@ -145,6 +146,7 @@ my class Attribute { # declared in BOOTSTRAP
method readonly() { !self.rw }
method package() { $!package }
method inlined() { $!inlined }
method dimensions() { $!dimensions } # turn list_i into List
multi method Str(Attribute:D:) { self.name }
multi method gist(Attribute:D:) { self.type.^name ~ " " ~ self.name }

Expand Down

0 comments on commit 36c92d5

Please sign in to comment.