Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
replace 'is inlined' trait by HAS scope declarator
Attributes in CStructs and CUnions can specify whether they are 'embedded'
into the surrouning structure or referenced. To say the former one will now
be able to use the 'HAS' keyword, to do the latter one uses 'has' as usual.
  • Loading branch information
FROGGS committed May 21, 2015
1 parent b865609 commit c006cd1
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 9 deletions.
37 changes: 32 additions & 5 deletions lib/NativeCall.pm
@@ -1,6 +1,6 @@
use nqp;

unit module NativeCall;
module NativeCall {

# Throwaway type just to get us some way to get at the NativeCall
# representation.
Expand Down Expand Up @@ -414,10 +414,6 @@ multi trait_mod:<is>(Routine $p, :$encoded!) is export(:DEFAULT, :traits) {
$p does NativeCallEncoded[$encoded];
}

multi trait_mod:<is>(Attribute $a, :$inlined!) is export(:DEFAULT, :traits) {
nqp::bindattr_i(nqp::decont($a), $a.WHAT, '$!inlined', 1);
}

role ExplicitlyManagedString {
has $.cstr is rw;
}
Expand Down Expand Up @@ -456,4 +452,35 @@ sub cglobal($libname, $symbol, $target-type) is export is rw {
)
}

}

sub EXPORT(|) {
use NQPHLL:from<NQP>;
my role HAS-decl-grammar {
# This is a direct copy of scope_declarator:sym<has>, besides the uppercase spelling.
token scope_declarator:sym<HAS> {
:my $*LINE_NO := HLL::Compiler.lineof(self.orig(), self.from(), :cache(1));
<sym>
:my $*HAS_SELF := 'partial';
:my $*ATTR_INIT_BLOCK;
<scoped('has')>
}
}
my role HAS-decl-actions {
method scope_declarator:sym<HAS>(Mu $/) {
# my $scoped := $<scoped>.ast;
my Mu $scoped := nqp::atkey(nqp::findmethod($/, 'hash')($/), 'scoped').ast;
my Mu $attr := $scoped.ann('metaattr');
# Mark $attr as inlined, that's why we do all this.
nqp::bindattr_i(nqp::decont($attr), $attr.WHAT, '$!inlined', 1);
# make $scoped
nqp::bindattr(nqp::decont($/), $/.WHAT, '$!made', $scoped);
}
}
nqp::bindkey(%*LANG, 'MAIN', %*LANG<MAIN>.HOW.mixin(%*LANG<MAIN>, HAS-decl-grammar));
nqp::bindkey(%*LANG, 'MAIN-actions', %*LANG<MAIN>.HOW.mixin(%*LANG<MAIN-actions>, HAS-decl-actions));

{}
}

# vim:ft=perl6
2 changes: 1 addition & 1 deletion t/04-nativecall/06-struct.t
Expand Up @@ -92,7 +92,7 @@ class PointerStruct is repr('CStruct') {
}

class StructIntStruct is repr('CStruct') {
has IntStruct $.a is inlined;
HAS IntStruct $.a;
has int32 $.i;
}

Expand Down
6 changes: 3 additions & 3 deletions t/04-nativecall/13-union.t
Expand Up @@ -21,7 +21,7 @@ class MyStruct is repr('CStruct') {
has long $.long;
has num64 $.num;
has int8 $.byte;
has Onion $.onion is inlined;
HAS Onion $.onion;
has num32 $.float;

method init() {
Expand Down Expand Up @@ -122,8 +122,8 @@ class YourStruct is repr('CStruct') {
}

class UnionOfStructs is repr('CUnion') {
has MyStruct $.a is inlined;
has YourStruct $.b is inlined;
HAS MyStruct $.a;
HAS YourStruct $.b;
}

sub ReturnUnionOfStructs() returns UnionOfStructs is native('./13-union') { * }
Expand Down

0 comments on commit c006cd1

Please sign in to comment.