-
Notifications
You must be signed in to change notification settings - Fork 10.2k
/
Expr.h
3345 lines (2752 loc) · 109 KB
/
Expr.h
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
//===--- Expr.h - Swift Language Expression ASTs ----------------*- C++ -*-===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2015 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
//
// This file defines the Expr class and subclasses.
//
//===----------------------------------------------------------------------===//
#ifndef SWIFT_AST_EXPR_H
#define SWIFT_AST_EXPR_H
#include "swift/AST/CaptureInfo.h"
#include "swift/AST/ConcreteDeclRef.h"
#include "swift/AST/DeclContext.h"
#include "swift/AST/Identifier.h"
#include "swift/AST/Substitution.h"
#include "swift/AST/TypeLoc.h"
#include "swift/Basic/SourceLoc.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/StringRef.h"
namespace llvm {
struct fltSemantics;
}
namespace swift {
class ArchetypeType;
class ASTContext;
class Type;
class ValueDecl;
class Decl;
class Pattern;
class SubscriptDecl;
class Stmt;
class BraceStmt;
class ASTWalker;
class Initializer;
class VarDecl;
class OpaqueValueExpr;
class ProtocolConformance;
class FuncDecl;
class ConstructorDecl;
class SubstitutableType;
class TypeDecl;
class PatternBindingDecl;
enum class ExprKind : uint8_t {
#define EXPR(Id, Parent) Id,
#define EXPR_RANGE(Id, FirstId, LastId) \
First_##Id##Expr = FirstId, Last_##Id##Expr = LastId,
#include "swift/AST/ExprNodes.def"
};
/// Discriminates the different kinds of checked cast supported.
enum class CheckedCastKind : unsigned {
/// The kind has not been determined yet.
Unresolved,
/// Valid resolved kinds start here.
First_Resolved,
/// The requested cast is an implicit conversion, so this is a coercion.
Coercion = First_Resolved,
/// A cast from a class to one of its subclasses.
Downcast,
/// A cast from a class to a type parameter constrained by that class as a
/// superclass.
SuperToArchetype,
/// A cast from a type parameter to another type parameter.
ArchetypeToArchetype,
/// A cast from a type parameter to a concrete type.
ArchetypeToConcrete,
/// A cast from an existential type to a type parameter.
ExistentialToArchetype,
/// A cast from an existential type to a concrete type.
ExistentialToConcrete,
/// A cast from a concrete type to a type parameter.
ConcreteToArchetype,
/// A cast from a concrete type to an existential type it is not statically
/// known to conform to.
ConcreteToUnrelatedExistential,
// A downcast from an array type to another array type.
ArrayDowncast,
// A downcast from a dictionary type to another dictionary type.
DictionaryDowncast,
// A downcast from a dictionary type to another dictionary type that
// requires bridging.
DictionaryDowncastBridged,
/// A downcast from an object of class or Objective-C existential
/// type to its bridged value type.
BridgeFromObjectiveC,
Last_CheckedCastKind = BridgeFromObjectiveC,
};
/// Expr - Base class for all expressions in swift.
class alignas(8) Expr {
Expr(const Expr&) = delete;
void operator=(const Expr&) = delete;
class ExprBitfields {
friend class Expr;
/// The subclass of Expr that this is.
unsigned Kind : 8;
/// Whether the Expr represents something directly written in source or
/// it was implicitly generated by the type-checker.
unsigned Implicit : 1;
};
enum { NumExprBits = 9 };
static_assert(NumExprBits <= 32, "fits in an unsigned");
class LiteralExprBitfields {
friend class LiteralExpr;
unsigned : NumExprBits;
};
enum { NumLiteralExprBits = NumExprBits + 0 };
static_assert(NumLiteralExprBits <= 32, "fits in an unsigned");
class IntegerLiteralExprBitfields {
friend class IntegerLiteralExpr;
unsigned : NumLiteralExprBits;
unsigned IsNegative : 1;
};
enum { NumIntegerLiteralExprBits = NumLiteralExprBits + 1 };
static_assert(NumIntegerLiteralExprBits <= 32, "fits in an unsigned");
class StringLiteralExprBitfields {
friend class StringLiteralExpr;
unsigned : NumLiteralExprBits;
unsigned Encoding : 2;
unsigned IsSingleUnicodeScalar : 1;
unsigned IsSingleExtendedGraphemeCluster : 1;
};
enum { NumStringLiteralExprBits = NumLiteralExprBits + 2 };
static_assert(NumStringLiteralExprBits <= 32, "fits in an unsigned");
class DeclRefExprBitfields {
friend class DeclRefExpr;
unsigned : NumExprBits;
unsigned IsDirectPropertyAccess : 1;
};
enum { NumDeclRefExprBits = NumExprBits + 1 };
static_assert(NumDeclRefExprBits <= 32, "fits in an unsigned");
class MemberRefExprBitfields {
friend class MemberRefExpr;
unsigned : NumExprBits;
unsigned IsDirectPropertyAccess : 1;
unsigned IsSuper : 1;
};
enum { NumMemberRefExprBits = NumExprBits + 2 };
static_assert(NumMemberRefExprBits <= 32, "fits in an unsigned");
class TupleExprBitfields {
friend class TupleExpr;
unsigned : NumExprBits;
/// Whether this tuple has a trailing closure.
unsigned HasTrailingClosure : 1;
/// Whether this tuple has any labels.
unsigned HasElementNames : 1;
/// Whether this tuple has label locations.
unsigned HasElementNameLocations : 1;
};
enum { NumTupleExprBits = NumExprBits + 3 };
static_assert(NumTupleExprBits <= 32, "fits in an unsigned");
class SubscriptExprBitfields {
friend class SubscriptExpr;
unsigned : NumExprBits;
unsigned IsSuper : 1;
};
enum { NumSubscriptExprBits = NumExprBits + 1 };
static_assert(NumSubscriptExprBits <= 32, "fits in an unsigned");
class BooleanLiteralExprBitfields {
friend class BooleanLiteralExpr;
unsigned : NumLiteralExprBits;
unsigned Value : 1;
};
enum { NumBooleanLiteralExprBits = NumLiteralExprBits + 1 };
static_assert(NumBooleanLiteralExprBits <= 32, "fits in an unsigned");
class MagicIdentifierLiteralExprBitfields {
friend class MagicIdentifierLiteralExpr;
unsigned : NumLiteralExprBits;
unsigned Kind : 2;
unsigned StringEncoding : 1;
};
enum { NumMagicIdentifierLiteralExprBits = NumLiteralExprBits + 3 };
static_assert(NumMagicIdentifierLiteralExprBits <= 32, "fits in an unsigned");
class AbstractClosureExprBitfields {
friend class AbstractClosureExpr;
unsigned : NumExprBits;
unsigned Discriminator : 16;
enum : unsigned {
InvalidDiscriminator = 0xFFFF
};
};
enum { NumAbstractClosureExprBits = NumExprBits + 16 };
static_assert(NumAbstractClosureExprBits <= 32, "fits in an unsigned");
class ClosureExprBitfields {
friend class ClosureExpr;
unsigned : NumAbstractClosureExprBits;
/// True if closure parameters were synthesized from anonymous closure
/// variables.
unsigned HasAnonymousClosureVars : 1;
};
enum { NumClosureExprBits = NumAbstractClosureExprBits + 1 };
static_assert(NumClosureExprBits <= 32, "fits in an unsigned");
class BindOptionalExprBitfields {
friend class BindOptionalExpr;
unsigned : NumExprBits;
unsigned Depth : 16;
};
enum { NumBindOptionalExprBits = NumExprBits + 16 };
static_assert(NumBindOptionalExprBits <= 32, "fits in an unsigned");
enum { NumCheckedCastKindBits = 4 };
class CheckedCastExprBitfields {
friend class CheckedCastExpr;
unsigned : NumExprBits;
unsigned CastKind : NumCheckedCastKindBits;
};
enum { NumCheckedCastExprBits = NumExprBits + 4 };
static_assert(NumCheckedCastExprBits <= 32, "fits in an unsigned");
static_assert(unsigned(CheckedCastKind::Last_CheckedCastKind)
< (1 << NumCheckedCastKindBits),
"unable to fit a CheckedCastKind in the given number of bits");
class CollectionUpcastConversionExprBitfields {
friend class CollectionUpcastConversionExpr;
unsigned : NumExprBits;
unsigned BridgesToObjC : 1;
};
enum { NumCollectionUpcastConversionExprBits = NumExprBits + 1 };
static_assert(NumCollectionUpcastConversionExprBits <= 32, "fits in an unsigned");
protected:
union {
ExprBitfields ExprBits;
LiteralExprBitfields LiteralExprBits;
IntegerLiteralExprBitfields IntegerLiteralExprBits;
StringLiteralExprBitfields StringLiteralExprBits;
DeclRefExprBitfields DeclRefExprBits;
TupleExprBitfields TupleExprBits;
MemberRefExprBitfields MemberRefExprBits;
SubscriptExprBitfields SubscriptExprBits;
BooleanLiteralExprBitfields BooleanLiteralExprBits;
MagicIdentifierLiteralExprBitfields MagicIdentifierLiteralExprBits;
AbstractClosureExprBitfields AbstractClosureExprBits;
ClosureExprBitfields ClosureExprBits;
BindOptionalExprBitfields BindOptionalExprBits;
CheckedCastExprBitfields CheckedCastExprBits;
CollectionUpcastConversionExprBitfields CollectionUpcastConversionExprBits;
};
private:
/// Ty - This is the type of the expression.
Type Ty;
protected:
Expr(ExprKind Kind, bool Implicit, Type Ty = Type()) : Ty(Ty) {
ExprBits.Kind = unsigned(Kind);
ExprBits.Implicit = Implicit;
}
public:
/// Return the kind of this expression.
ExprKind getKind() const { return ExprKind(ExprBits.Kind); }
/// \brief Retrieve the name of the given expression kind.
///
/// This name should only be used for debugging dumps and other
/// developer aids, and should never be part of a diagnostic or exposed
/// to the user of the compiler in any way.
static StringRef getKindName(ExprKind K);
/// getType - Return the type of this expression.
Type getType() const { return Ty; }
/// setType - Sets the type of this expression.
void setType(Type T) { Ty = T; }
/// \brief Return the source range of the expression.
SourceRange getSourceRange() const;
/// getStartLoc - Return the location of the start of the expression.
SourceLoc getStartLoc() const { return getSourceRange().Start; }
/// \brief Retrieve the location of the end of the expression.
SourceLoc getEndLoc() const { return getSourceRange().End; }
/// getLoc - Return the caret location of this expression.
SourceLoc getLoc() const;
SourceLoc TrailingSemiLoc;
/// getSemanticsProvidingExpr - Find the smallest subexpression
/// which obeys the property that evaluating it is exactly
/// equivalent to evaluating this expression.
///
/// Looks through parentheses. Would not look through something
/// like '(foo(), x:bar(), baz()).x'.
Expr *getSemanticsProvidingExpr();
const Expr *getSemanticsProvidingExpr() const {
return const_cast<Expr *>(this)->getSemanticsProvidingExpr();
}
/// getValueProvidingExpr - Find the smallest subexpression which is
/// responsible for generating the value of this expression.
/// Evaluating the result is not necessarily equivalent to
/// evaluating this expression because of potential missing
/// side-effects (which may influence the returned value).
Expr *getValueProvidingExpr();
const Expr *getValueProvidingExpr() const {
return const_cast<Expr *>(this)->getValueProvidingExpr();
}
/// walk - This recursively walks the AST rooted at this expression.
Expr *walk(ASTWalker &walker);
Expr *walk(ASTWalker &&walker) { return walk(walker); }
/// findExistingInitializerContext - Given that this expression is
/// an initializer that belongs in some sort of Initializer
/// context, look through it for any existing context object.
Initializer *findExistingInitializerContext();
/// Determine whether this expression refers to a statically-derived metatype.
///
/// A statically-derived metatype is a metatype value that comes from
/// referring to a named type directly.
bool isStaticallyDerivedMetatype() const;
/// isImplicit - Determines whether this expression was implicitly-generated,
/// rather than explicitly written in the AST.
bool isImplicit() const {
return ExprBits.Implicit;
}
void setImplicit(bool Implicit = true) {
ExprBits.Implicit = Implicit;
}
/// Determine whether this expression is 'super', possibly converted to
/// a base class.
bool isSuperExpr() const;
LLVM_ATTRIBUTE_DEPRECATED(
void dump() const LLVM_ATTRIBUTE_USED,
"only for use within the debugger");
void dump(raw_ostream &OS) const;
void print(raw_ostream &OS, unsigned Indent = 0) const;
void print(ASTPrinter &Printer, const PrintOptions &Opts) const;
// Only allow allocation of Exprs using the allocator in ASTContext
// or by doing a placement new.
void *operator new(size_t Bytes, ASTContext &C,
unsigned Alignment = alignof(Expr));
// Make placement new and vanilla new/delete illegal for Exprs.
void *operator new(size_t Bytes) throw() = delete;
void operator delete(void *Data) throw() = delete;
void *operator new(size_t Bytes, void *Mem) {
assert(Mem);
return Mem;
}
};
/// ErrorExpr - Represents a semantically erroneous subexpression in the AST,
/// typically this will have an ErrorType.
class ErrorExpr : public Expr {
SourceRange Range;
public:
ErrorExpr(SourceRange Range, Type Ty = Type())
: Expr(ExprKind::Error, /*Implicit=*/true, Ty), Range(Range) {}
SourceRange getSourceRange() const { return Range; }
static bool classof(const Expr *E) {
return E->getKind() == ExprKind::Error;
}
};
/// LiteralExpr - Common base class between the literals.
class LiteralExpr : public Expr {
public:
LiteralExpr(ExprKind Kind, bool Implicit) : Expr(Kind, Implicit) {}
static bool classof(const Expr *E) {
return E->getKind() >= ExprKind::First_LiteralExpr &&
E->getKind() <= ExprKind::Last_LiteralExpr;
}
};
/// \brief The 'nil' literal.
///
class NilLiteralExpr : public LiteralExpr {
SourceLoc Loc;
public:
NilLiteralExpr(SourceLoc Loc, bool Implicit = false)
: LiteralExpr(ExprKind::NilLiteral, Implicit), Loc(Loc) {
}
SourceRange getSourceRange() const {
return Loc;
}
static bool classof(const Expr *E) {
return E->getKind() == ExprKind::NilLiteral;
}
};
/// \brief Integer literal with a '+' or '-' sign, like '+4' or '- 2'.
///
/// After semantic analysis assigns types, this is guaranteed to only have
/// a BuiltinIntegerType.
class IntegerLiteralExpr : public LiteralExpr {
/// The value of the literal as an ASTContext-owned string. Underscores must
/// be stripped.
StringRef Val; // Use StringRef instead of APInt, APInt leaks.
SourceLoc MinusLoc;
SourceLoc DigitsLoc;
public:
IntegerLiteralExpr(StringRef Val, SourceLoc DigitsLoc, bool Implicit)
: LiteralExpr(ExprKind::IntegerLiteral, Implicit), Val(Val),
DigitsLoc(DigitsLoc) {
IntegerLiteralExprBits.IsNegative = false;
}
APInt getValue() const;
static APInt getValue(StringRef Text, unsigned BitWidth);
bool isNegative() const { return IntegerLiteralExprBits.IsNegative; }
void setNegative(SourceLoc Loc) {
MinusLoc = Loc;
IntegerLiteralExprBits.IsNegative = true;
}
StringRef getDigitsText() const { return Val; }
SourceRange getSourceRange() const {
if (isNegative())
return { MinusLoc, DigitsLoc };
else
return DigitsLoc;
}
static bool classof(const Expr *E) {
return E->getKind() == ExprKind::IntegerLiteral;
}
};
/// FloatLiteralExpr - Floating point literal, like '4.0'. After semantic
/// analysis assigns types, this is guaranteed to only have a
/// BuiltinFloatingPointType.
class FloatLiteralExpr : public LiteralExpr {
/// The value of the literal as an ASTContext-owned string. Underscores must
/// be stripped.
StringRef Val; // Use StringRef instead of APFloat, APFloat leaks.
SourceLoc Loc;
public:
FloatLiteralExpr(StringRef Val, SourceLoc Loc, bool Implicit)
: LiteralExpr(ExprKind::FloatLiteral, Implicit), Val(Val), Loc(Loc) {}
APFloat getValue() const;
static APFloat getValue(StringRef Text, const llvm::fltSemantics &Semantics);
StringRef getText() const { return Val; }
SourceRange getSourceRange() const { return Loc; }
static bool classof(const Expr *E) {
return E->getKind() == ExprKind::FloatLiteral;
}
};
/// \brief A Boolean literal ('true' or 'false')
///
class BooleanLiteralExpr : public LiteralExpr {
SourceLoc Loc;
public:
BooleanLiteralExpr(bool Value, SourceLoc Loc, bool Implicit = false)
: LiteralExpr(ExprKind::BooleanLiteral, Implicit), Loc(Loc) {
BooleanLiteralExprBits.Value = Value;
}
/// Retrieve the Boolean value of this literal.
bool getValue() const { return BooleanLiteralExprBits.Value; }
SourceRange getSourceRange() const {
return Loc;
}
static bool classof(const Expr *E) {
return E->getKind() == ExprKind::BooleanLiteral;
}
};
/// CharacterLiteral - Character literal, like 'x'. After semantic analysis
/// assigns types, this is guaranteed to only have a 32-bit BuiltinIntegerType.
class CharacterLiteralExpr : public LiteralExpr {
uint32_t Val;
SourceLoc Loc;
public:
CharacterLiteralExpr(uint32_t Val, SourceLoc Loc)
: LiteralExpr(ExprKind::CharacterLiteral, /*Implicit=*/false),
Val(Val), Loc(Loc) {}
uint32_t getValue() const { return Val; }
SourceRange getSourceRange() const { return Loc; }
static bool classof(const Expr *E) {
return E->getKind() == ExprKind::CharacterLiteral;
}
};
/// StringLiteralExpr - String literal, like '"foo"'. After semantic
/// analysis assigns types, this is guaranteed to only have a
/// BuiltinRawPointerType.
class StringLiteralExpr : public LiteralExpr {
StringRef Val;
SourceRange Range;
public:
/// The encoding that should be used for the string literal.
enum Encoding : unsigned {
/// A UTF-8 string.
UTF8,
/// A UTF-16 string.
UTF16,
/// A single UnicodeScalar, passed as an integer.
OneUnicodeScalar
};
StringLiteralExpr(StringRef Val, SourceRange Range);
StringRef getValue() const { return Val; }
SourceRange getSourceRange() const { return Range; }
/// Determine the encoding that should be used for this string literal.
Encoding getEncoding() const {
return static_cast<Encoding>(StringLiteralExprBits.Encoding);
}
/// Set the encoding that should be used for this string literal.
void setEncoding(Encoding encoding) {
StringLiteralExprBits.Encoding = static_cast<unsigned>(encoding);
}
bool isSingleUnicodeScalar() const {
return StringLiteralExprBits.IsSingleUnicodeScalar;
}
bool isSingleExtendedGraphemeCluster() const {
return StringLiteralExprBits.IsSingleExtendedGraphemeCluster;
}
static bool classof(const Expr *E) {
return E->getKind() == ExprKind::StringLiteral;
}
};
/// InterpolatedStringLiteral - An interpolated string literal.
///
/// An interpolated string literal mixes expressions (which are evaluated and
/// converted into string form) within a string literal.
///
/// \code
/// "[\(min)..\(max)]"
/// \endcode
class InterpolatedStringLiteralExpr : public LiteralExpr {
SourceLoc Loc;
MutableArrayRef<Expr *> Segments;
Expr *SemanticExpr;
public:
InterpolatedStringLiteralExpr(SourceLoc Loc, MutableArrayRef<Expr *> Segments)
: LiteralExpr(ExprKind::InterpolatedStringLiteral, /*Implicit=*/false),
Loc(Loc), Segments(Segments), SemanticExpr() { }
MutableArrayRef<Expr *> getSegments() { return Segments; }
ArrayRef<Expr *> getSegments() const { return Segments; }
/// \brief Retrieve the expression that actually evaluates the resulting
/// string, typically with a series of '+' operations.
Expr *getSemanticExpr() const { return SemanticExpr; }
void setSemanticExpr(Expr *SE) { SemanticExpr = SE; }
SourceRange getSourceRange() const { return Loc; }
static bool classof(const Expr *E) {
return E->getKind() == ExprKind::InterpolatedStringLiteral;
}
};
/// MagicIdentifierLiteralExpr - A magic identifier like __FILE__ which expands
/// out to a literal at SILGen time.
class MagicIdentifierLiteralExpr : public LiteralExpr {
public:
enum Kind : unsigned {
File, Line, Column, Function
};
private:
SourceLoc Loc;
public:
MagicIdentifierLiteralExpr(Kind kind, SourceLoc loc, bool implicit)
: LiteralExpr(ExprKind::MagicIdentifierLiteral, implicit), Loc(loc)
{
MagicIdentifierLiteralExprBits.Kind = static_cast<unsigned>(kind);
MagicIdentifierLiteralExprBits.StringEncoding
= static_cast<unsigned>(StringLiteralExpr::UTF8);
}
Kind getKind() const {
return static_cast<Kind>(MagicIdentifierLiteralExprBits.Kind);
}
bool isFile() const { return getKind() == File; }
bool isFunction() const { return getKind() == Function; }
bool isLine() const { return getKind() == Line; }
bool isColumn() const { return getKind() == Column; }
bool isString() const {
switch (getKind()) {
case File:
case Function:
return true;
case Line:
case Column:
return false;
}
}
SourceRange getSourceRange() const { return Loc; }
// For a magic identifier that produces a string literal, retrieve the
// encoding for that string literal.
StringLiteralExpr::Encoding getStringEncoding() const {
assert(isString() && "Magic identifier literal has non-string encoding");
return static_cast<StringLiteralExpr::Encoding>(
MagicIdentifierLiteralExprBits.StringEncoding);
}
// For a magic identifier that produces a string literal, set the encoding
// for the string literal.
void setStringEncoding(StringLiteralExpr::Encoding encoding) {
assert(isString() && "Magic identifier literal has non-string encoding");
MagicIdentifierLiteralExprBits.StringEncoding
= static_cast<unsigned>(encoding);
}
static bool classof(const Expr *E) {
return E->getKind() == ExprKind::MagicIdentifierLiteral;
}
};
/// DiscardAssignmentExpr - A '_' in the left-hand side of an assignment, which
/// discards the corresponding tuple element on the right-hand side.
class DiscardAssignmentExpr : public Expr {
SourceLoc Loc;
public:
DiscardAssignmentExpr(SourceLoc Loc, bool Implicit)
: Expr(ExprKind::DiscardAssignment, Implicit), Loc(Loc) {}
SourceRange getSourceRange() const { return Loc; }
SourceLoc getLoc() const { return Loc; }
static bool classof(const Expr *E) {
return E->getKind() == ExprKind::DiscardAssignment;
}
};
/// DeclRefExpr - A reference to a value, "x".
class DeclRefExpr : public Expr {
/// This is used when the reference is specialized, e.g "GenCls<Int>", to
/// hold information about the generic arguments.
struct SpecializeInfo {
ConcreteDeclRef D;
ArrayRef<TypeRepr*> GenericArgs;
};
/// \brief The declaration pointer or SpecializeInfo pointer if it was
/// explicitly specialized with <...>.
llvm::PointerUnion<ConcreteDeclRef, SpecializeInfo *> DOrSpecialized;
SourceLoc Loc;
SpecializeInfo *getSpecInfo() const {
return DOrSpecialized.dyn_cast<SpecializeInfo*>();
}
public:
DeclRefExpr(ConcreteDeclRef D, SourceLoc Loc, bool Implicit,
// If True, access to computed properties with storage goes to
// the storage, instead of through the accessors.
bool UsesDirectPropertyAccess = false, Type Ty = Type())
: Expr(ExprKind::DeclRef, Implicit, Ty), DOrSpecialized(D), Loc(Loc) {
DeclRefExprBits.IsDirectPropertyAccess = UsesDirectPropertyAccess;
}
/// Retrieve the declaration to which this expression refers.
ValueDecl *getDecl() const {
return getDeclRef().getDecl();
}
/// Return true if this access is direct, meaning that it does not call the
/// getter or setter.
bool isDirectPropertyAccess() const {
return DeclRefExprBits.IsDirectPropertyAccess;
}
/// Retrieve the concrete declaration reference.
ConcreteDeclRef getDeclRef() const {
if (auto Spec = getSpecInfo())
return Spec->D;
return DOrSpecialized.get<ConcreteDeclRef>();
}
/// Set the declaration.
void setDeclRef(ConcreteDeclRef ref);
void setSpecialized();
/// \brief Determine whether this declaration reference was immediately
/// specialized by <...>.
bool isSpecialized() const { return getSpecInfo() != nullptr; }
/// Set the generic arguments.
///
/// This copies the array using ASTContext's allocator.
void setGenericArgs(ArrayRef<TypeRepr*> GenericArgs);
/// Returns the generic arguments if it was specialized or an empty array
/// otherwise.
ArrayRef<TypeRepr *> getGenericArgs() const {
if (auto Spec = getSpecInfo())
return Spec->GenericArgs;
return ArrayRef<TypeRepr *>();
}
SourceRange getSourceRange() const { return Loc; }
static bool classof(const Expr *E) {
return E->getKind() == ExprKind::DeclRef;
}
};
/// A reference to 'super'. References to members of 'super' resolve to members
/// of a superclass of 'self'.
class SuperRefExpr : public Expr {
VarDecl *Self;
SourceLoc Loc;
public:
SuperRefExpr(VarDecl *Self, SourceLoc Loc, bool Implicit,
Type SuperTy = Type())
: Expr(ExprKind::SuperRef, Implicit, SuperTy), Self(Self), Loc(Loc) {}
VarDecl *getSelf() const { return Self; }
SourceLoc getSuperLoc() const { return Loc; }
SourceRange getSourceRange() const { return Loc; }
static bool classof(const Expr *E) {
return E->getKind() == ExprKind::SuperRef;
}
};
/// A reference to a type in expression context, spelled out as a TypeLoc. Sema
/// forms this expression as a result of name binding. This always has
/// MetaTypetype.
class TypeExpr : public Expr {
TypeLoc Info;
TypeExpr(Type Ty);
public:
// Create a TypeExpr with location information.
TypeExpr(TypeLoc Ty);
// Create an implicit TypeExpr, which has no location information.
static TypeExpr *createImplicit(Type Ty, ASTContext &C) {
return new (C) TypeExpr(Ty);
}
// Create an implicit TypeExpr, with location information even though it
// shouldn't have one. This is presently used to work around other location
// processing bugs. If you have an implicit location, use createImplicit.
static TypeExpr *createImplicitHack(SourceLoc Loc, Type Ty, ASTContext &C);
/// Return a TypeExpr for a TypeDecl and the specified location.
static TypeExpr *createForDecl(SourceLoc Loc, TypeDecl *D);
static TypeExpr *createForSpecializedDecl(SourceLoc Loc, TypeDecl *D,
ArrayRef<TypeRepr*> args,
SourceRange angleLocs);
TypeLoc &getTypeLoc() { return Info; }
TypeLoc getTypeLoc() const { return Info; }
TypeRepr *getTypeRepr() const { return Info.getTypeRepr(); }
// NOTE: TypeExpr::getType() returns the type of the expr node, which is the
// metatype of what is stored as an operand type.
SourceRange getSourceRange() const { return Info.getSourceRange(); }
static bool classof(const Expr *E) {
return E->getKind() == ExprKind::Type;
}
};
/// A reference to another initializer from within a constructor body,
/// either to a delegating initializer or to a super.init invocation.
/// For a reference type, this semantically references a different constructor
/// entry point, called the 'initializing constructor', from the 'allocating
/// constructor' entry point referenced by a 'new' expression.
class OtherConstructorDeclRefExpr : public Expr {
ConcreteDeclRef Ctor;
SourceLoc Loc;
public:
OtherConstructorDeclRefExpr(ConcreteDeclRef Ctor, SourceLoc Loc,
bool Implicit, Type Ty = {})
: Expr(ExprKind::OtherConstructorDeclRef, Implicit, Ty),
Ctor(Ctor), Loc(Loc)
{}
ConstructorDecl *getDecl() const;
ConcreteDeclRef getDeclRef() const { return Ctor; }
SourceLoc getConstructorLoc() const { return Loc; }
SourceRange getSourceRange() const { return Loc; }
static bool classof(const Expr *E) {
return E->getKind() == ExprKind::OtherConstructorDeclRef;
}
};
/// An unresolved reference to a constructor member of a value. Resolves to a
/// DotSyntaxCall involving the value and the resolved constructor.
class UnresolvedConstructorExpr : public Expr {
Expr *SubExpr;
SourceLoc DotLoc;
SourceLoc ConstructorLoc;
public:
UnresolvedConstructorExpr(Expr *SubExpr, SourceLoc DotLoc,
SourceLoc ConstructorLoc, bool Implicit)
: Expr(ExprKind::UnresolvedConstructor, Implicit),
SubExpr(SubExpr), DotLoc(DotLoc), ConstructorLoc(ConstructorLoc)
{}
Expr *getSubExpr() const { return SubExpr; }
void setSubExpr(Expr *e) { SubExpr = e; }
SourceLoc getLoc() const { return ConstructorLoc; }
SourceLoc getConstructorLoc() const { return ConstructorLoc; }
SourceLoc getDotLoc() const { return DotLoc; }
SourceRange getSourceRange() const {
return SourceRange(SubExpr->getStartLoc(), ConstructorLoc);
}
static bool classof(const Expr *E) {
return E->getKind() == ExprKind::UnresolvedConstructor;
}
};
/// OverloadSetRefExpr - A reference to an overloaded set of values with a
/// single name.
///
/// This is an abstract class that covers the various different kinds of
/// overload sets.
class OverloadSetRefExpr : public Expr {
ArrayRef<ValueDecl*> Decls;
protected:
OverloadSetRefExpr(ExprKind Kind, ArrayRef<ValueDecl*> decls, bool Implicit,
Type Ty)
: Expr(Kind, Implicit, Ty), Decls(decls) {}
public:
ArrayRef<ValueDecl*> getDecls() const { return Decls; }
/// getBaseType - Determine the type of the base object provided for the
/// given overload set, which is only non-null when dealing with an overloaded
/// member reference.
Type getBaseType() const;
/// hasBaseObject - Determine whether this overloaded expression has a
/// concrete base object (which is not a metatype).
bool hasBaseObject() const;
static bool classof(const Expr *E) {
return E->getKind() >= ExprKind::First_OverloadSetRefExpr &&
E->getKind() <= ExprKind::Last_OverloadSetRefExpr;
}
};
/// OverloadedDeclRefExpr - A reference to an overloaded name that should
/// eventually be resolved (by overload resolution) to a value reference.
class OverloadedDeclRefExpr : public OverloadSetRefExpr {
SourceLoc Loc;
bool IsSpecialized = false;
bool IsPotentiallyDelayedGlobalOperator = false;
public:
OverloadedDeclRefExpr(ArrayRef<ValueDecl*> Decls, SourceLoc Loc,
bool Implicit, Type Ty = Type())
: OverloadSetRefExpr(ExprKind::OverloadedDeclRef, Decls, Implicit, Ty),
Loc(Loc) { }
SourceLoc getLoc() const { return Loc; }
SourceRange getSourceRange() const { return Loc; }
void setSpecialized(bool specialized) { IsSpecialized = specialized; }
/// \brief Determine whether this declaration reference was immediately
/// specialized by <...>.
bool isSpecialized() const { return IsSpecialized; }
void setIsPotentiallyDelayedGlobalOperator() {
IsPotentiallyDelayedGlobalOperator = true;
}
bool isPotentiallyDelayedGlobalOperator() const {
return IsPotentiallyDelayedGlobalOperator;
}
static bool classof(const Expr *E) {
return E->getKind() == ExprKind::OverloadedDeclRef;
}
};
/// OverloadedMemberRefExpr - A reference to an overloaded name that is a
/// member, relative to some base expression, that will eventually be
/// resolved to some kind of member-reference expression.
class OverloadedMemberRefExpr : public OverloadSetRefExpr {
Expr *SubExpr;
SourceLoc DotLoc;
SourceLoc MemberLoc;
public:
OverloadedMemberRefExpr(Expr *SubExpr, SourceLoc DotLoc,
ArrayRef<ValueDecl *> Decls, SourceLoc MemberLoc,
bool Implicit, Type Ty = Type())
: OverloadSetRefExpr(ExprKind::OverloadedMemberRef, Decls, Implicit, Ty),
SubExpr(SubExpr), DotLoc(DotLoc), MemberLoc(MemberLoc) { }
SourceLoc getDotLoc() const { return DotLoc; }
SourceLoc getMemberLoc() const { return MemberLoc; }
Expr *getBase() const { return SubExpr; }
void setBase(Expr *E) { SubExpr = E; }
SourceLoc getLoc() const { return MemberLoc; }
SourceLoc getStartLoc() const {
return DotLoc.isValid()? SubExpr->getStartLoc() : MemberLoc;
}
SourceLoc getEndLoc() const { return MemberLoc; }
SourceRange getSourceRange() const {
return SourceRange(getStartLoc(), MemberLoc);
}
static bool classof(const Expr *E) {
return E->getKind() == ExprKind::OverloadedMemberRef;
}
};
/// UnresolvedDeclRefExpr - This represents use of an undeclared identifier,
/// which may ultimately be a use of something that hasn't been defined yet, it
/// may be a use of something that got imported (which will be resolved during
/// sema), or may just be a use of an unknown identifier.
///
class UnresolvedDeclRefExpr : public Expr {