/
ConcreteRoleHOW.nqp
163 lines (144 loc) Β· 5.28 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
class Perl6::Metamodel::ConcreteRoleHOW
does Perl6::Metamodel::Naming
does Perl6::Metamodel::LanguageRevision
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
does Perl6::Metamodel::C3MRO
{
# 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);
Perl6::Metamodel::Configuration.role_to_role_applier_type.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
}
# It makes sense for concretizations to default to MRO order of roles.
method roles($obj, :$transitive = 1, :$mro = 1) {
self.roles-ordered($obj, @!roles, :$transitive, :$mro);
}
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, :$roles = 0, :$unhidden = 0) {
[$obj]
}
method find_method_qualified($obj, $qtype, $name) {
$obj := nqp::decont($obj);
$qtype := nqp::decont($qtype);
if $qtype.HOW.archetypes.parametric {
my $found-role := nqp::null();
for self.concretizations($obj, :transitive) {
my $candidate := $_;
my $role := $_.HOW.roles($_, :!transitive, :!mro)[0];
if nqp::can($role.HOW, 'group') {
$role := $role.HOW.group($role);
}
if $qtype =:= $role {
# XXX Better be replaced with Exception throwing. The mechanizm could be provided via
# Perl6::Metamodel::Configuration where a property could be set pointing to a Raku object.
# It could be something like:
# Perl6::Metamodel::Configuration.throw("nqp::die message", ['X', 'Method', 'Ambiguous'], |%exception-params);
nqp::die("Ambiguous concretization lookup for " ~ $qtype.HOW.name($qtype))
unless nqp::isnull($found-role);
$found-role := $candidate;
}
}
nqp::isnull($found-role)
?? nqp::null()
!! $found-role.HOW.method_table($found-role){$name}
|| $found-role.HOW.submethod_table($found-role){$name}
|| nqp::null()
}
elsif nqp::istype($obj, $qtype) {
# Non-parametric, so just locate it from the already concrete type.
nqp::findmethod($qtype, $name)
}
else {
nqp::null()
}
}
}