This file is indexed.

/usr/lib/perl6/sources/24DD121B5B4774C04A7084827BFAD92199756E03 is in rakudo 2018.03-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
use nqp;
use QAST:from<NQP>;

use NativeCall::Types;
use NativeCall::Compiler::GNU;
use NativeCall::Compiler::MSVC;

my $repr_map := nqp::hash(
  "CArray",    "carray",
  "CPPStruct", "cppstruct",
  "CPointer",  "cpointer",
  "CStruct",   "cstruct",
  "CUnion",    "cunion",
  "VMArray",   "vmarray",
);

module NativeCall {

my constant long          is export(:types, :DEFAULT) = NativeCall::Types::long;
my constant longlong      is export(:types, :DEFAULT) = NativeCall::Types::longlong;
my constant ulong         is export(:types, :DEFAULT) = NativeCall::Types::ulong;
my constant ulonglong     is export(:types, :DEFAULT) = NativeCall::Types::ulonglong;
my constant bool          is export(:types, :DEFAULT) = NativeCall::Types::bool;
my constant size_t        is export(:types, :DEFAULT) = NativeCall::Types::size_t;
my constant ssize_t       is export(:types, :DEFAULT) = NativeCall::Types::ssize_t;
my constant void          is export(:types, :DEFAULT) = NativeCall::Types::void;
my constant CArray        is export(:types, :DEFAULT) = NativeCall::Types::CArray;
my constant Pointer       is export(:types, :DEFAULT) = NativeCall::Types::Pointer;
my constant OpaquePointer is export(:types, :DEFAULT) = NativeCall::Types::Pointer;


# Role for carrying extra calling convention information.
my role NativeCallingConvention[$name] {
    method native_call_convention() { $name };
}

# Role for carrying extra string encoding information.
my role NativeCallEncoded[$name] {
    method native_call_encoded() { $name };
}

my role NativeCallMangled[$name] {
    method native_call_mangled() { $name }
}


# Throwaway type just to get us some way to get at the NativeCall
# representation.
my class native_callsite is repr('NativeCall') { }

# Maps a chosen string encoding to a type recognized by the native call engine.
sub string_encoding_to_nci_type(\encoding) {
    my str $enc = encoding;
    nqp::iseq_s($enc,"utf8")
      ?? "utf8str"
      !! nqp::iseq_s($enc,"ascii")
        ?? "asciistr"
        !! nqp::iseq_s($enc,"utf16")
          ?? "utf16str"
          !! die "Unknown string encoding for native call: $enc"
}

# Builds a hash of type information for the specified parameter.
sub param_hash_for(Parameter $p) {
    my Mu $result := nqp::hash();
    my $type := $p.type();
    nqp::bindkey($result, 'typeobj', nqp::decont($type));
    nqp::bindkey($result, 'rw', nqp::unbox_i(1)) if $p.rw;
    if $type ~~ Str {
        my $enc := $p.?native_call_encoded() || 'utf8';
        nqp::bindkey($result, 'type', nqp::unbox_s(string_encoding_to_nci_type($enc)));
        nqp::bindkey($result, 'free_str', nqp::unbox_i(1));
    }
    elsif $type ~~ Callable {
        nqp::bindkey($result, 'type', nqp::unbox_s(type_code_for($type)));
        my $info := param_list_for($p.sub_signature);
        nqp::unshift($info, return_hash_for($p.sub_signature, :with-typeobj));
        nqp::bindkey($result, 'callback_args', $info);
    }
    else {
        nqp::bindkey($result, 'type', nqp::unbox_s(type_code_for($type)));
    }
    $result
}

# Builds the list of parameter information for a callback argument.
sub param_list_for(Signature $sig, &r?) {
    my $params   := nqp::getattr($sig.params,List,'$!reified');
    my int $elems = nqp::elems($params);

    # not sending Method's default slurpy *%_ (which is always last)
    --$elems
      if nqp::istype(&r,Method)
      && nqp::iseq_s(nqp::atpos($params,$elems - 1).name,'%_');

    # build list
    my $result := nqp::setelems(nqp::list,$elems);
    my int $i   = -1;
    nqp::bindpos($result,$i,
      param_hash_for(nqp::atpos($params,$i))
    ) while nqp::islt_i($i = nqp::add_i($i,1),$elems);

    $result
}

# Builds a hash of type information for the specified return type.
sub return_hash_for(Signature $s, &r?, :$with-typeobj, :$entry-point) {
    my Mu $result := nqp::hash();
    my $returns := $s.returns;
    nqp::bindkey($result, 'typeobj',     nqp::decont($returns))     if $with-typeobj;
    nqp::bindkey($result, 'entry_point', nqp::decont($entry-point)) if $entry-point;
    if $returns ~~ Str {
        my $enc := &r.?native_call_encoded() || 'utf8';
        nqp::bindkey($result, 'type', nqp::unbox_s(string_encoding_to_nci_type($enc)));
        nqp::bindkey($result, 'free_str', nqp::unbox_i(0));
    }
    # TODO: If we ever want to handle function pointers returned from C, this
    # bit of code needs to handle that.
    else {
        nqp::bindkey($result, 'type',
            $returns =:= Mu ?? 'void' !! nqp::unbox_s(type_code_for($returns)));
    }
    $result
}

my $signed_ints_by_size :=
  nqp::list_s( "", "char", "short", "", "int", "", "", "", "longlong" );

# Gets the NCI type code to use based on a given Perl 6 type.
my $type_map := nqp::hash(
  "Bool",       nqp::atpos_s($signed_ints_by_size,nativesizeof(bool)),
  "bool",       nqp::atpos_s($signed_ints_by_size,nativesizeof(bool)),
  "Callable",   "callback",
  "Int",        "longlong",
  "int",        "long",
  "int16",      "short",
  "int32",      "int",
  "int64",      "longlong",
  "int8",       "char",
  "long",       "long",
  "longdouble", "longdouble",
  "longlong",   "longlong",
  "Num",        "double",
  "num",        "double",
  "num32",      "float",
  "num64",      "double",
  "size_t",     nqp::atpos_s($signed_ints_by_size,nativesizeof(size_t)),
  "ssize_t",    nqp::atpos_s($signed_ints_by_size,nativesizeof(ssize_t)),
  "uint",       "ulong",
  "uint16",     "ushort",
  "uint32",     "uint",
  "uint64",     "ulonglong",
  "uint8",      "uchar",
  "ulong",      "ulong",
  "ulonglong",  "ulonglong",
);

sub type_code_for(Mu ::T) {
    if nqp::atkey($type_map,T.^shortname) -> $type {
        $type
    }
    elsif nqp::atkey($repr_map,T.REPR) -> $type {
        $type
    }
    # the REPR of a Buf or Blob type object is Uninstantiable, so
    # needs an extra special case here that isn't covered in the
    # hash lookup above.
    elsif nqp::istype(T,Blob) {
        "vmarray"
    }
    elsif nqp::istype(T,Pointer) {
        "cpointer"
    }
    else {
        die
"Unknown type {T.^name} used in native call.\n" ~
"If you want to pass a struct, be sure to use the CStruct or\n" ~
"CPPStruct representation.\n" ~
"If you want to pass an array, be sure to use the CArray type.";
    }
}

sub gen_native_symbol(Routine $r, :$cpp-name-mangler) {
    if ! $r.?native_call_mangled {
        # Native symbol or name is said to be already mangled
        $r.?native_symbol // $r.name;
    } elsif $r.package.REPR eq 'CPPStruct' {
        # Mangle C++ classes
        $cpp-name-mangler($r, $r.?native_symbol // ($r.package.^name ~ '::' ~ $r.name));
    } else {
        # Mangle C
        $cpp-name-mangler($r, $r.?native_symbol // $r.name)
    }
}

multi sub map_return_type(Mu $type) { Mu }
multi sub map_return_type($type) {
    nqp::istype($type, Int) ?? Int
                            !! nqp::istype($type, Num) ?? Num !! $type;
}

my role NativeCallSymbol[Str $name] {
    method native_symbol()  { $name }
}

sub guess_library_name($lib) is export(:TEST) {
    my $libname;
    my $apiversion = '';
    my Str $ext = '';
    given $lib {
        when IO::Path {
            $libname = $lib.absolute;
        }
        when Distribution::Resource {
            return $lib.platform-library-name.Str;
        }
        when Callable {
           return $lib();
        }
        when List {
           $libname = $lib[0];
           $apiversion = $lib[1];
        }
        when Str {
           $libname = $lib;
        }
    }
    return '' unless $libname.DEFINITE;
    #Already a full name?
    return $libname if ($libname ~~ /\.<.alpha>+$/ or $libname ~~ /\.so(\.<.digit>+)+$/);
    return $*VM.platform-library-name($libname.IO, :version($apiversion || Version)).Str;
}

my %lib;
my @cpp-name-mangler =
    &NativeCall::Compiler::MSVC::mangle_cpp_symbol,
    &NativeCall::Compiler::GNU::mangle_cpp_symbol,
;

sub guess-name-mangler(Routine $r, Str $libname) {
    if $r.package.REPR eq 'CPPStruct' {
        my $sym = $r.?native_symbol // ($r.package.^name ~ '::' ~ $r.name);
        for @cpp-name-mangler -> &mangler {
            return &mangler if try cglobal($libname, mangler($r, $sym), Pointer)
        }
        die "Don't know how to mangle symbol '$sym' for library '$libname'"
    }
    elsif $r.?native_call_mangled {
        my $sym = $r.?native_symbol // $r.name;
        for @cpp-name-mangler -> &mangler {
            return &mangler if try cglobal($libname, mangler($r, $sym), Pointer)
        }
        die "Don't know how to mangle symbol '$sym' for library '$libname'"
    }
}

my Lock $setup-lock .= new;

# This role is mixed in to any routine that is marked as being a
# native call.
our role Native[Routine $r, $libname where Str|Callable|List|IO::Path|Distribution::Resource] {
    has int $!setup;
    has int $!precomp-setup;
    has native_callsite $!call is box_target;
    has Mu $!rettype;
    has $!cpp-name-mangler;
    has Pointer $!entry-point;
    has int $!arity;
    has int8 $!is-clone;
    has int8 $!any-optionals;
    has Mu $!optimized-body;
    has Mu $!jit-optimized-body;

    method !setup() {
        $setup-lock.protect: {
            return if $!setup || $*W && $*W.is_precompilation_mode && $!precomp-setup;
            # Make sure that C++ methods are treated as mangled (unless set otherwise)
            if self.package.REPR eq 'CPPStruct' and not self.does(NativeCallMangled) {
              self does NativeCallMangled[True];
            }

            my $guessed_libname = guess_library_name($libname);
            if self.does(NativeCallMangled) and $r.?native_call_mangled {
              # if needed, try to guess mangler
              $!cpp-name-mangler  = %lib{$guessed_libname} //
                  (%lib{$guessed_libname} = guess-name-mangler($r, $guessed_libname));
            }
            my Mu $arg_info := param_list_for($r.signature, $r);
            my $conv = self.?native_call_convention || '';
            my $jitted = nqp::buildnativecall(self,
                nqp::unbox_s($guessed_libname),                           # library name
                nqp::unbox_s(gen_native_symbol($r, :$!cpp-name-mangler)), # symbol to call
                nqp::unbox_s($conv),        # calling convention
                $arg_info,
                return_hash_for($r.signature, $r, :$!entry-point));
            $!rettype := nqp::decont(map_return_type($r.returns)) unless $!rettype;
            $!arity = $r.signature.arity;
            ($*W && $*W.is_precompilation_mode ?? $!precomp-setup !! $!setup) = $jitted ?? 2 !! 1;

            $!any-optionals = self!any-optionals;

            my $body := $jitted ?? $!jit-optimized-body !! $!optimized-body;
            if $body {
                nqp::bindattr(
                    self,
                    Code,
                    '$!do',
                    nqp::getattr(nqp::hllizefor($body, 'perl6'), ForeignCode, '$!do')
                );
                nqp::setinvokespec(self,
                    Code.HOW.invocation_attr_class(Code),
                    Code.HOW.invocation_attr_name(Code),
                    nqp::null());
            }
        }
    }

    method !any-optionals() {
        for $r.signature.params -> $p {
            return True if $p.optional
        }
        return False
    }

    method !decont-for-type($type) {
           $type ~~ Str ?? 'decont_s'
        !! $type ~~ Int ?? 'decont_i'
        !! $type ~~ Num ?? 'decont_n'
        !! 'decont';
    }

    method !create-jit-compiled-function-body(Routine $r) {
        my $block := QAST::Block.new(:name($r.name), :arity($!arity), :blocktype('declaration_static'));
        my $locals = 0;
        my $args = 0;
        my (@params, @assigns);
        for $r.signature.params {
            next if nqp::istype($r, Method) && ($_.name // '') eq '%_';
            $args++;
            my $name = $_.name || '__anonymous_param__' ~ $++;
            my $lowered_param_name = '__lowered_param__' ~ $locals;
            my $lowered_name = '__lowered__' ~ $locals++;
            $block.push: QAST::Var.new(
                :name($lowered_name),
                :scope<local>,
                :decl<var>,
                :returns(
                       $_.type ~~ Str ?? nqp::bootstr()
                    !! $_.type ~~ Int ?? nqp::bootint()
                    !! $_.type ~~ Num ?? nqp::bootnum()
                    !! $_.type
                ),
            );
            @params.push: QAST::Var.new(:scope<local>, :name($lowered_name));
            $block.push: QAST::Var.new(
                :name($lowered_param_name),
                :scope<local>,
                :decl<param>,
                :slurpy($_.slurpy ?? 1 !! 0),
            );
            if $_.rw and nqp::objprimspec($_.type) > 0 {
                $block.push: QAST::Var.new(
                    :name($name),
                    :scope<lexicalref>,
                    :decl<var>,
                    :returns($_.type),
                );
                $block.push:
                    QAST::Op.new(
                        :op<bind>,
                        QAST::Var.new(:scope<lexicalref>, :name($name)),
                        QAST::Var.new(:scope<local>, :name($lowered_param_name)),
                    );
            }
            $block.push: QAST::Op.new(
                :op<if>,
                QAST::Op.new(
                    :op<isconcrete>,
                    QAST::Var.new(:scope<local>, :name($lowered_param_name)),
                ),
                QAST::Op.new(
                    :op<bind>,
                    QAST::Var.new(:scope<local>, :name($lowered_name)),
                    QAST::Op.new(
                        :op(self!decont-for-type($_.type)),
                        QAST::Var.new(:scope<local>, :name($lowered_param_name)),
                    ),
                ),
                QAST::Op.new(
                    :op<bind>,
                    QAST::Var.new(:scope<local>, :name($lowered_name)),
                       $_.type ~~ Str ?? Str
                    !! $_.type ~~ Int ?? QAST::IVal.new(:value(0))
                    !! $_.type ~~ Num ?? QAST::NVal.new(:value(0))
                    !! QAST::IVal.new(:value(0))
                ),
            );

            if $_.rw and nqp::objprimspec($_.type) > 0 {
                @assigns.push: QAST::Op.new(
                    :op<assign>,
                    QAST::Var.new(:scope<lexicalref>, :name($name)),
                    QAST::Op.new(:op<getarg_i>, QAST::IVal.new(:value($args - 1))),
                );
            }
        }
        $!rettype := nqp::decont(map_return_type($r.returns)) unless $!rettype;
        my $invoke_op := QAST::Op.new(
            :op<nativeinvoke>,
            QAST::WVal.new(:value(self)),
            QAST::WVal.new(:value($!rettype)),
        );
        $invoke_op.push: nqp::decont($_) for @params;
        if @assigns {
            $block.push: QAST::Op.new(
                :op<bind>,
                QAST::Var.new(
                    :name<return_value>,
                    :scope<local>,
                    :decl<var>,
                ),
                $invoke_op
            );
            $block.push: nqp::decont($_) for @assigns;
            $block.push: QAST::Var.new(:name<return_value>, :scope<local>);
        }
        else {
            $block.push: $invoke_op;
        }
        $block
    }

    method !create-function-body(Routine $r) {
        my $block := QAST::Block.new(:name($r.name), :arity($!arity), :blocktype('declaration_static'));
        my $arglist := QAST::Op.new(:op<list>);
        my $locals = 0;
        for $r.signature.params {
            next if nqp::istype($r, Method) && ($_.name // '') eq '%_';
            my $name = $_.name || '__anonymous_param__' ~ $++;
            my $decont = self!decont-for-type($_.type);
            if $_.rw and nqp::objprimspec($_.type) > 0 {
                $block.push: QAST::Var.new(
                    :name($name),
                    :scope<lexicalref>,
                    :decl<var>,
                    :returns($_.type),
                );
                my $lowered_name = '__lowered_param__' ~ $locals++;
                $block.push: QAST::Var.new(
                    :name($lowered_name),
                    :scope<local>,
                    :decl<param>,
                    QAST::Op.new(
                        :op<bind>,
                        QAST::Var.new(:scope<lexicalref>, :name($name)),
                        QAST::Var.new(:scope<local>, :name($lowered_name)),
                    ),
                );
                $arglist.push: QAST::Var.new(:scope<lexicalref>, :name($name));
            }
            else {
                my $lowered_name = '__lowered__' ~ $locals++;
                $block.push: QAST::Var.new(
                    :name($lowered_name),
                    :scope<local>,
                    :decl<param>,
                    :slurpy($_.slurpy ?? 1 !! 0),
                );
                $block.push: QAST::Op.new(
                    :op<bind>,
                    QAST::Var.new(:scope<local>, :name($lowered_name)),
                    QAST::Op.new(
                        :op<if>,
                        QAST::Op.new(
                            :op<isconcrete>,
                            QAST::Var.new(:scope<local>, :name($lowered_name)),
                        ),
                        QAST::Op.new(
                            :op(self!decont-for-type($_.type)),
                            QAST::Var.new(:scope<local>, :name($lowered_name)),
                        ),
                        QAST::Var.new(:scope<local>, :name($lowered_name)),
                    ),
                );
                $arglist.push: QAST::Var.new(:scope<local>, :name($lowered_name));
            }
        }
        $!rettype := nqp::decont(map_return_type($r.returns)) unless $!rettype;
        $block.push: QAST::Op.new(
            :op<nativecallinvoke>,
            QAST::WVal.new(:value($!rettype)),
            QAST::WVal.new(:value(self)),
            $arglist,
        );
        $block
    }

    method !compile-function-body(Mu $block) {
        my $perl6comp := nqp::getcomp("perl6");
        my @stages = $perl6comp.stages;
        Nil until @stages.shift eq 'optimize';

        my $result := $block;
        $result := $perl6comp.^can($_)
            ?? $perl6comp."$_"($result)
            !! $perl6comp.backend."$_"($result)
            for @stages;
        my $body := nqp::compunitmainline($result);
        $*W.add_object($body) if $*W;

        nqp::setcodename($body, $r.name);
        $body
    }

    method create-optimized-call() {
        unless $!optimized-body {
            $setup-lock.protect: {
                unless nqp::defined(nqp::getobjsc(self)) {
                    if $*W {
                        $*W.add_object(self);
                    }
                    else {
                        my $sc := nqp::createsc('NativeCallSub' ~ nqp::objectid(self));
                        nqp::setobjsc(self, $sc);
                        my int $idx = nqp::scobjcount($sc);
                        nqp::scsetobj($sc, $idx, self);
                    }
                }

                my $optimized-body     := self!create-function-body($r);
                $optimized-body.annotate('code_object', self);
                $optimized-body.code_object(self);
                my $stub := nqp::freshcoderef(nqp::getattr(sub (*@args, *%named) { die "stub called" }, Code, '$!do'));
                nqp::setcodename($stub, self.name);
                nqp::markcodestatic($stub);
                nqp::markcodestub($stub);
                nqp::bindattr(self, $?CLASS, '$!optimized-body', $stub);
                my $jit-optimized-body := self!create-jit-compiled-function-body($r);
                $jit-optimized-body.annotate('code_object', self);
                $jit-optimized-body.code_object(self);
                nqp::bindattr(self, $?CLASS, '$!jit-optimized-body', $stub);
                my $fixups := QAST::Stmts.new();
                my $des := QAST::Stmts.new();
                if $*W {
                    $*W.add_root_code_ref($stub, $optimized-body);
                    $*W.add_root_code_ref($stub, $jit-optimized-body);
                    $*W.add_object($?CLASS);
                    $*UNIT.push($optimized-body);
                    $*UNIT.push($jit-optimized-body);
                    $fixups.push($*W.set_attribute(self, $?CLASS, '$!optimized-body',
                        QAST::BVal.new( :value($optimized-body) )));
                    $fixups.push($*W.set_attribute(self, $?CLASS, '$!jit-optimized-body',
                        QAST::BVal.new( :value($jit-optimized-body) )));
                    $*W.add_fixup_task(:deserialize_ast($fixups), :fixup_ast($fixups));
                }
                else {
                    $!optimized-body     := self!compile-function-body(self!create-function-body($r));
                    $!jit-optimized-body := self!compile-function-body(self!create-jit-compiled-function-body($r));
                }
            }
        }
    }

    method clone() {
        my $clone := callsame;
        nqp::bindattr_i($clone, $?CLASS, '$!is-clone', 1);
        nqp::bindattr($clone, $?CLASS, '$!optimized-body', Mu);
        nqp::bindattr($clone, $?CLASS, '$!jit-optimized-body', Mu);
        $clone
    }

    method CALL-ME(|args) {
        self.create-optimized-call() unless
            $!is-clone # Clones and original would share the invokespec but not the $!do attribute
            or $!any-optionals # the compiled code doesn't support optional parameters yet
            or $*W;    # Avoid issues with compiling specialized version during BEGIN time
        self!setup();

        my Mu $args := nqp::getattr(nqp::decont(args), Capture, '@!list');
        if nqp::elems($args) != $!arity {
            X::TypeCheck::Argument.new(
                :objname($.name),
                :arguments(args.list.map(*.^name))
                :signature(try $r.signature.gist),
            ).throw
        }

        nqp::nativecall($!rettype, self, $args)
    }
}

multi sub postcircumfix:<[ ]>(CArray:D \array, $pos) is export(:DEFAULT, :types) {
    $pos ~~ Iterable ?? $pos.map: { array.AT-POS($_) } !! array.AT-POS($pos);
}
multi sub postcircumfix:<[ ]>(CArray:D \array, *@pos) is export(:DEFAULT, :types) {
    @pos.map: { array.AT-POS($_) };
}

multi trait_mod:<is>(Routine $r, :$symbol!) is export(:DEFAULT, :traits) {
    $r does NativeCallSymbol[$symbol];
}

# Specifies the calling convention to use for a native call.
multi trait_mod:<is>(Routine $r, :$nativeconv!) is export(:DEFAULT, :traits) {
    $r does NativeCallingConvention[$nativeconv];
}

# Ways to specify how to marshall strings.
multi trait_mod:<is>(Parameter $p, :$encoded!) is export(:DEFAULT, :traits) {
    $p does NativeCallEncoded[$encoded];
}
multi trait_mod:<is>(Routine $p, :$encoded!) is export(:DEFAULT, :traits) {
    $p does NativeCallEncoded[$encoded];
}

multi trait_mod:<is>(Routine $p, :$mangled!) is export(:DEFAULT, :traits) {
    $p does NativeCallMangled[$mangled === True ?? 'C++' !! $mangled];
}

role ExplicitlyManagedString {
    has $.cstr is rw;
}

multi explicitly-manage(Str $x, :$encoding = 'utf8') is export(:DEFAULT,
:utils) {
    $x does ExplicitlyManagedString;
    my $class = class CStr is repr('CStr') { method encoding() { $encoding; } };
    $x.cstr = nqp::box_s(nqp::unbox_s($x), nqp::decont($class));
}

role CPPConst {
    method cpp-const() { 1 }
}
multi trait_mod:<is>(Parameter $p, :$cpp-const!) is export(:DEFAULT, :traits) {
    $p does CPPConst;
}

role CPPRef {
    method cpp-ref() { 1 }
}
multi trait_mod:<is>(Parameter $p, :$cpp-ref!) is export(:DEFAULT, :traits) {
    $p does CPPRef;
}

multi refresh($obj) is export(:DEFAULT, :utils) {
    nqp::nativecallrefresh($obj);
    1;
}

multi sub nativecast(Signature $target-type, $source) is export(:DEFAULT) {
    my $r := sub { };
    $r does Native[$r, Str];
    nqp::bindattr($r, Code, '$!signature', nqp::decont($target-type));
    nqp::bindattr($r, $r.WHAT, '$!entry-point', $source);
    $r
}

multi sub nativecast(Int $target-type, $source) is export(:DEFAULT) {
    nqp::nativecallcast(nqp::decont($target-type),
        Int, nqp::decont($source));
}

multi sub nativecast(Num $target-type, $source) is export(:DEFAULT) {
    nqp::nativecallcast(nqp::decont($target-type),
        Num, nqp::decont($source));
}

multi sub nativecast($target-type, $source) is export(:DEFAULT) {
    nqp::nativecallcast(nqp::decont($target-type),
        nqp::decont($target-type), nqp::decont($source));
}

sub nativesizeof($obj) is export(:DEFAULT) {
    nqp::nativecallsizeof($obj)
}

sub cglobal($libname, $symbol, $target-type) is export is rw {
    Proxy.new(
        FETCH => -> $ {
            nqp::nativecallglobal(
                nqp::unbox_s(guess_library_name($libname)),
                nqp::unbox_s($symbol),
                nqp::decont($target-type),
                nqp::decont(map_return_type($target-type)))
        },
        STORE => -> | { die "Writing to C globals NYI" }
    )
}

}

sub check_routine_sanity(Routine $r) is export(:TEST) {
    #Maybe this should use the hash already existing?
    sub validnctype (Mu ::T) {
      return True if nqp::existskey($repr_map,T.REPR) && T.REPR ne 'CArray' | 'CPointer';
      return True if T.^name eq 'Str' | 'str' | 'Bool';
      return False if T.REPR eq 'P6opaque';
      return False if T.HOW.^can("nativesize") && !nqp::defined(T.^nativesize); #to disting int and int32 for example
      return validnctype(T.of) if T.REPR eq 'CArray' | 'CPointer' and T.^can('of');
      return True;
    }
    my $sig = $r.signature;
    for @($sig.params).kv -> $i, $param {
        next if $r ~~ Method and ($i < 1 or $i == $sig.params.elems - 1); #Method have two extra parameters
        if $param.type ~~ Callable {
          # We probably want to check the given routine type too here. but I don't know how
          next;
        }
        next unless $param.type ~~ Buf | Blob #Buf are Uninstantiable, make this buggy
        || $param.type.^can('gist'); #FIXME, it's to handle case of class A { sub foo(A) is native) }, the type is not complete
        if !validnctype($param.type) {
           warn "In '{$r.name}' routine declaration - Not an accepted NativeCall type"
            ~ " for parameter [{$i + 1}] {$param.name ?? $param.name !! ''} : {$param.type.^name}\n"
            ~ " --> For Numerical type, use the appropriate int32/int64/num64...";
        }
    }
    return True if $r.returns.REPR eq 'CPointer' | 'CStruct' | 'CPPStruct'; #Meh fix but 'imcomplete' type are a pain
    if $r.returns.^name ne 'Mu' && !validnctype($r.returns) {
        warn "The returning type of '{$r.name}' --> {$r.returns.^name} is erroneous."
            ~ " You should not return a non NativeCall supported type (like Int inplace of int32),"
            ~ " truncating errors can appear with different architectures";
    }
}

sub EXPORT(|) {
    my @routines_to_setup;
    if $*W {
        my $block := {
            for @routines_to_setup {
                .create-optimized-call;
                CATCH { default { note $_ } }
            }
        };
        $*W.add_object($block);
        my $op := $*W.add_phaser(Mu, 'CHECK', $block, class :: { method cuid { (^2**128).pick }});
    }
    # Specifies that the routine is actually a native call, into the
    # current executable (platform specific) or into a named library
    my $native_trait := multi trait_mod:<is>(Routine $r, :$native!) {
        check_routine_sanity($r);
        $r does NativeCall::Native[$r, $native === True ?? Str !! $native];
        @routines_to_setup.push: $r;
    };
    Map.new(
        '&trait_mod:<is>' => $native_trait.dispatcher,
    );
}

# vim:ft=perl6