/
ConcreteRoleHOW.nqp
136 lines (118 loc) · 3.68 KB
/
ConcreteRoleHOW.nqp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
class Perl6::Metamodel::ConcreteRoleHOW
does Perl6::Metamodel::Naming
does Perl6::Metamodel::Versioning
does Perl6::Metamodel::PrivateMethodContainer
does Perl6::Metamodel::MethodContainer
does Perl6::Metamodel::MultiMethodContainer
does Perl6::Metamodel::AttributeContainer
does Perl6::Metamodel::RoleContainer
does Perl6::Metamodel::MultipleInheritance
does Perl6::Metamodel::ArrayType
does Perl6::Metamodel::Concretization
{
# Any collisions to resolve.
has @!collisions;
# The (parametric) role(s) that this concrete one was directly derived
# from.
has @!roles;
# Full flat list of done roles.
has @!role_typecheck_list;
# Are we composed yet?
has $!composed;
my $archetypes := Perl6::Metamodel::Archetypes.new( :nominal(1), :composable(1) );
method archetypes() {
$archetypes
}
method new(*%named) {
nqp::findmethod(NQPMu, 'BUILDALL')(nqp::create(self), |%named)
}
my class Collision {
has $!name;
has @!roles;
has $!private;
has $!multi;
method name() { $!name }
method roles() { @!roles }
method private() { $!private }
method multi() { $!multi }
}
method new_type(:@roles, :$name = '<anon>', :$ver, :$auth, :$repr, :$api) {
my $metarole := self.new(:roles(@roles));
my $obj := nqp::settypehll(nqp::newtype($metarole, 'Uninstantiable'), 'perl6');
$metarole.set_name($obj, $name);
$metarole.set_ver($obj, $ver);
$metarole.set_auth($obj, $auth) if $auth;
$metarole.set_api($obj, $api) if $api;
$obj;
}
method add_collision($obj, $colliding_name, @role_names, :$private = 0, :$multi) {
@!collisions[+@!collisions] := Collision.new(
:name($colliding_name), :roles(@role_names), :$private, :$multi
);
}
method compose($the-obj) {
my $obj := nqp::decont($the-obj);
RoleToRoleApplier.apply($obj, self.roles_to_compose($obj));
for self.roles_to_compose($obj) {
@!role_typecheck_list[+@!role_typecheck_list] := $_;
for $_.HOW.role_typecheck_list($_) {
@!role_typecheck_list[+@!role_typecheck_list] := $_;
}
}
for @!roles {
@!role_typecheck_list[+@!role_typecheck_list] := $_;
for $_.HOW.role_typecheck_list($_) {
@!role_typecheck_list[+@!role_typecheck_list] := $_;
}
}
self.publish_type_cache($obj);
$!composed := 1;
$obj
}
method is_composed($obj) {
$!composed ?? 1 !! 0
}
method collisions($obj) {
@!collisions
}
method roles($obj, :$transitive = 1) {
if $transitive {
my @trans;
for @!roles {
@trans.push($_);
for $_.HOW.roles($_) {
@trans.push($_);
}
}
}
else {
@!roles
}
}
method add_to_role_typecheck_list($obj, $type) {
@!role_typecheck_list[+@!role_typecheck_list] := $type;
}
method role_typecheck_list($obj) {
@!role_typecheck_list
}
method type_check($obj, $checkee) {
my $decont := nqp::decont($checkee);
if $decont =:= $obj.WHAT {
return 1;
}
for @!role_typecheck_list {
if nqp::decont($_) =:= $decont {
return 1;
}
}
0
}
method publish_type_cache($obj) {
my @types := [$obj.WHAT];
for @!role_typecheck_list { @types.push($_) }
nqp::settypecache($obj, @types)
}
method mro($obj) {
[$obj]
}
}