forked from jckarter/clay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
clay.hpp
2727 lines (2225 loc) · 71.7 KB
/
clay.hpp
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
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#ifndef __CLAY_HPP
#define __CLAY_HPP
#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#define _SCL_SECURE_NO_WARNINGS
#endif
#include <string>
#include <vector>
#include <exception>
#include <map>
#include <set>
#include <iomanip>
#include <cassert>
#include <cctype>
#include <cstdio>
#include <cstdlib>
#include <cstdarg>
#include <climits>
#include <cerrno>
#ifdef _MSC_VER
// LLVM headers spew warnings on MSVC
#pragma warning(push)
#pragma warning(disable: 4146 4244 4267 4355 4146 4800 4996)
#endif
#include <llvm/ADT/FoldingSet.h>
#include <llvm/ADT/SmallString.h>
#include <llvm/ADT/SmallVector.h>
#include <llvm/ADT/StringMap.h>
#include <llvm/ADT/StringRef.h>
#include <llvm/ADT/Triple.h>
#include <llvm/Analysis/Passes.h>
#include <llvm/Assembly/Writer.h>
#include <llvm/Assembly/Parser.h>
#include <llvm/Assembly/PrintModulePass.h>
#include <llvm/BasicBlock.h>
#include <llvm/Bitcode/ReaderWriter.h>
#include <llvm/CodeGen/LinkAllAsmWriterComponents.h>
#include <llvm/CodeGen/LinkAllCodegenComponents.h>
#include <llvm/CodeGen/ValueTypes.h>
#include <llvm/DataLayout.h>
#include <llvm/DebugInfo.h>
#include <llvm/DerivedTypes.h>
#include <llvm/DIBuilder.h>
#include <llvm/ExecutionEngine/ExecutionEngine.h>
#include <llvm/ExecutionEngine/GenericValue.h>
#include <llvm/ExecutionEngine/JIT.h>
#include <llvm/Function.h>
#include <llvm/Intrinsics.h>
#include <llvm/IRBuilder.h>
#include <llvm/LinkAllVMCore.h>
#include <llvm/Linker.h>
#include <llvm/LLVMContext.h>
#include <llvm/Module.h>
#include <llvm/PassManager.h>
#include <llvm/Support/Allocator.h>
#include <llvm/Support/Dwarf.h>
#include <llvm/Support/FileSystem.h>
#include <llvm/Support/FormattedStream.h>
#include <llvm/Support/Host.h>
#include <llvm/Support/MemoryBuffer.h>
#include <llvm/Support/Path.h>
#include <llvm/Support/PathV2.h>
#include <llvm/Support/SourceMgr.h>
#include <llvm/Support/TargetRegistry.h>
#include <llvm/Support/TargetSelect.h>
#include <llvm/Support/TargetSelect.h>
#include <llvm/Support/raw_ostream.h>
#include <llvm/Target/TargetOptions.h>
#include <llvm/Transforms/IPO/PassManagerBuilder.h>
#include <llvm/Transforms/IPO.h>
#include <llvm/Transforms/Scalar.h>
#include <llvm/Transforms/Vectorize.h>
#include <llvm/Type.h>
#ifdef _MSC_VER
#pragma warning(pop)
#endif
#include "refcounted.hpp"
namespace clay {
template<bool Cond> struct StaticAssertChecker;
template<> struct StaticAssertChecker<true> { typedef int type; };
template<> struct StaticAssertChecker<false> { };
#define _CLAY_CAT(a,b) a##b
#define CLAY_CAT(a,b) _CLAY_CAT(a,b)
#define CLAY_STATIC_ASSERT(cond) \
static const ::clay::StaticAssertChecker<(cond)>::type CLAY_CAT(static_assert_, __LINE__);
using std::string;
using std::vector;
using std::pair;
using std::make_pair;
using std::map;
using std::set;
}
#ifdef _MSC_VER
#define strtoll _strtoi64
#define strtoull _strtoui64
#define strtold strtod
#define snprintf _snprintf
#define CLAY_ALIGN(n) __declspec(align(n))
#include <complex>
namespace clay {
typedef std::complex<float> clay_cfloat;
typedef std::complex<double> clay_cdouble;
typedef std::complex<long double> clay_cldouble;
template<typename T>
inline T clay_creal(std::complex<T> const &c) { return std::real(c); }
template<typename T>
inline T clay_cimag(std::complex<T> const &c) { return std::imag(c); }
}
#else
#define CLAY_ALIGN(n) __attribute__((aligned(n)))
namespace clay {
typedef _Complex float clay_cfloat;
typedef _Complex double clay_cdouble;
typedef _Complex long double clay_cldouble;
inline float clay_creal(_Complex float c) { return __real__ c; }
inline double clay_creal(_Complex double c) { return __real__ c; }
inline long double clay_creal(_Complex long double c) { return __real__ c; }
inline float clay_cimag(_Complex float c) { return __imag__ c; }
inline double clay_cimag(_Complex double c) { return __imag__ c; }
inline long double clay_cimag(_Complex long double c) { return __imag__ c; }
}
#endif
#define CLAY_LANGUAGE_VERSION "0.2-WIP"
#define CLAY_COMPILER_VERSION "0.2git"
namespace clay {
//
// container typedefs
//
typedef llvm::SmallString<260> PathString;
//
// Target-specific types
//
typedef int ptrdiff32_t;
typedef long long ptrdiff64_t;
typedef unsigned size32_t;
typedef unsigned long long size64_t;
//
// int128 type
//
#if defined(__GNUC__) && defined(_INT128_DEFINED)
typedef __int128 clay_int128;
typedef unsigned __int128 clay_uint128;
//#elif (defined(__clang__))
//typedef __int128_t clay_int128;
//typedef __uint128_t clay_uint128;
#else
// fake it by doing 64-bit math in a 128-bit padded container
struct uint128_holder;
struct int128_holder {
ptrdiff64_t lowValue;
ptrdiff64_t highPad; // not used in static math
int128_holder() {}
explicit int128_holder(ptrdiff64_t low) : lowValue(low), highPad(low < 0 ? -1 : 0) {}
int128_holder(ptrdiff64_t low, ptrdiff64_t high)
: lowValue(low), highPad(high) {}
explicit int128_holder(uint128_holder y);
int128_holder &operator=(ptrdiff64_t low) {
new ((void*)this) int128_holder(low);
return *this;
}
int128_holder operator-() const { return int128_holder(-lowValue); }
int128_holder operator~() const { return int128_holder(~lowValue); }
bool operator==(int128_holder y) const { return lowValue == y.lowValue; }
bool operator< (int128_holder y) const { return lowValue < y.lowValue; }
int128_holder operator+ (int128_holder y) const { return int128_holder(lowValue + y.lowValue); }
int128_holder operator- (int128_holder y) const { return int128_holder(lowValue - y.lowValue); }
int128_holder operator* (int128_holder y) const { return int128_holder(lowValue * y.lowValue); }
int128_holder operator/ (int128_holder y) const { return int128_holder(lowValue / y.lowValue); }
int128_holder operator% (int128_holder y) const { return int128_holder(lowValue % y.lowValue); }
int128_holder operator<<(int128_holder y) const { return int128_holder(lowValue << y.lowValue); }
int128_holder operator>>(int128_holder y) const { return int128_holder(lowValue >> y.lowValue); }
int128_holder operator& (int128_holder y) const { return int128_holder(lowValue & y.lowValue); }
int128_holder operator| (int128_holder y) const { return int128_holder(lowValue | y.lowValue); }
int128_holder operator^ (int128_holder y) const { return int128_holder(lowValue ^ y.lowValue); }
operator ptrdiff64_t() const { return lowValue; }
} CLAY_ALIGN(16);
struct uint128_holder {
size64_t lowValue;
size64_t highPad; // not used in static math
uint128_holder() {}
explicit uint128_holder(size64_t low) : lowValue(low), highPad(0) {}
uint128_holder(size64_t low, size64_t high) : lowValue(low), highPad(high) {}
explicit uint128_holder(int128_holder y) : lowValue((size64_t)y.lowValue), highPad((size64_t)y.highPad) {}
uint128_holder &operator=(size64_t low) {
new ((void*)this) uint128_holder(low);
return *this;
}
uint128_holder operator-() const { return uint128_holder((size64_t)(-(ptrdiff64_t)lowValue)); }
uint128_holder operator~() const { return uint128_holder(~lowValue); }
bool operator==(uint128_holder y) const { return lowValue == y.lowValue; }
bool operator< (uint128_holder y) const { return lowValue < y.lowValue; }
uint128_holder operator+ (uint128_holder y) const { return uint128_holder(lowValue + y.lowValue); }
uint128_holder operator- (uint128_holder y) const { return uint128_holder(lowValue - y.lowValue); }
uint128_holder operator* (uint128_holder y) const { return uint128_holder(lowValue * y.lowValue); }
uint128_holder operator/ (uint128_holder y) const { return uint128_holder(lowValue / y.lowValue); }
uint128_holder operator% (uint128_holder y) const { return uint128_holder(lowValue % y.lowValue); }
uint128_holder operator<<(uint128_holder y) const { return uint128_holder(lowValue << y.lowValue); }
uint128_holder operator>>(uint128_holder y) const { return uint128_holder(lowValue >> y.lowValue); }
uint128_holder operator& (uint128_holder y) const { return uint128_holder(lowValue & y.lowValue); }
uint128_holder operator| (uint128_holder y) const { return uint128_holder(lowValue | y.lowValue); }
uint128_holder operator^ (uint128_holder y) const { return uint128_holder(lowValue ^ y.lowValue); }
operator size64_t() const { return lowValue; }
} CLAY_ALIGN(16);
}
namespace std {
template<>
class numeric_limits<clay::int128_holder> {
public:
static clay::int128_holder min() throw() {
return clay::int128_holder(0, std::numeric_limits<clay::ptrdiff64_t>::min());
}
static clay::int128_holder max() throw() {
return clay::int128_holder(-1, std::numeric_limits<clay::ptrdiff64_t>::max());
}
};
template<>
class numeric_limits<clay::uint128_holder> {
public:
static clay::uint128_holder min() throw() {
return clay::uint128_holder(0, 0);
}
static clay::uint128_holder max() throw() {
return clay::uint128_holder(
std::numeric_limits<clay::size64_t>::max(),
std::numeric_limits<clay::size64_t>::max()
);
}
};
}
namespace clay {
inline int128_holder::int128_holder(uint128_holder y)
: lowValue((ptrdiff64_t)y.lowValue), highPad((ptrdiff64_t)y.highPad) {}
typedef int128_holder clay_int128;
typedef uint128_holder clay_uint128;
#endif
inline llvm::raw_ostream &operator<<(llvm::raw_ostream &os, const clay_int128 &x) {
return os << ptrdiff64_t(x);
}
inline llvm::raw_ostream &operator<<(llvm::raw_ostream &os, const clay_uint128 &x) {
return os << size64_t(x);
}
//
// ObjectKind
//
enum ObjectKind {
SOURCE,
LOCATION,
IDENTIFIER,
DOTTED_NAME,
EXPRESSION,
EXPR_LIST,
STATEMENT,
CASE_BLOCK,
CATCH,
FORMAL_ARG,
RETURN_SPEC,
LLVM_CODE,
CODE,
RECORD_DECL,
RECORD_BODY,
RECORD_FIELD,
VARIANT_DECL,
INSTANCE_DECL,
NEW_TYPE_DECL,
OVERLOAD,
PROCEDURE,
INTRINSIC,
ENUM_DECL,
ENUM_MEMBER,
GLOBAL_VARIABLE,
EXTERNAL_PROCEDURE,
EXTERNAL_ARG,
EXTERNAL_VARIABLE,
EVAL_TOPLEVEL,
GLOBAL_ALIAS,
IMPORT,
MODULE_DECLARATION,
MODULE,
ENV,
PRIM_OP,
TYPE,
PATTERN,
MULTI_PATTERN,
VALUE_HOLDER,
MULTI_STATIC,
PVALUE,
MULTI_PVALUE,
EVALUE,
MULTI_EVALUE,
CVALUE,
MULTI_CVALUE,
DOCUMENTATION,
STATIC_ASSERT_TOP_LEVEL,
};
//
// Object
//
struct Object : public RefCounted {
ObjectKind objKind;
Object(ObjectKind objKind)
: objKind(objKind) {}
virtual ~Object() {}
};
typedef Pointer<Object> ObjectPtr;
//
// forwards
//
struct Source;
struct ANode;
struct Identifier;
struct DottedName;
struct Expr;
struct BoolLiteral;
struct IntLiteral;
struct FloatLiteral;
struct CharLiteral;
struct StringLiteral;
struct NameRef;
struct FILEExpr;
struct LINEExpr;
struct COLUMNExpr;
struct ARGExpr;
struct Tuple;
struct Paren;
struct Indexing;
struct Call;
struct FieldRef;
struct StaticIndexing;
struct VariadicOp;
struct And;
struct Or;
struct Lambda;
struct Unpack;
struct StaticExpr;
struct DispatchExpr;
struct ForeignExpr;
struct ObjectExpr;
struct EvalExpr;
struct ExprList;
struct Statement;
struct Block;
struct Label;
struct Binding;
struct Assignment;
struct InitAssignment;
struct VariadicAssignment;
struct Goto;
struct Switch;
struct CaseBlock;
struct Return;
struct If;
struct ExprStatement;
struct While;
struct Break;
struct Continue;
struct For;
struct ForeignStatement;
struct Try;
struct Catch;
struct Throw;
struct StaticFor;
struct Finally;
struct OnError;
struct Unreachable;
struct EvalStatement;
struct StaticAssertStatement;
struct FormalArg;
struct ReturnSpec;
struct LLVMCode;
struct Code;
struct TopLevelItem;
struct RecordDecl;
struct RecordBody;
struct RecordField;
struct VariantDecl;
struct InstanceDecl;
struct NewTypeDecl;
struct Overload;
struct Procedure;
struct IntrinsicSymbol;
struct EnumDecl;
struct EnumMember;
struct GlobalVariable;
struct GVarInstance;
struct ExternalProcedure;
struct ExternalArg;
struct ExternalVariable;
struct EvalTopLevel;
struct StaticAssertTopLevel;
struct Documentation;
struct GlobalAlias;
struct Import;
struct ImportModule;
struct ImportStar;
struct ImportMembers;
struct Module;
struct ModuleDeclaration;
struct Env;
struct PrimOp;
struct Type;
struct BoolType;
struct IntegerType;
struct FloatType;
struct ComplexType;
struct ArrayType;
struct VecType;
struct TupleType;
struct UnionType;
struct PointerType;
struct CodePointerType;
struct CCodePointerType;
struct RecordType;
struct VariantType;
struct StaticType;
struct EnumType;
struct NewType;
struct Pattern;
struct PatternCell;
struct PatternStruct;
struct MultiPattern;
struct MultiPatternCell;
struct MultiPatternList;
struct ValueHolder;
struct MultiStatic;
struct PValue;
struct MultiPValue;
struct EValue;
struct MultiEValue;
struct CValue;
struct MultiCValue;
struct ObjectTable;
struct ValueStackEntry;
struct MatchResult;
struct MatchFailureError;
//
// Pointer typedefs
//
typedef Pointer<Source> SourcePtr;
typedef Pointer<ANode> ANodePtr;
typedef Pointer<Identifier> IdentifierPtr;
typedef Pointer<DottedName> DottedNamePtr;
typedef Pointer<Expr> ExprPtr;
typedef Pointer<BoolLiteral> BoolLiteralPtr;
typedef Pointer<IntLiteral> IntLiteralPtr;
typedef Pointer<FloatLiteral> FloatLiteralPtr;
typedef Pointer<CharLiteral> CharLiteralPtr;
typedef Pointer<StringLiteral> StringLiteralPtr;
typedef Pointer<NameRef> NameRefPtr;
typedef Pointer<FILEExpr> FILEExprPtr;
typedef Pointer<LINEExpr> LINEExprPtr;
typedef Pointer<COLUMNExpr> COLUMNExprPtr;
typedef Pointer<ARGExpr> ARGExprPtr;
typedef Pointer<Tuple> TuplePtr;
typedef Pointer<Paren> ParenPtr;
typedef Pointer<Indexing> IndexingPtr;
typedef Pointer<Call> CallPtr;
typedef Pointer<FieldRef> FieldRefPtr;
typedef Pointer<StaticIndexing> StaticIndexingPtr;
typedef Pointer<VariadicOp> VariadicOpPtr;
typedef Pointer<And> AndPtr;
typedef Pointer<Or> OrPtr;
typedef Pointer<Lambda> LambdaPtr;
typedef Pointer<Unpack> UnpackPtr;
typedef Pointer<StaticExpr> StaticExprPtr;
typedef Pointer<DispatchExpr> DispatchExprPtr;
typedef Pointer<ForeignExpr> ForeignExprPtr;
typedef Pointer<ObjectExpr> ObjectExprPtr;
typedef Pointer<EvalExpr> EvalExprPtr;
typedef Pointer<ExprList> ExprListPtr;
typedef Pointer<Statement> StatementPtr;
typedef Pointer<Block> BlockPtr;
typedef Pointer<Label> LabelPtr;
typedef Pointer<Binding> BindingPtr;
typedef Pointer<Assignment> AssignmentPtr;
typedef Pointer<InitAssignment> InitAssignmentPtr;
typedef Pointer<VariadicAssignment> VariadicAssignmentPtr;
typedef Pointer<Goto> GotoPtr;
typedef Pointer<Switch> SwitchPtr;
typedef Pointer<CaseBlock> CaseBlockPtr;
typedef Pointer<Return> ReturnPtr;
typedef Pointer<If> IfPtr;
typedef Pointer<ExprStatement> ExprStatementPtr;
typedef Pointer<While> WhilePtr;
typedef Pointer<Break> BreakPtr;
typedef Pointer<Continue> ContinuePtr;
typedef Pointer<For> ForPtr;
typedef Pointer<ForeignStatement> ForeignStatementPtr;
typedef Pointer<Try> TryPtr;
typedef Pointer<Catch> CatchPtr;
typedef Pointer<Throw> ThrowPtr;
typedef Pointer<StaticFor> StaticForPtr;
typedef Pointer<Finally> FinallyPtr;
typedef Pointer<OnError> OnErrorPtr;
typedef Pointer<Unreachable> UnreachablePtr;
typedef Pointer<EvalStatement> EvalStatementPtr;
typedef Pointer<StaticAssertStatement> StaticAssertStatementPtr;
typedef Pointer<FormalArg> FormalArgPtr;
typedef Pointer<ReturnSpec> ReturnSpecPtr;
typedef Pointer<LLVMCode> LLVMCodePtr;
typedef Pointer<Code> CodePtr;
typedef Pointer<TopLevelItem> TopLevelItemPtr;
typedef Pointer<RecordDecl> RecordDeclPtr;
typedef Pointer<RecordBody> RecordBodyPtr;
typedef Pointer<RecordField> RecordFieldPtr;
typedef Pointer<VariantDecl> VariantDeclPtr;
typedef Pointer<InstanceDecl> InstanceDeclPtr;
typedef Pointer<NewTypeDecl> NewTypeDeclPtr;
typedef Pointer<Overload> OverloadPtr;
typedef Pointer<Procedure> ProcedurePtr;
typedef Pointer<IntrinsicSymbol> IntrinsicPtr;
typedef Pointer<EnumDecl> EnumDeclPtr;
typedef Pointer<EnumMember> EnumMemberPtr;
typedef Pointer<GlobalVariable> GlobalVariablePtr;
typedef Pointer<GVarInstance> GVarInstancePtr;
typedef Pointer<ExternalProcedure> ExternalProcedurePtr;
typedef Pointer<ExternalArg> ExternalArgPtr;
typedef Pointer<ExternalVariable> ExternalVariablePtr;
typedef Pointer<EvalTopLevel> EvalTopLevelPtr;
typedef Pointer<StaticAssertTopLevel> StaticAssertTopLevelPtr;
typedef Pointer<Documentation> DocumentationPtr;
typedef Pointer<GlobalAlias> GlobalAliasPtr;
typedef Pointer<Import> ImportPtr;
typedef Pointer<ImportModule> ImportModulePtr;
typedef Pointer<ImportStar> ImportStarPtr;
typedef Pointer<ImportMembers> ImportMembersPtr;
typedef Pointer<Module> ModulePtr;
typedef Pointer<ModuleDeclaration> ModuleDeclarationPtr;
typedef Pointer<Env> EnvPtr;
typedef Pointer<PrimOp> PrimOpPtr;
typedef Pointer<Type> TypePtr;
typedef Pointer<BoolType> BoolTypePtr;
typedef Pointer<IntegerType> IntegerTypePtr;
typedef Pointer<FloatType> FloatTypePtr;
typedef Pointer<ComplexType> ComplexTypePtr;
typedef Pointer<ArrayType> ArrayTypePtr;
typedef Pointer<VecType> VecTypePtr;
typedef Pointer<TupleType> TupleTypePtr;
typedef Pointer<UnionType> UnionTypePtr;
typedef Pointer<PointerType> PointerTypePtr;
typedef Pointer<CodePointerType> CodePointerTypePtr;
typedef Pointer<CCodePointerType> CCodePointerTypePtr;
typedef Pointer<RecordType> RecordTypePtr;
typedef Pointer<VariantType> VariantTypePtr;
typedef Pointer<StaticType> StaticTypePtr;
typedef Pointer<EnumType> EnumTypePtr;
typedef Pointer<NewType> NewTypePtr;
typedef Pointer<Pattern> PatternPtr;
typedef Pointer<PatternCell> PatternCellPtr;
typedef Pointer<PatternStruct> PatternStructPtr;
typedef Pointer<MultiPattern> MultiPatternPtr;
typedef Pointer<MultiPatternCell> MultiPatternCellPtr;
typedef Pointer<MultiPatternList> MultiPatternListPtr;
typedef Pointer<ValueHolder> ValueHolderPtr;
typedef Pointer<MultiStatic> MultiStaticPtr;
typedef Pointer<PValue> PValuePtr;
typedef Pointer<MultiPValue> MultiPValuePtr;
typedef Pointer<EValue> EValuePtr;
typedef Pointer<MultiEValue> MultiEValuePtr;
typedef Pointer<CValue> CValuePtr;
typedef Pointer<MultiCValue> MultiCValuePtr;
typedef Pointer<ObjectTable> ObjectTablePtr;
typedef Pointer<MatchResult> MatchResultPtr;
//
// Source, Location
//
struct Location;
struct Source : public Object {
string fileName;
llvm::OwningPtr<llvm::MemoryBuffer> buffer;
llvm::TrackingVH<llvm::MDNode> debugInfo;
Source(llvm::StringRef fileName);
Source(llvm::StringRef lineOfCode, int dummy);
Source(llvm::StringRef fileName, llvm::MemoryBuffer *buffer)
: Object(SOURCE), fileName(fileName), buffer(buffer), debugInfo(NULL)
{ }
const char *data() const { return buffer->getBufferStart(); }
const char *endData() const { return buffer->getBufferEnd(); }
size_t size() const { return buffer->getBufferSize(); }
llvm::DIFile getDebugInfo() const { return llvm::DIFile(debugInfo); }
};
struct Location {
SourcePtr source;
unsigned offset;
Location()
: source(NULL), offset(0) {}
Location(const SourcePtr &source, unsigned offset)
: source(source), offset(offset) {}
bool ok() const { return source != NULL; }
};
//
// error module
//
struct CompileContextEntry {
vector<ObjectPtr> params;
vector<unsigned> dispatchIndices;
Location location;
ObjectPtr callable;
bool hasParams:1;
CompileContextEntry(ObjectPtr callable)
: callable(callable), hasParams(false) {}
CompileContextEntry(ObjectPtr callable, llvm::ArrayRef<ObjectPtr> params)
: params(params), callable(callable), hasParams(true) {}
CompileContextEntry(ObjectPtr callable,
llvm::ArrayRef<ObjectPtr> params,
llvm::ArrayRef<unsigned> dispatchIndices)
: params(params), dispatchIndices(dispatchIndices), callable(callable), hasParams(true) {}
};
void pushCompileContext(ObjectPtr obj);
void pushCompileContext(ObjectPtr obj, llvm::ArrayRef<ObjectPtr> params);
void pushCompileContext(ObjectPtr obj,
llvm::ArrayRef<ObjectPtr> params,
llvm::ArrayRef<unsigned> dispatchIndices);
void popCompileContext();
vector<CompileContextEntry> getCompileContext();
void setCompileContext(llvm::ArrayRef<CompileContextEntry> x);
struct CompileContextPusher {
CompileContextPusher(ObjectPtr obj) {
pushCompileContext(obj);
}
CompileContextPusher(ObjectPtr obj, llvm::ArrayRef<ObjectPtr> params) {
pushCompileContext(obj, params);
}
CompileContextPusher(ObjectPtr obj, llvm::ArrayRef<TypePtr> params) {
vector<ObjectPtr> params2;
for (unsigned i = 0; i < params.size(); ++i)
params2.push_back((Object *)(params[i].ptr()));
pushCompileContext(obj, params2);
}
CompileContextPusher(ObjectPtr obj,
llvm::ArrayRef<TypePtr> params,
llvm::ArrayRef<unsigned> dispatchIndices) {
vector<ObjectPtr> params2;
for (unsigned i = 0; i < params.size(); ++i)
params2.push_back((Object *)(params[i].ptr()));
pushCompileContext(obj, params2, dispatchIndices);
}
~CompileContextPusher() {
popCompileContext();
}
};
void pushLocation(Location const &location);
void popLocation();
Location topLocation();
struct LocationContext {
Location loc;
LocationContext(Location const &loc)
: loc(loc) {
if (loc.ok()) pushLocation(loc);
}
~LocationContext() {
if (loc.ok())
popLocation();
}
private :
LocationContext(const LocationContext &) {}
void operator=(const LocationContext &) {}
};
void getLineCol(Location const &location,
unsigned &line,
unsigned &column,
unsigned &tabColumn);
llvm::DIFile getDebugLineCol(Location const &location, unsigned &line, unsigned &column);
void printFileLineCol(llvm::raw_ostream &out, Location const &location);
void invalidStaticObjectError(ObjectPtr obj);
void argumentInvalidStaticObjectError(unsigned index, ObjectPtr obj);
struct DebugPrinter {
static int indent;
ObjectPtr obj;
DebugPrinter(ObjectPtr obj);
~DebugPrinter();
};
extern "C" void displayCompileContext();
//
// AST
//
static llvm::BumpPtrAllocator *ANodeAllocator = new llvm::BumpPtrAllocator();
struct ANode : public Object {
Location location;
ANode(ObjectKind objKind)
: Object(objKind) {}
void *operator new(size_t num_bytes) {
return ANodeAllocator->Allocate(num_bytes, llvm::AlignOf<ANode>::Alignment);
}
void operator delete(void* anode) {
ANodeAllocator->Deallocate(anode);
}
};
struct Identifier : public ANode {
const llvm::SmallString<16> str;
bool isOperator:1;
Identifier(llvm::StringRef str)
: ANode(IDENTIFIER), str(str), isOperator(false) {}
Identifier(llvm::StringRef str, bool isOperator)
: ANode(IDENTIFIER), str(str), isOperator(isOperator) {}
static map<llvm::StringRef, IdentifierPtr> freeIdentifiers; // in parser.cpp
static Identifier *get(llvm::StringRef str, bool isOperator = false) {
map<llvm::StringRef, IdentifierPtr>::const_iterator iter = freeIdentifiers.find(str);
if (iter == freeIdentifiers.end()) {
Identifier *ident = new Identifier(str, isOperator);
freeIdentifiers[ident->str] = ident;
return ident;
} else
return iter->second.ptr();
}
static Identifier *get(llvm::StringRef str, Location const &location, bool isOperator = false) {
Identifier *ident = new Identifier(str, isOperator);
ident->location = location;
return ident;
}
};
struct DottedName : public ANode {
llvm::SmallVector<IdentifierPtr, 2> parts;
DottedName()
: ANode(DOTTED_NAME) {}
DottedName(llvm::ArrayRef<IdentifierPtr> parts)
: ANode(DOTTED_NAME), parts(parts.begin(), parts.end()) {}
string join() const;
};
//
// Expr
//
enum ExprKind {
BOOL_LITERAL,
INT_LITERAL,
FLOAT_LITERAL,
CHAR_LITERAL,
STRING_LITERAL,
FILE_EXPR,
LINE_EXPR,
COLUMN_EXPR,
ARG_EXPR,
NAME_REF,
TUPLE,
PAREN,
INDEXING,
CALL,
FIELD_REF,
STATIC_INDEXING,
VARIADIC_OP,
AND,
OR,
LAMBDA,
UNPACK,
STATIC_EXPR,
DISPATCH_EXPR,
FOREIGN_EXPR,
OBJECT_EXPR,
EVAL_EXPR
};
struct Expr : public ANode {
ExprKind exprKind;
Location startLocation;
Location endLocation;
MultiPValuePtr cachedAnalysis;
Expr(ExprKind exprKind)
: ANode(EXPRESSION), exprKind(exprKind) {}
string asString() {
if (!startLocation.ok() || !endLocation.ok())
return "<generated expression>";
assert(startLocation.source == endLocation.source);
const char *data = startLocation.source->data();
return string(data + startLocation.offset, data + endLocation.offset);
}
};
struct BoolLiteral : public Expr {
bool value;
BoolLiteral(bool value)
: Expr(BOOL_LITERAL), value(value) {}
};
struct IntLiteral : public Expr {
string value;
string suffix;
IntLiteral(llvm::StringRef value)
: Expr(INT_LITERAL), value(value) {}
IntLiteral(llvm::StringRef value, llvm::StringRef suffix)
: Expr(INT_LITERAL), value(value), suffix(suffix) {}
};
struct FloatLiteral : public Expr {
string value;
string suffix;
FloatLiteral(llvm::StringRef value)
: Expr(FLOAT_LITERAL), value(value) {}
FloatLiteral(llvm::StringRef value, llvm::StringRef suffix)
: Expr(FLOAT_LITERAL), value(value), suffix(suffix) {}
};
struct CharLiteral : public Expr {
ExprPtr desugared;
char value;
CharLiteral(char value)
: Expr(CHAR_LITERAL), value(value) {}
};
struct StringLiteral : public Expr {
IdentifierPtr value;
StringLiteral(IdentifierPtr value)
: Expr(STRING_LITERAL), value(value) {}
};
struct NameRef : public Expr {
IdentifierPtr name;
NameRef(IdentifierPtr name)
: Expr(NAME_REF), name(name) {}
};
struct FILEExpr : public Expr {
FILEExpr()
: Expr(FILE_EXPR) {}