/
type.rakumod
553 lines (476 loc) Β· 18.1 KB
/
type.rakumod
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
# Some kind of type (done by all kinds of things that result in a type).
class RakuAST::Type
is RakuAST::Term
is RakuAST::Meta
{
# Checks if the type is statically known to be some particular type
# (provided as the type object, not as another RakuAST node).
method is-known-to-be(Mu $type) {
nqp::die('Expected a type object') if nqp::isconcrete($type);
if nqp::istype(self, RakuAST::Lookup) && self.is-resolved {
my $resolution := self.resolution;
if nqp::istype($resolution, RakuAST::CompileTimeValue) {
return nqp::istype($resolution.compile-time-value, $type);
}
}
0
}
method is-known-to-be-exactly(Mu $type) {
nqp::die('Expected a type object') if nqp::isconcrete($type);
if nqp::istype(self, RakuAST::Lookup) && self.is-resolved {
my $resolution := self.resolution;
if nqp::istype($resolution, RakuAST::CompileTimeValue) {
return $resolution.compile-time-value =:= $type;
}
}
0
}
}
# A simple type name, e.g. Int, Foo::Bar, etc.
class RakuAST::Type::Simple
is RakuAST::Type
is RakuAST::Lookup
{
has RakuAST::Name $.name;
method new(RakuAST::Name $name) {
my $obj := nqp::create(self);
nqp::bindattr($obj, RakuAST::Type::Simple, '$!name', $name);
$obj
}
method resolve-with(RakuAST::Resolver $resolver) {
my $resolved := $resolver.resolve-name-constant($!name);
if $resolved {
self.set-resolution($resolved);
}
Nil
}
method PRODUCE-META-OBJECT() {
self.resolution.compile-time-value
}
method IMPL-EXPR-QAST(RakuAST::IMPL::QASTContext $context) {
my $value := self.resolution.compile-time-value;
if $value.HOW.archetypes.generic {
QAST::Var.new( :name($!name.canonicalize), :scope('lexical') )
}
else {
$context.ensure-sc($value);
QAST::WVal.new( :$value )
}
}
method IMPL-CAN-INTERPRET() {
self.is-resolved && nqp::istype(self.resolution, RakuAST::CompileTimeValue)
}
method IMPL-INTERPRET(RakuAST::IMPL::InterpContext $ctx) {
self.resolution.compile-time-value
}
method visit-children(Code $visitor) {
$visitor($!name);
}
}
# A simple type name, e.g. Int, Foo::Bar, etc. that should be looked up in the
# setting.
class RakuAST::Type::Setting
is RakuAST::Type::Simple
{
# TODO limit lookup to setting
}
class RakuAST::Type::Derived
is RakuAST::Type
is RakuAST::Lookup
{
has RakuAST::Type $.base-type;
method resolve-with(RakuAST::Resolver $resolver) {
$!base-type.resolve-with($resolver);
self.set-resolution(self);
Nil
}
}
class RakuAST::Type::Coercion
is RakuAST::Type::Derived
is RakuAST::Declaration
{
has RakuAST::Type $.constraint;
method new(RakuAST::Type $base-type, Mu $constraint) {
my $obj := nqp::create(self);
nqp::bindattr($obj, RakuAST::Type::Derived, '$!base-type', $base-type);
nqp::bindattr($obj, RakuAST::Type::Coercion, '$!constraint', $constraint);
$obj
}
method PRODUCE-META-OBJECT() {
Perl6::Metamodel::CoercionHOW.new_type(
self.base-type.compile-time-value,
$!constraint.meta-object,
);
}
method IMPL-EXPR-QAST(RakuAST::IMPL::QASTContext $context) {
my $value := self.meta-object;
$context.ensure-sc($value);
QAST::WVal.new( :$value )
}
method IMPL-CAN-INTERPRET() {
nqp::istype(self.base-type, RakuAST::CompileTimeValue)
&& nqp::istype($!constraint, RakuAST::CompileTimeValue)
}
method IMPL-INTERPRET(RakuAST::IMPL::InterpContext $ctx) {
self.meta-object
}
method visit-children(Code $visitor) {
$visitor(self.base-type);
$visitor($!constraint);
}
method is-simple-lexical-declaration() {
False
}
}
class RakuAST::Type::Definedness
is RakuAST::Type::Derived
is RakuAST::Declaration
{
has Bool $.definite;
method new(RakuAST::Type $base-type, Bool $definite) {
my $obj := nqp::create(self);
nqp::bindattr($obj, RakuAST::Type::Derived, '$!base-type', $base-type);
nqp::bindattr($obj, RakuAST::Type::Definedness, '$!definite', $definite ?? True !! False);
$obj
}
method PRODUCE-META-OBJECT() {
Perl6::Metamodel::DefiniteHOW.new_type(
:base_type(self.base-type.compile-time-value),
:definite($!definite),
);
}
method IMPL-EXPR-QAST(RakuAST::IMPL::QASTContext $context) {
my $value := self.meta-object;
$context.ensure-sc($value);
QAST::WVal.new( :$value )
}
method IMPL-CAN-INTERPRET() {
nqp::istype(self.base-type, RakuAST::CompileTimeValue)
}
method IMPL-INTERPRET(RakuAST::IMPL::InterpContext $ctx) {
self.meta-object
}
method is-simple-lexical-declaration() {
False
}
method visit-children(Code $visitor) {
$visitor(self.base-type);
}
}
class RakuAST::Type::Capture
is RakuAST::Type
is RakuAST::Declaration
{
has RakuAST::Name $.name;
method new(RakuAST::Name $name) {
my $obj := nqp::create(self);
nqp::bindattr($obj, RakuAST::Type::Capture, '$!name', $name);
$obj
}
method lexical-name() {
$!name.canonicalize
}
method generate-lookup() {
my $lookup := RakuAST::Term::Name.new($!name);
$lookup.set-resolution(self);
$lookup
}
method default-scope() { 'my' }
method allowed-scopes() { self.IMPL-WRAP-LIST(['my']) }
method PRODUCE-META-OBJECT() {
Perl6::Metamodel::GenericHOW.new_type(
:name($!name.canonicalize),
);
}
method IMPL-EXPR-QAST(RakuAST::IMPL::QASTContext $context) {
self.IMPL-LOOKUP-QAST($context)
}
method IMPL-QAST-DECL(RakuAST::IMPL::QASTContext $context) {
QAST::Var.new( :decl('var'), :scope('lexical'), :name($!name.canonicalize) )
}
method IMPL-BIND-QAST(RakuAST::IMPL::QASTContext $context, Mu $source-qast) {
QAST::Op.new(
:op('bind'),
QAST::Var.new( :scope('lexical'), :name($!name.canonicalize) ),
QAST::Op.new(:op('what'), $source-qast)
)
}
method IMPL-LOOKUP-QAST(RakuAST::IMPL::QASTContext $context) {
QAST::Var.new( :name($!name.canonicalize), :scope('lexical') )
}
method IMPL-CAN-INTERPRET() {
True
}
method IMPL-INTERPRET(RakuAST::IMPL::InterpContext $ctx) {
self.meta-object
}
method visit-children(Code $visitor) {
$visitor($!name);
}
}
class RakuAST::Type::Parameterized
is RakuAST::Type::Derived
is RakuAST::Declaration
{
has RakuAST::ArgList $.args;
method new(RakuAST::Type $base-type, RakuAST::ArgList $args) {
nqp::die('need a base-type, not ' ~ $base-type.dump) if !nqp::istype($base-type, RakuAST::Type);
my $obj := nqp::create(self);
nqp::bindattr($obj, RakuAST::Type::Derived, '$!base-type', $base-type);
nqp::bindattr($obj, RakuAST::Type::Parameterized, '$!args', $args);
$obj
}
method visit-children(Code $visitor) {
$visitor(self.base-type);
$visitor($!args);
}
method PRODUCE-META-OBJECT() {
if $!args.IMPL-HAS-ONLY-COMPILE-TIME-VALUES {
my $args := $!args.IMPL-COMPILE-TIME-VALUES;
my @pos := $args[0];
my %named := $args[1];
my $ptype := self.base-type.compile-time-value;
$ptype.HOW.parameterize($ptype, |@pos, |%named)
}
else {
nqp::die('Cannot do compile time parameterization with these args');
}
}
method IMPL-EXPR-QAST(RakuAST::IMPL::QASTContext $context) {
if $!args.IMPL-HAS-ONLY-COMPILE-TIME-VALUES {
my $value := self.meta-object;
$context.ensure-sc($value);
QAST::WVal.new( :$value )
}
else {
my $ptype := self.base-type.compile-time-value;
my $ptref := QAST::WVal.new( :value($ptype) );
my $qast := QAST::Op.new(:op<callmethod>, :name<parameterize>, QAST::Op.new(:op<how>, $ptref), $ptref);
$!args.IMPL-ADD-QAST-ARGS($context, $qast);
$qast
}
}
method IMPL-CAN-INTERPRET() {
nqp::istype(self.base-type, RakuAST::CompileTimeValue)
&& $!args.IMPL-CAN-INTERPRET
}
method IMPL-INTERPRET(RakuAST::IMPL::InterpContext $ctx) {
self.meta-object
}
method is-simple-lexical-declaration() {
False
}
}
class RakuAST::Type::Subset
is RakuAST::Type
is RakuAST::Declaration
is RakuAST::BeginTime
is RakuAST::TraitTarget
is RakuAST::StubbyMeta
is RakuAST::Attaching
{
has RakuAST::Name $.name;
has RakuAST::Expression $.where;
has RakuAST::Declaration $.of;
has RakuAST::Package $!current-package;
method new(RakuAST::Name :$name, RakuAST::Expression :$where, List :$traits, str :$scope) {
my $obj := nqp::create(self);
nqp::bindattr($obj, RakuAST::Type::Subset, '$!name', $name);
nqp::bindattr($obj, RakuAST::Type::Subset, '$!where', $where);
nqp::bindattr_s($obj, RakuAST::Declaration, '$!scope', $scope);
for $obj.IMPL-UNWRAP-LIST($traits) {
$obj.add-trait($_.ast);
}
$obj
}
method default-scope() { 'our' }
method allowed-scopes() { self.IMPL-WRAP-LIST(['my', 'our']) }
method lexical-name() { $!name.canonicalize }
method generate-lookup() {
my $lookup := RakuAST::Term::Name.new($!name);
$lookup.set-resolution(self);
$lookup
}
method visit-children(Code $visitor) {
$visitor($!name);
$visitor($!where);
# External constants break if visited with missing IMPL-QAST-DECL. Adding a sensible IMPL-QAST-DECL
# results in lexical declarations for things like Int, which will break if added more than once.
$visitor($!of) if $!of && !nqp::istype($!of, RakuAST::Declaration::External::Constant);
# Below fails with No such method 'apply-sink' for invocant of type 'GLOBALish'
#$visitor($!current-package);
}
method is-lexical() { True }
method is-simple-lexical-declaration() { False }
method attach(RakuAST::Resolver $resolver) {
nqp::bindattr(self, RakuAST::Type::Subset, '$!current-package', $resolver.current-package);
}
method IMPL-EXPR-QAST(RakuAST::IMPL::QASTContenxt $context) {
my $value := self.meta-object;
$context.ensure-sc($value);
QAST::WVal.new( :$value )
}
method IMPL-INSTALL-SUBSET(RakuAST::Resolver $resolver, RakuAST::Name $name, Mu $type-object) {
my str $scope := self.scope;
my $target;
my $final;
my $lexical;
my $lexically-registered;
if $name.is-identifier {
$final := $name.canonicalize;
$lexical := $resolver.resolve-lexical-constant($final);
# If the subset shares the name of a stubbed package, it will have resolved via the above.
if !$lexical {
$resolver.current-scope.merge-generated-lexical-declaration:
# TODO: Using Implicit::Constant because it had the right type signature/QAST output
RakuAST::VarDeclaration::Implicit::Constant.new:
:name($final),
:value($type-object),
:scope($scope);
}
$lexically-registered := True;
# If `our`-scoped, also put it into the current package.
if $scope eq 'our' {
# TODO conflicts
$target := $!current-package;
}
} else {
my @parts := nqp::clone(self.IMPL-UNWRAP-LIST($name.parts));
$final := nqp::pop(@parts).name;
my $resolved := $resolver.partially-resolve-name-constant(RakuAST::Name.new(|@parts));
if $resolved { # first parts of the name found
$resolved := self.IMPL-UNWRAP-LIST($resolved);
$target := $resolved[0];
my $parts := $resolved[1];
my @parts := self.IMPL-UNWRAP-LIST($parts);
$scope := 'our'; # Ensure we install the package into the parent stash
if nqp::elems(@parts) {
my $longname := $target.HOW.name($target);
for @parts {
$longname := $longname ~ '::' ~ $_.name;
my $package := Perl6::Metamodel::PackageHOW.new_type(name => $longname);
$package.HOW.compose($package);
my %stash := $resolver.IMPL-STASH-HASH($target);
%stash{$_.name} := $package;
$target := $package;
}
}
} else {
my $first := nqp::shift(@parts).name;
$target := Perl6::Metamodel::PackageHOW.new_type(name => $first);
$target.HOW.compose($target);
$resolver.current-scope.merge-generated-lexical-declaration:
RakuAST::Declaration::LexicalPackage.new:
:lexical-name($first),
:compile-time-value($target),
:package($!current-package);
if $scope eq 'our' {
# TODO conflicts
my %stash := $resolver.IMPL-STASH-HASH($!current-package);
%stash{$first} := $target;
}
$scope := 'our'; # Ensure we install the package into the generated stub
my $longname := $first;
for @parts {
$longname := $longname ~ '::' ~ $_.name;
my $package := Perl6::Metamodel::PackageHOW.new_type(name => $longname);
$package.HOW.compose($package);
my %stash := $resolver.IMPL-STASH-HASH($target);
%stash{$_.name} := $package;
$target := $package;
}
}
$lexical := $resolver.resolve-lexical-constant($final);
}
my %stash := $resolver.IMPL-STASH-HASH($target);
# upgrade a lexically imported package stub to package scope if it exists
if $lexical {
%stash{$final} := $lexical.compile-time-value;
}
# Take care of installing in case we had to make or find packages
if !$lexically-registered {
$resolver.current-scope.merge-generated-lexical-declaration:
#TODO: Using Implicit::Constant because it had the right type signature/QAST output combination
RakuAST::VarDeclaration::Implicit::Constant.new:
:name($final),
:value($type-object),
:scope($scope);
}
if $scope eq 'our' {
if nqp::existskey(%stash, $final) {
nqp::setwho($type-object, %stash{$final}.WHO);
}
%stash{$final} := $type-object;
}
}
method is-begin-performed-after-children() { True }
method PERFORM-BEGIN-AFTER-CHILDREN(RakuAST::Resolver $resolver, RakuAST::IMPL::QASTContext $context) {
self.apply-traits($resolver, $context, self);
for self.IMPL-UNWRAP-LIST(self.traits) {
if nqp::istype($_, RakuAST::Trait::Of) {
if $!of {
nqp::die("Cannot declare more than one 'of' trait per subset");
}
my $of-type := $resolver.resolve-name-constant($_.type.name);
nqp::bindattr(self, RakuAST::Type::Subset, '$!of', $of-type);
}
}
my $block;
if !$!where.IMPL-CURRIED && (!nqp::istype($!where, RakuAST::Code) || nqp::istype($!where, RakuAST::RegexThunk)) {
$block := RakuAST::Block.new(
body => RakuAST::Blockoid.new(
RakuAST::StatementList.new(
RakuAST::Statement::Expression.new(
expression => RakuAST::ApplyPostfix.new(
operand => RakuAST::ApplyPostfix.new(
operand => $!where,
postfix => RakuAST::Call::Method.new(
name => RakuAST::Name.from-identifier('ACCEPTS'),
args => RakuAST::ArgList.new(
RakuAST::Var::Lexical.new('$_'),
),
),
),
postfix => RakuAST::Call::Method.new(
name => RakuAST::Name.from-identifier('Bool'),
),
),
),
),
),
);
$block.IMPL-CHECK($resolver, $context, False);
nqp::bindattr(self, RakuAST::Type::Subset, '$!where', $block);
}
my $type-object := self.stubbed-meta-object;
$type-object.HOW.set_name(
$type-object,
$!name.qualified-with(
RakuAST::Name.from-identifier-parts(
|nqp::split('::', $!current-package.HOW.name($!current-package))
)
).canonicalize(:colonpairs(0))
) if !nqp::eqaddr($!current-package, $resolver.get-global);
self.IMPL-INSTALL-SUBSET($resolver, $!name, $type-object);
}
method PRODUCE-STUBBED-META-OBJECT() {
Perl6::Metamodel::SubsetHOW.new_type(
:name($!name.canonicalize),
:refinee(Any),
:refinement(Any)
)
}
method PRODUCE-META-OBJECT() {
my $type := self.stubbed-meta-object;
if $!of {
$type.HOW.set_of($type, $!of.compile-time-value);
}
$type.HOW.set_where(
$type,
$!where.IMPL-CURRIED
?? $!where.IMPL-CURRIED.meta-object
!! $!where.compile-time-value
);
$type
}
}