1 //===- ExprObjC.h - Classes for representing ObjC expressions ---*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file defines the ExprObjC interface and subclasses.
11 //===----------------------------------------------------------------------===//
13 #ifndef LLVM_CLANG_AST_EXPROBJC_H
14 #define LLVM_CLANG_AST_EXPROBJC_H
16 #include "clang/AST/Decl.h"
17 #include "clang/AST/DeclObjC.h"
18 #include "clang/AST/Expr.h"
19 #include "clang/AST/OperationKinds.h"
20 #include "clang/AST/SelectorLocationsKind.h"
21 #include "clang/AST/Stmt.h"
22 #include "clang/AST/Type.h"
23 #include "clang/Basic/IdentifierTable.h"
24 #include "clang/Basic/LLVM.h"
25 #include "clang/Basic/SourceLocation.h"
26 #include "clang/Basic/Specifiers.h"
27 #include "llvm/ADT/ArrayRef.h"
28 #include "llvm/ADT/None.h"
29 #include "llvm/ADT/Optional.h"
30 #include "llvm/ADT/PointerIntPair.h"
31 #include "llvm/ADT/PointerUnion.h"
32 #include "llvm/ADT/StringRef.h"
33 #include "llvm/ADT/iterator_range.h"
34 #include "llvm/Support/Casting.h"
35 #include "llvm/Support/Compiler.h"
36 #include "llvm/Support/TrailingObjects.h"
37 #include "llvm/Support/VersionTuple.h"
38 #include "llvm/Support/type_traits.h"
46 class CXXBaseSpecifier;
48 /// ObjCStringLiteral, used for Objective-C string literals
50 class ObjCStringLiteral : public Expr {
55 ObjCStringLiteral(StringLiteral *SL, QualType T, SourceLocation L)
56 : Expr(ObjCStringLiteralClass, T, VK_RValue, OK_Ordinary, false, false,
58 String(SL), AtLoc(L) {}
59 explicit ObjCStringLiteral(EmptyShell Empty)
60 : Expr(ObjCStringLiteralClass, Empty) {}
62 StringLiteral *getString() { return cast<StringLiteral>(String); }
63 const StringLiteral *getString() const { return cast<StringLiteral>(String); }
64 void setString(StringLiteral *S) { String = S; }
66 SourceLocation getAtLoc() const { return AtLoc; }
67 void setAtLoc(SourceLocation L) { AtLoc = L; }
69 SourceLocation getBeginLoc() const LLVM_READONLY { return AtLoc; }
70 SourceLocation getEndLoc() const LLVM_READONLY { return String->getEndLoc(); }
73 child_range children() { return child_range(&String, &String+1); }
75 const_child_range children() const {
76 return const_child_range(&String, &String + 1);
79 static bool classof(const Stmt *T) {
80 return T->getStmtClass() == ObjCStringLiteralClass;
84 /// ObjCBoolLiteralExpr - Objective-C Boolean Literal.
85 class ObjCBoolLiteralExpr : public Expr {
90 ObjCBoolLiteralExpr(bool val, QualType Ty, SourceLocation l)
91 : Expr(ObjCBoolLiteralExprClass, Ty, VK_RValue, OK_Ordinary, false, false,
94 explicit ObjCBoolLiteralExpr(EmptyShell Empty)
95 : Expr(ObjCBoolLiteralExprClass, Empty) {}
97 bool getValue() const { return Value; }
98 void setValue(bool V) { Value = V; }
100 SourceLocation getBeginLoc() const LLVM_READONLY { return Loc; }
101 SourceLocation getEndLoc() const LLVM_READONLY { return Loc; }
103 SourceLocation getLocation() const { return Loc; }
104 void setLocation(SourceLocation L) { Loc = L; }
107 child_range children() {
108 return child_range(child_iterator(), child_iterator());
111 const_child_range children() const {
112 return const_child_range(const_child_iterator(), const_child_iterator());
115 static bool classof(const Stmt *T) {
116 return T->getStmtClass() == ObjCBoolLiteralExprClass;
120 /// ObjCBoxedExpr - used for generalized expression boxing.
121 /// as in: @(strdup("hello world")), @(random()) or @(view.frame)
122 /// Also used for boxing non-parenthesized numeric literals;
123 /// as in: @42 or \@true (c++/objc++) or \@__objc_yes (c/objc).
124 class ObjCBoxedExpr : public Expr {
126 ObjCMethodDecl *BoxingMethod;
130 friend class ASTStmtReader;
132 ObjCBoxedExpr(Expr *E, QualType T, ObjCMethodDecl *method,
134 : Expr(ObjCBoxedExprClass, T, VK_RValue, OK_Ordinary,
135 E->isTypeDependent(), E->isValueDependent(),
136 E->isInstantiationDependent(),
137 E->containsUnexpandedParameterPack()),
138 SubExpr(E), BoxingMethod(method), Range(R) {}
139 explicit ObjCBoxedExpr(EmptyShell Empty)
140 : Expr(ObjCBoxedExprClass, Empty) {}
142 Expr *getSubExpr() { return cast<Expr>(SubExpr); }
143 const Expr *getSubExpr() const { return cast<Expr>(SubExpr); }
145 ObjCMethodDecl *getBoxingMethod() const {
149 // Indicates whether this boxed expression can be emitted as a compile-time
151 bool isExpressibleAsConstantInitializer() const {
152 return !BoxingMethod && SubExpr;
155 SourceLocation getAtLoc() const { return Range.getBegin(); }
157 SourceLocation getBeginLoc() const LLVM_READONLY { return Range.getBegin(); }
158 SourceLocation getEndLoc() const LLVM_READONLY { return Range.getEnd(); }
160 SourceRange getSourceRange() const LLVM_READONLY {
165 child_range children() { return child_range(&SubExpr, &SubExpr+1); }
167 const_child_range children() const {
168 return const_child_range(&SubExpr, &SubExpr + 1);
171 using const_arg_iterator = ConstExprIterator;
173 const_arg_iterator arg_begin() const {
174 return reinterpret_cast<Stmt const * const*>(&SubExpr);
177 const_arg_iterator arg_end() const {
178 return reinterpret_cast<Stmt const * const*>(&SubExpr + 1);
181 static bool classof(const Stmt *T) {
182 return T->getStmtClass() == ObjCBoxedExprClass;
186 /// ObjCArrayLiteral - used for objective-c array containers; as in:
187 /// @[@"Hello", NSApp, [NSNumber numberWithInt:42]];
188 class ObjCArrayLiteral final
190 private llvm::TrailingObjects<ObjCArrayLiteral, Expr *> {
191 unsigned NumElements;
193 ObjCMethodDecl *ArrayWithObjectsMethod;
195 ObjCArrayLiteral(ArrayRef<Expr *> Elements,
196 QualType T, ObjCMethodDecl * Method,
199 explicit ObjCArrayLiteral(EmptyShell Empty, unsigned NumElements)
200 : Expr(ObjCArrayLiteralClass, Empty), NumElements(NumElements) {}
203 friend class ASTStmtReader;
204 friend TrailingObjects;
206 static ObjCArrayLiteral *Create(const ASTContext &C,
207 ArrayRef<Expr *> Elements,
208 QualType T, ObjCMethodDecl * Method,
211 static ObjCArrayLiteral *CreateEmpty(const ASTContext &C,
212 unsigned NumElements);
214 SourceLocation getBeginLoc() const LLVM_READONLY { return Range.getBegin(); }
215 SourceLocation getEndLoc() const LLVM_READONLY { return Range.getEnd(); }
216 SourceRange getSourceRange() const LLVM_READONLY { return Range; }
218 /// Retrieve elements of array of literals.
219 Expr **getElements() { return getTrailingObjects<Expr *>(); }
221 /// Retrieve elements of array of literals.
222 const Expr * const *getElements() const {
223 return getTrailingObjects<Expr *>();
226 /// getNumElements - Return number of elements of objective-c array literal.
227 unsigned getNumElements() const { return NumElements; }
229 /// getElement - Return the Element at the specified index.
230 Expr *getElement(unsigned Index) {
231 assert((Index < NumElements) && "Arg access out of range!");
232 return getElements()[Index];
234 const Expr *getElement(unsigned Index) const {
235 assert((Index < NumElements) && "Arg access out of range!");
236 return getElements()[Index];
239 ObjCMethodDecl *getArrayWithObjectsMethod() const {
240 return ArrayWithObjectsMethod;
244 child_range children() {
245 return child_range(reinterpret_cast<Stmt **>(getElements()),
246 reinterpret_cast<Stmt **>(getElements()) + NumElements);
249 const_child_range children() const {
250 auto Children = const_cast<ObjCArrayLiteral *>(this)->children();
251 return const_child_range(Children.begin(), Children.end());
254 static bool classof(const Stmt *T) {
255 return T->getStmtClass() == ObjCArrayLiteralClass;
259 /// An element in an Objective-C dictionary literal.
261 struct ObjCDictionaryElement {
262 /// The key for the dictionary element.
265 /// The value of the dictionary element.
268 /// The location of the ellipsis, if this is a pack expansion.
269 SourceLocation EllipsisLoc;
271 /// The number of elements this pack expansion will expand to, if
272 /// this is a pack expansion and is known.
273 Optional<unsigned> NumExpansions;
275 /// Determines whether this dictionary element is a pack expansion.
276 bool isPackExpansion() const { return EllipsisLoc.isValid(); }
283 /// Internal struct for storing Key/value pair.
284 struct ObjCDictionaryLiteral_KeyValuePair {
289 /// Internal struct to describes an element that is a pack
290 /// expansion, used if any of the elements in the dictionary literal
291 /// are pack expansions.
292 struct ObjCDictionaryLiteral_ExpansionData {
293 /// The location of the ellipsis, if this element is a pack
295 SourceLocation EllipsisLoc;
297 /// If non-zero, the number of elements that this pack
298 /// expansion will expand to (+1).
299 unsigned NumExpansionsPlusOne;
302 /// ObjCDictionaryLiteral - AST node to represent objective-c dictionary
303 /// literals; as in: @{@"name" : NSUserName(), @"date" : [NSDate date] };
304 class ObjCDictionaryLiteral final
306 private llvm::TrailingObjects<ObjCDictionaryLiteral,
307 ObjCDictionaryLiteral_KeyValuePair,
308 ObjCDictionaryLiteral_ExpansionData> {
309 /// The number of elements in this dictionary literal.
310 unsigned NumElements : 31;
312 /// Determine whether this dictionary literal has any pack expansions.
314 /// If the dictionary literal has pack expansions, then there will
315 /// be an array of pack expansion data following the array of
316 /// key/value pairs, which provide the locations of the ellipses (if
317 /// any) and number of elements in the expansion (if known). If
318 /// there are no pack expansions, we optimize away this storage.
319 unsigned HasPackExpansions : 1;
322 ObjCMethodDecl *DictWithObjectsMethod;
324 using KeyValuePair = ObjCDictionaryLiteral_KeyValuePair;
325 using ExpansionData = ObjCDictionaryLiteral_ExpansionData;
327 ObjCDictionaryLiteral(ArrayRef<ObjCDictionaryElement> VK,
328 bool HasPackExpansions,
329 QualType T, ObjCMethodDecl *method,
332 explicit ObjCDictionaryLiteral(EmptyShell Empty, unsigned NumElements,
333 bool HasPackExpansions)
334 : Expr(ObjCDictionaryLiteralClass, Empty), NumElements(NumElements),
335 HasPackExpansions(HasPackExpansions) {}
337 size_t numTrailingObjects(OverloadToken<KeyValuePair>) const {
342 friend class ASTStmtReader;
343 friend class ASTStmtWriter;
344 friend TrailingObjects;
346 static ObjCDictionaryLiteral *Create(const ASTContext &C,
347 ArrayRef<ObjCDictionaryElement> VK,
348 bool HasPackExpansions,
349 QualType T, ObjCMethodDecl *method,
352 static ObjCDictionaryLiteral *CreateEmpty(const ASTContext &C,
353 unsigned NumElements,
354 bool HasPackExpansions);
356 /// getNumElements - Return number of elements of objective-c dictionary
358 unsigned getNumElements() const { return NumElements; }
360 ObjCDictionaryElement getKeyValueElement(unsigned Index) const {
361 assert((Index < NumElements) && "Arg access out of range!");
362 const KeyValuePair &KV = getTrailingObjects<KeyValuePair>()[Index];
363 ObjCDictionaryElement Result = { KV.Key, KV.Value, SourceLocation(), None };
364 if (HasPackExpansions) {
365 const ExpansionData &Expansion =
366 getTrailingObjects<ExpansionData>()[Index];
367 Result.EllipsisLoc = Expansion.EllipsisLoc;
368 if (Expansion.NumExpansionsPlusOne > 0)
369 Result.NumExpansions = Expansion.NumExpansionsPlusOne - 1;
374 ObjCMethodDecl *getDictWithObjectsMethod() const {
375 return DictWithObjectsMethod;
378 SourceLocation getBeginLoc() const LLVM_READONLY { return Range.getBegin(); }
379 SourceLocation getEndLoc() const LLVM_READONLY { return Range.getEnd(); }
380 SourceRange getSourceRange() const LLVM_READONLY { return Range; }
383 child_range children() {
384 // Note: we're taking advantage of the layout of the KeyValuePair struct
385 // here. If that struct changes, this code will need to change as well.
386 static_assert(sizeof(KeyValuePair) == sizeof(Stmt *) * 2,
387 "KeyValuePair is expected size");
389 reinterpret_cast<Stmt **>(getTrailingObjects<KeyValuePair>()),
390 reinterpret_cast<Stmt **>(getTrailingObjects<KeyValuePair>()) +
394 const_child_range children() const {
395 auto Children = const_cast<ObjCDictionaryLiteral *>(this)->children();
396 return const_child_range(Children.begin(), Children.end());
399 static bool classof(const Stmt *T) {
400 return T->getStmtClass() == ObjCDictionaryLiteralClass;
404 /// ObjCEncodeExpr, used for \@encode in Objective-C. \@encode has the same
405 /// type and behavior as StringLiteral except that the string initializer is
406 /// obtained from ASTContext with the encoding type as an argument.
407 class ObjCEncodeExpr : public Expr {
408 TypeSourceInfo *EncodedType;
409 SourceLocation AtLoc, RParenLoc;
412 ObjCEncodeExpr(QualType T, TypeSourceInfo *EncodedType,
413 SourceLocation at, SourceLocation rp)
414 : Expr(ObjCEncodeExprClass, T, VK_LValue, OK_Ordinary,
415 EncodedType->getType()->isDependentType(),
416 EncodedType->getType()->isDependentType(),
417 EncodedType->getType()->isInstantiationDependentType(),
418 EncodedType->getType()->containsUnexpandedParameterPack()),
419 EncodedType(EncodedType), AtLoc(at), RParenLoc(rp) {}
421 explicit ObjCEncodeExpr(EmptyShell Empty) : Expr(ObjCEncodeExprClass, Empty){}
423 SourceLocation getAtLoc() const { return AtLoc; }
424 void setAtLoc(SourceLocation L) { AtLoc = L; }
425 SourceLocation getRParenLoc() const { return RParenLoc; }
426 void setRParenLoc(SourceLocation L) { RParenLoc = L; }
428 QualType getEncodedType() const { return EncodedType->getType(); }
430 TypeSourceInfo *getEncodedTypeSourceInfo() const { return EncodedType; }
432 void setEncodedTypeSourceInfo(TypeSourceInfo *EncType) {
433 EncodedType = EncType;
436 SourceLocation getBeginLoc() const LLVM_READONLY { return AtLoc; }
437 SourceLocation getEndLoc() const LLVM_READONLY { return RParenLoc; }
440 child_range children() {
441 return child_range(child_iterator(), child_iterator());
444 const_child_range children() const {
445 return const_child_range(const_child_iterator(), const_child_iterator());
448 static bool classof(const Stmt *T) {
449 return T->getStmtClass() == ObjCEncodeExprClass;
453 /// ObjCSelectorExpr used for \@selector in Objective-C.
454 class ObjCSelectorExpr : public Expr {
456 SourceLocation AtLoc, RParenLoc;
459 ObjCSelectorExpr(QualType T, Selector selInfo,
460 SourceLocation at, SourceLocation rp)
461 : Expr(ObjCSelectorExprClass, T, VK_RValue, OK_Ordinary, false, false,
463 SelName(selInfo), AtLoc(at), RParenLoc(rp) {}
464 explicit ObjCSelectorExpr(EmptyShell Empty)
465 : Expr(ObjCSelectorExprClass, Empty) {}
467 Selector getSelector() const { return SelName; }
468 void setSelector(Selector S) { SelName = S; }
470 SourceLocation getAtLoc() const { return AtLoc; }
471 SourceLocation getRParenLoc() const { return RParenLoc; }
472 void setAtLoc(SourceLocation L) { AtLoc = L; }
473 void setRParenLoc(SourceLocation L) { RParenLoc = L; }
475 SourceLocation getBeginLoc() const LLVM_READONLY { return AtLoc; }
476 SourceLocation getEndLoc() const LLVM_READONLY { return RParenLoc; }
478 /// getNumArgs - Return the number of actual arguments to this call.
479 unsigned getNumArgs() const { return SelName.getNumArgs(); }
482 child_range children() {
483 return child_range(child_iterator(), child_iterator());
486 const_child_range children() const {
487 return const_child_range(const_child_iterator(), const_child_iterator());
490 static bool classof(const Stmt *T) {
491 return T->getStmtClass() == ObjCSelectorExprClass;
495 /// ObjCProtocolExpr used for protocol expression in Objective-C.
497 /// This is used as: \@protocol(foo), as in:
499 /// [obj conformsToProtocol:@protocol(foo)]
502 /// The return type is "Protocol*".
503 class ObjCProtocolExpr : public Expr {
504 ObjCProtocolDecl *TheProtocol;
505 SourceLocation AtLoc, ProtoLoc, RParenLoc;
508 friend class ASTStmtReader;
509 friend class ASTStmtWriter;
511 ObjCProtocolExpr(QualType T, ObjCProtocolDecl *protocol,
512 SourceLocation at, SourceLocation protoLoc, SourceLocation rp)
513 : Expr(ObjCProtocolExprClass, T, VK_RValue, OK_Ordinary, false, false,
515 TheProtocol(protocol), AtLoc(at), ProtoLoc(protoLoc), RParenLoc(rp) {}
516 explicit ObjCProtocolExpr(EmptyShell Empty)
517 : Expr(ObjCProtocolExprClass, Empty) {}
519 ObjCProtocolDecl *getProtocol() const { return TheProtocol; }
520 void setProtocol(ObjCProtocolDecl *P) { TheProtocol = P; }
522 SourceLocation getProtocolIdLoc() const { return ProtoLoc; }
523 SourceLocation getAtLoc() const { return AtLoc; }
524 SourceLocation getRParenLoc() const { return RParenLoc; }
525 void setAtLoc(SourceLocation L) { AtLoc = L; }
526 void setRParenLoc(SourceLocation L) { RParenLoc = L; }
528 SourceLocation getBeginLoc() const LLVM_READONLY { return AtLoc; }
529 SourceLocation getEndLoc() const LLVM_READONLY { return RParenLoc; }
532 child_range children() {
533 return child_range(child_iterator(), child_iterator());
536 const_child_range children() const {
537 return const_child_range(const_child_iterator(), const_child_iterator());
540 static bool classof(const Stmt *T) {
541 return T->getStmtClass() == ObjCProtocolExprClass;
545 /// ObjCIvarRefExpr - A reference to an ObjC instance variable.
546 class ObjCIvarRefExpr : public Expr {
551 /// OpLoc - This is the location of '.' or '->'
552 SourceLocation OpLoc;
554 // True if this is "X->F", false if this is "X.F".
557 // True if ivar reference has no base (self assumed).
561 ObjCIvarRefExpr(ObjCIvarDecl *d, QualType t,
562 SourceLocation l, SourceLocation oploc,
564 bool arrow = false, bool freeIvar = false)
565 : Expr(ObjCIvarRefExprClass, t, VK_LValue,
566 d->isBitField() ? OK_BitField : OK_Ordinary,
567 /*TypeDependent=*/false, base->isValueDependent(),
568 base->isInstantiationDependent(),
569 base->containsUnexpandedParameterPack()),
570 D(d), Base(base), Loc(l), OpLoc(oploc), IsArrow(arrow),
571 IsFreeIvar(freeIvar) {}
573 explicit ObjCIvarRefExpr(EmptyShell Empty)
574 : Expr(ObjCIvarRefExprClass, Empty) {}
576 ObjCIvarDecl *getDecl() { return D; }
577 const ObjCIvarDecl *getDecl() const { return D; }
578 void setDecl(ObjCIvarDecl *d) { D = d; }
580 const Expr *getBase() const { return cast<Expr>(Base); }
581 Expr *getBase() { return cast<Expr>(Base); }
582 void setBase(Expr * base) { Base = base; }
584 bool isArrow() const { return IsArrow; }
585 bool isFreeIvar() const { return IsFreeIvar; }
586 void setIsArrow(bool A) { IsArrow = A; }
587 void setIsFreeIvar(bool A) { IsFreeIvar = A; }
589 SourceLocation getLocation() const { return Loc; }
590 void setLocation(SourceLocation L) { Loc = L; }
592 SourceLocation getBeginLoc() const LLVM_READONLY {
593 return isFreeIvar() ? Loc : getBase()->getBeginLoc();
595 SourceLocation getEndLoc() const LLVM_READONLY { return Loc; }
597 SourceLocation getOpLoc() const { return OpLoc; }
598 void setOpLoc(SourceLocation L) { OpLoc = L; }
601 child_range children() { return child_range(&Base, &Base+1); }
603 const_child_range children() const {
604 return const_child_range(&Base, &Base + 1);
607 static bool classof(const Stmt *T) {
608 return T->getStmtClass() == ObjCIvarRefExprClass;
612 /// ObjCPropertyRefExpr - A dot-syntax expression to access an ObjC
614 class ObjCPropertyRefExpr : public Expr {
616 /// If the bool is true, this is an implicit property reference; the
617 /// pointer is an (optional) ObjCMethodDecl and Setter may be set.
618 /// if the bool is false, this is an explicit property reference;
619 /// the pointer is an ObjCPropertyDecl and Setter is always null.
620 llvm::PointerIntPair<NamedDecl *, 1, bool> PropertyOrGetter;
622 /// Indicates whether the property reference will result in a message
623 /// to the getter, the setter, or both.
624 /// This applies to both implicit and explicit property references.
625 enum MethodRefFlags {
627 MethodRef_Getter = 0x1,
628 MethodRef_Setter = 0x2
631 /// Contains the Setter method pointer and MethodRefFlags bit flags.
632 llvm::PointerIntPair<ObjCMethodDecl *, 2, unsigned> SetterAndMethodRefFlags;
634 // FIXME: Maybe we should store the property identifier here,
635 // because it's not rederivable from the other data when there's an
636 // implicit property with no getter (because the 'foo' -> 'setFoo:'
637 // transformation is lossy on the first character).
639 SourceLocation IdLoc;
641 /// When the receiver in property access is 'super', this is
642 /// the location of the 'super' keyword. When it's an interface,
643 /// this is that interface.
644 SourceLocation ReceiverLoc;
645 llvm::PointerUnion3<Stmt *, const Type *, ObjCInterfaceDecl *> Receiver;
648 ObjCPropertyRefExpr(ObjCPropertyDecl *PD, QualType t,
649 ExprValueKind VK, ExprObjectKind OK,
650 SourceLocation l, Expr *base)
651 : Expr(ObjCPropertyRefExprClass, t, VK, OK,
652 /*TypeDependent=*/false, base->isValueDependent(),
653 base->isInstantiationDependent(),
654 base->containsUnexpandedParameterPack()),
655 PropertyOrGetter(PD, false), IdLoc(l), Receiver(base) {
656 assert(t->isSpecificPlaceholderType(BuiltinType::PseudoObject));
659 ObjCPropertyRefExpr(ObjCPropertyDecl *PD, QualType t,
660 ExprValueKind VK, ExprObjectKind OK,
661 SourceLocation l, SourceLocation sl, QualType st)
662 : Expr(ObjCPropertyRefExprClass, t, VK, OK,
663 /*TypeDependent=*/false, false, st->isInstantiationDependentType(),
664 st->containsUnexpandedParameterPack()),
665 PropertyOrGetter(PD, false), IdLoc(l), ReceiverLoc(sl),
666 Receiver(st.getTypePtr()) {
667 assert(t->isSpecificPlaceholderType(BuiltinType::PseudoObject));
670 ObjCPropertyRefExpr(ObjCMethodDecl *Getter, ObjCMethodDecl *Setter,
671 QualType T, ExprValueKind VK, ExprObjectKind OK,
672 SourceLocation IdLoc, Expr *Base)
673 : Expr(ObjCPropertyRefExprClass, T, VK, OK, false,
674 Base->isValueDependent(), Base->isInstantiationDependent(),
675 Base->containsUnexpandedParameterPack()),
676 PropertyOrGetter(Getter, true), SetterAndMethodRefFlags(Setter, 0),
677 IdLoc(IdLoc), Receiver(Base) {
678 assert(T->isSpecificPlaceholderType(BuiltinType::PseudoObject));
681 ObjCPropertyRefExpr(ObjCMethodDecl *Getter, ObjCMethodDecl *Setter,
682 QualType T, ExprValueKind VK, ExprObjectKind OK,
683 SourceLocation IdLoc,
684 SourceLocation SuperLoc, QualType SuperTy)
685 : Expr(ObjCPropertyRefExprClass, T, VK, OK, false, false, false, false),
686 PropertyOrGetter(Getter, true), SetterAndMethodRefFlags(Setter, 0),
687 IdLoc(IdLoc), ReceiverLoc(SuperLoc), Receiver(SuperTy.getTypePtr()) {
688 assert(T->isSpecificPlaceholderType(BuiltinType::PseudoObject));
691 ObjCPropertyRefExpr(ObjCMethodDecl *Getter, ObjCMethodDecl *Setter,
692 QualType T, ExprValueKind VK, ExprObjectKind OK,
693 SourceLocation IdLoc,
694 SourceLocation ReceiverLoc, ObjCInterfaceDecl *Receiver)
695 : Expr(ObjCPropertyRefExprClass, T, VK, OK, false, false, false, false),
696 PropertyOrGetter(Getter, true), SetterAndMethodRefFlags(Setter, 0),
697 IdLoc(IdLoc), ReceiverLoc(ReceiverLoc), Receiver(Receiver) {
698 assert(T->isSpecificPlaceholderType(BuiltinType::PseudoObject));
701 explicit ObjCPropertyRefExpr(EmptyShell Empty)
702 : Expr(ObjCPropertyRefExprClass, Empty) {}
704 bool isImplicitProperty() const { return PropertyOrGetter.getInt(); }
705 bool isExplicitProperty() const { return !PropertyOrGetter.getInt(); }
707 ObjCPropertyDecl *getExplicitProperty() const {
708 assert(!isImplicitProperty());
709 return cast<ObjCPropertyDecl>(PropertyOrGetter.getPointer());
712 ObjCMethodDecl *getImplicitPropertyGetter() const {
713 assert(isImplicitProperty());
714 return cast_or_null<ObjCMethodDecl>(PropertyOrGetter.getPointer());
717 ObjCMethodDecl *getImplicitPropertySetter() const {
718 assert(isImplicitProperty());
719 return SetterAndMethodRefFlags.getPointer();
722 Selector getGetterSelector() const {
723 if (isImplicitProperty())
724 return getImplicitPropertyGetter()->getSelector();
725 return getExplicitProperty()->getGetterName();
728 Selector getSetterSelector() const {
729 if (isImplicitProperty())
730 return getImplicitPropertySetter()->getSelector();
731 return getExplicitProperty()->getSetterName();
734 /// True if the property reference will result in a message to the
736 /// This applies to both implicit and explicit property references.
737 bool isMessagingGetter() const {
738 return SetterAndMethodRefFlags.getInt() & MethodRef_Getter;
741 /// True if the property reference will result in a message to the
743 /// This applies to both implicit and explicit property references.
744 bool isMessagingSetter() const {
745 return SetterAndMethodRefFlags.getInt() & MethodRef_Setter;
748 void setIsMessagingGetter(bool val = true) {
749 setMethodRefFlag(MethodRef_Getter, val);
752 void setIsMessagingSetter(bool val = true) {
753 setMethodRefFlag(MethodRef_Setter, val);
756 const Expr *getBase() const {
757 return cast<Expr>(Receiver.get<Stmt*>());
760 return cast<Expr>(Receiver.get<Stmt*>());
763 SourceLocation getLocation() const { return IdLoc; }
765 SourceLocation getReceiverLocation() const { return ReceiverLoc; }
767 QualType getSuperReceiverType() const {
768 return QualType(Receiver.get<const Type*>(), 0);
771 ObjCInterfaceDecl *getClassReceiver() const {
772 return Receiver.get<ObjCInterfaceDecl*>();
775 bool isObjectReceiver() const { return Receiver.is<Stmt*>(); }
776 bool isSuperReceiver() const { return Receiver.is<const Type*>(); }
777 bool isClassReceiver() const { return Receiver.is<ObjCInterfaceDecl*>(); }
779 /// Determine the type of the base, regardless of the kind of receiver.
780 QualType getReceiverType(const ASTContext &ctx) const;
782 SourceLocation getBeginLoc() const LLVM_READONLY {
783 return isObjectReceiver() ? getBase()->getBeginLoc()
784 : getReceiverLocation();
787 SourceLocation getEndLoc() const LLVM_READONLY { return IdLoc; }
790 child_range children() {
791 if (Receiver.is<Stmt*>()) {
792 Stmt **begin = reinterpret_cast<Stmt**>(&Receiver); // hack!
793 return child_range(begin, begin+1);
795 return child_range(child_iterator(), child_iterator());
798 const_child_range children() const {
799 auto Children = const_cast<ObjCPropertyRefExpr *>(this)->children();
800 return const_child_range(Children.begin(), Children.end());
803 static bool classof(const Stmt *T) {
804 return T->getStmtClass() == ObjCPropertyRefExprClass;
808 friend class ASTStmtReader;
809 friend class ASTStmtWriter;
811 void setExplicitProperty(ObjCPropertyDecl *D, unsigned methRefFlags) {
812 PropertyOrGetter.setPointer(D);
813 PropertyOrGetter.setInt(false);
814 SetterAndMethodRefFlags.setPointer(nullptr);
815 SetterAndMethodRefFlags.setInt(methRefFlags);
818 void setImplicitProperty(ObjCMethodDecl *Getter, ObjCMethodDecl *Setter,
819 unsigned methRefFlags) {
820 PropertyOrGetter.setPointer(Getter);
821 PropertyOrGetter.setInt(true);
822 SetterAndMethodRefFlags.setPointer(Setter);
823 SetterAndMethodRefFlags.setInt(methRefFlags);
826 void setBase(Expr *Base) { Receiver = Base; }
827 void setSuperReceiver(QualType T) { Receiver = T.getTypePtr(); }
828 void setClassReceiver(ObjCInterfaceDecl *D) { Receiver = D; }
830 void setLocation(SourceLocation L) { IdLoc = L; }
831 void setReceiverLocation(SourceLocation Loc) { ReceiverLoc = Loc; }
833 void setMethodRefFlag(MethodRefFlags flag, bool val) {
834 unsigned f = SetterAndMethodRefFlags.getInt();
839 SetterAndMethodRefFlags.setInt(f);
843 /// ObjCSubscriptRefExpr - used for array and dictionary subscripting.
844 /// array[4] = array[3]; dictionary[key] = dictionary[alt_key];
845 class ObjCSubscriptRefExpr : public Expr {
846 // Location of ']' in an indexing expression.
847 SourceLocation RBracket;
849 // array/dictionary base expression.
850 // for arrays, this is a numeric expression. For dictionaries, this is
851 // an objective-c object pointer expression.
852 enum { BASE, KEY, END_EXPR };
853 Stmt* SubExprs[END_EXPR];
855 ObjCMethodDecl *GetAtIndexMethodDecl;
857 // For immutable objects this is null. When ObjCSubscriptRefExpr is to read
858 // an indexed object this is null too.
859 ObjCMethodDecl *SetAtIndexMethodDecl;
862 ObjCSubscriptRefExpr(Expr *base, Expr *key, QualType T,
863 ExprValueKind VK, ExprObjectKind OK,
864 ObjCMethodDecl *getMethod,
865 ObjCMethodDecl *setMethod, SourceLocation RB)
866 : Expr(ObjCSubscriptRefExprClass, T, VK, OK,
867 base->isTypeDependent() || key->isTypeDependent(),
868 base->isValueDependent() || key->isValueDependent(),
869 (base->isInstantiationDependent() ||
870 key->isInstantiationDependent()),
871 (base->containsUnexpandedParameterPack() ||
872 key->containsUnexpandedParameterPack())),
873 RBracket(RB), GetAtIndexMethodDecl(getMethod),
874 SetAtIndexMethodDecl(setMethod) {
875 SubExprs[BASE] = base; SubExprs[KEY] = key;
878 explicit ObjCSubscriptRefExpr(EmptyShell Empty)
879 : Expr(ObjCSubscriptRefExprClass, Empty) {}
881 SourceLocation getRBracket() const { return RBracket; }
882 void setRBracket(SourceLocation RB) { RBracket = RB; }
884 SourceLocation getBeginLoc() const LLVM_READONLY {
885 return SubExprs[BASE]->getBeginLoc();
888 SourceLocation getEndLoc() const LLVM_READONLY { return RBracket; }
890 Expr *getBaseExpr() const { return cast<Expr>(SubExprs[BASE]); }
891 void setBaseExpr(Stmt *S) { SubExprs[BASE] = S; }
893 Expr *getKeyExpr() const { return cast<Expr>(SubExprs[KEY]); }
894 void setKeyExpr(Stmt *S) { SubExprs[KEY] = S; }
896 ObjCMethodDecl *getAtIndexMethodDecl() const {
897 return GetAtIndexMethodDecl;
900 ObjCMethodDecl *setAtIndexMethodDecl() const {
901 return SetAtIndexMethodDecl;
904 bool isArraySubscriptRefExpr() const {
905 return getKeyExpr()->getType()->isIntegralOrEnumerationType();
908 child_range children() {
909 return child_range(SubExprs, SubExprs+END_EXPR);
912 const_child_range children() const {
913 return const_child_range(SubExprs, SubExprs + END_EXPR);
916 static bool classof(const Stmt *T) {
917 return T->getStmtClass() == ObjCSubscriptRefExprClass;
921 friend class ASTStmtReader;
924 /// An expression that sends a message to the given Objective-C
927 /// The following contains two message send expressions:
930 /// [[NSString alloc] initWithString:@"Hello"]
933 /// The innermost message send invokes the "alloc" class method on the
934 /// NSString class, while the outermost message send invokes the
935 /// "initWithString" instance method on the object returned from
936 /// NSString's "alloc". In all, an Objective-C message send can take
937 /// on four different (although related) forms:
939 /// 1. Send to an object instance.
940 /// 2. Send to a class.
941 /// 3. Send to the superclass instance of the current class.
942 /// 4. Send to the superclass of the current class.
944 /// All four kinds of message sends are modeled by the ObjCMessageExpr
945 /// class, and can be distinguished via \c getReceiverKind(). Example:
947 /// The "void *" trailing objects are actually ONE void * (the
948 /// receiver pointer), and NumArgs Expr *. But due to the
949 /// implementation of children(), these must be together contiguously.
950 class ObjCMessageExpr final
952 private llvm::TrailingObjects<ObjCMessageExpr, void *, SourceLocation> {
953 /// Stores either the selector that this message is sending
954 /// to (when \c HasMethod is zero) or an \c ObjCMethodDecl pointer
955 /// referring to the method that we type-checked against.
956 uintptr_t SelectorOrMethod = 0;
958 enum { NumArgsBitWidth = 16 };
960 /// The number of arguments in the message send, not
961 /// including the receiver.
962 unsigned NumArgs : NumArgsBitWidth;
964 /// The kind of message send this is, which is one of the
965 /// ReceiverKind values.
967 /// We pad this out to a byte to avoid excessive masking and shifting.
970 /// Whether we have an actual method prototype in \c
971 /// SelectorOrMethod.
973 /// When non-zero, we have a method declaration; otherwise, we just
975 unsigned HasMethod : 1;
977 /// Whether this message send is a "delegate init call",
978 /// i.e. a call of an init method on self from within an init method.
979 unsigned IsDelegateInitCall : 1;
981 /// Whether this message send was implicitly generated by
982 /// the implementation rather than explicitly written by the user.
983 unsigned IsImplicit : 1;
985 /// Whether the locations of the selector identifiers are in a
986 /// "standard" position, a enum SelectorLocationsKind.
987 unsigned SelLocsKind : 2;
989 /// When the message expression is a send to 'super', this is
990 /// the location of the 'super' keyword.
991 SourceLocation SuperLoc;
993 /// The source locations of the open and close square
994 /// brackets ('[' and ']', respectively).
995 SourceLocation LBracLoc, RBracLoc;
997 ObjCMessageExpr(EmptyShell Empty, unsigned NumArgs)
998 : Expr(ObjCMessageExprClass, Empty), Kind(0), HasMethod(false),
999 IsDelegateInitCall(false), IsImplicit(false), SelLocsKind(0) {
1000 setNumArgs(NumArgs);
1003 ObjCMessageExpr(QualType T, ExprValueKind VK,
1004 SourceLocation LBracLoc,
1005 SourceLocation SuperLoc,
1006 bool IsInstanceSuper,
1009 ArrayRef<SourceLocation> SelLocs,
1010 SelectorLocationsKind SelLocsK,
1011 ObjCMethodDecl *Method,
1012 ArrayRef<Expr *> Args,
1013 SourceLocation RBracLoc,
1015 ObjCMessageExpr(QualType T, ExprValueKind VK,
1016 SourceLocation LBracLoc,
1017 TypeSourceInfo *Receiver,
1019 ArrayRef<SourceLocation> SelLocs,
1020 SelectorLocationsKind SelLocsK,
1021 ObjCMethodDecl *Method,
1022 ArrayRef<Expr *> Args,
1023 SourceLocation RBracLoc,
1025 ObjCMessageExpr(QualType T, ExprValueKind VK,
1026 SourceLocation LBracLoc,
1029 ArrayRef<SourceLocation> SelLocs,
1030 SelectorLocationsKind SelLocsK,
1031 ObjCMethodDecl *Method,
1032 ArrayRef<Expr *> Args,
1033 SourceLocation RBracLoc,
1036 size_t numTrailingObjects(OverloadToken<void *>) const { return NumArgs + 1; }
1038 void setNumArgs(unsigned Num) {
1039 assert((Num >> NumArgsBitWidth) == 0 && "Num of args is out of range!");
1043 void initArgsAndSelLocs(ArrayRef<Expr *> Args,
1044 ArrayRef<SourceLocation> SelLocs,
1045 SelectorLocationsKind SelLocsK);
1047 /// Retrieve the pointer value of the message receiver.
1048 void *getReceiverPointer() const { return *getTrailingObjects<void *>(); }
1050 /// Set the pointer value of the message receiver.
1051 void setReceiverPointer(void *Value) {
1052 *getTrailingObjects<void *>() = Value;
1055 SelectorLocationsKind getSelLocsKind() const {
1056 return (SelectorLocationsKind)SelLocsKind;
1059 bool hasStandardSelLocs() const {
1060 return getSelLocsKind() != SelLoc_NonStandard;
1063 /// Get a pointer to the stored selector identifiers locations array.
1064 /// No locations will be stored if HasStandardSelLocs is true.
1065 SourceLocation *getStoredSelLocs() {
1066 return getTrailingObjects<SourceLocation>();
1068 const SourceLocation *getStoredSelLocs() const {
1069 return getTrailingObjects<SourceLocation>();
1072 /// Get the number of stored selector identifiers locations.
1073 /// No locations will be stored if HasStandardSelLocs is true.
1074 unsigned getNumStoredSelLocs() const {
1075 if (hasStandardSelLocs())
1077 return getNumSelectorLocs();
1080 static ObjCMessageExpr *alloc(const ASTContext &C,
1081 ArrayRef<Expr *> Args,
1082 SourceLocation RBraceLoc,
1083 ArrayRef<SourceLocation> SelLocs,
1085 SelectorLocationsKind &SelLocsK);
1086 static ObjCMessageExpr *alloc(const ASTContext &C,
1088 unsigned NumStoredSelLocs);
1091 friend class ASTStmtReader;
1092 friend class ASTStmtWriter;
1093 friend TrailingObjects;
1095 /// The kind of receiver this message is sending to.
1097 /// The receiver is a class.
1100 /// The receiver is an object instance.
1103 /// The receiver is a superclass.
1106 /// The receiver is the instance of the superclass object.
1110 /// Create a message send to super.
1112 /// \param Context The ASTContext in which this expression will be created.
1114 /// \param T The result type of this message.
1116 /// \param VK The value kind of this message. A message returning
1117 /// a l-value or r-value reference will be an l-value or x-value,
1120 /// \param LBracLoc The location of the open square bracket '['.
1122 /// \param SuperLoc The location of the "super" keyword.
1124 /// \param IsInstanceSuper Whether this is an instance "super"
1125 /// message (otherwise, it's a class "super" message).
1127 /// \param Sel The selector used to determine which method gets called.
1129 /// \param Method The Objective-C method against which this message
1130 /// send was type-checked. May be nullptr.
1132 /// \param Args The message send arguments.
1134 /// \param RBracLoc The location of the closing square bracket ']'.
1135 static ObjCMessageExpr *Create(const ASTContext &Context, QualType T,
1137 SourceLocation LBracLoc,
1138 SourceLocation SuperLoc,
1139 bool IsInstanceSuper,
1142 ArrayRef<SourceLocation> SelLocs,
1143 ObjCMethodDecl *Method,
1144 ArrayRef<Expr *> Args,
1145 SourceLocation RBracLoc,
1148 /// Create a class message send.
1150 /// \param Context The ASTContext in which this expression will be created.
1152 /// \param T The result type of this message.
1154 /// \param VK The value kind of this message. A message returning
1155 /// a l-value or r-value reference will be an l-value or x-value,
1158 /// \param LBracLoc The location of the open square bracket '['.
1160 /// \param Receiver The type of the receiver, including
1161 /// source-location information.
1163 /// \param Sel The selector used to determine which method gets called.
1165 /// \param Method The Objective-C method against which this message
1166 /// send was type-checked. May be nullptr.
1168 /// \param Args The message send arguments.
1170 /// \param RBracLoc The location of the closing square bracket ']'.
1171 static ObjCMessageExpr *Create(const ASTContext &Context, QualType T,
1173 SourceLocation LBracLoc,
1174 TypeSourceInfo *Receiver,
1176 ArrayRef<SourceLocation> SelLocs,
1177 ObjCMethodDecl *Method,
1178 ArrayRef<Expr *> Args,
1179 SourceLocation RBracLoc,
1182 /// Create an instance message send.
1184 /// \param Context The ASTContext in which this expression will be created.
1186 /// \param T The result type of this message.
1188 /// \param VK The value kind of this message. A message returning
1189 /// a l-value or r-value reference will be an l-value or x-value,
1192 /// \param LBracLoc The location of the open square bracket '['.
1194 /// \param Receiver The expression used to produce the object that
1195 /// will receive this message.
1197 /// \param Sel The selector used to determine which method gets called.
1199 /// \param Method The Objective-C method against which this message
1200 /// send was type-checked. May be nullptr.
1202 /// \param Args The message send arguments.
1204 /// \param RBracLoc The location of the closing square bracket ']'.
1205 static ObjCMessageExpr *Create(const ASTContext &Context, QualType T,
1207 SourceLocation LBracLoc,
1210 ArrayRef<SourceLocation> SeLocs,
1211 ObjCMethodDecl *Method,
1212 ArrayRef<Expr *> Args,
1213 SourceLocation RBracLoc,
1216 /// Create an empty Objective-C message expression, to be
1217 /// filled in by subsequent calls.
1219 /// \param Context The context in which the message send will be created.
1221 /// \param NumArgs The number of message arguments, not including
1223 static ObjCMessageExpr *CreateEmpty(const ASTContext &Context,
1225 unsigned NumStoredSelLocs);
1227 /// Indicates whether the message send was implicitly
1228 /// generated by the implementation. If false, it was written explicitly
1229 /// in the source code.
1230 bool isImplicit() const { return IsImplicit; }
1232 /// Determine the kind of receiver that this message is being
1234 ReceiverKind getReceiverKind() const { return (ReceiverKind)Kind; }
1236 /// \return the return type of the message being sent.
1237 /// This is not always the type of the message expression itself because
1238 /// of references (the expression would not have a reference type).
1239 /// It is also not always the declared return type of the method because
1240 /// of `instancetype` (in that case it's an expression type).
1241 QualType getCallReturnType(ASTContext &Ctx) const;
1243 /// Source range of the receiver.
1244 SourceRange getReceiverRange() const;
1246 /// Determine whether this is an instance message to either a
1247 /// computed object or to super.
1248 bool isInstanceMessage() const {
1249 return getReceiverKind() == Instance || getReceiverKind() == SuperInstance;
1252 /// Determine whether this is an class message to either a
1253 /// specified class or to super.
1254 bool isClassMessage() const {
1255 return getReceiverKind() == Class || getReceiverKind() == SuperClass;
1258 /// Returns the object expression (receiver) for an instance message,
1259 /// or null for a message that is not an instance message.
1260 Expr *getInstanceReceiver() {
1261 if (getReceiverKind() == Instance)
1262 return static_cast<Expr *>(getReceiverPointer());
1266 const Expr *getInstanceReceiver() const {
1267 return const_cast<ObjCMessageExpr*>(this)->getInstanceReceiver();
1270 /// Turn this message send into an instance message that
1271 /// computes the receiver object with the given expression.
1272 void setInstanceReceiver(Expr *rec) {
1274 setReceiverPointer(rec);
1277 /// Returns the type of a class message send, or NULL if the
1278 /// message is not a class message.
1279 QualType getClassReceiver() const {
1280 if (TypeSourceInfo *TSInfo = getClassReceiverTypeInfo())
1281 return TSInfo->getType();
1286 /// Returns a type-source information of a class message
1287 /// send, or nullptr if the message is not a class message.
1288 TypeSourceInfo *getClassReceiverTypeInfo() const {
1289 if (getReceiverKind() == Class)
1290 return reinterpret_cast<TypeSourceInfo *>(getReceiverPointer());
1294 void setClassReceiver(TypeSourceInfo *TSInfo) {
1296 setReceiverPointer(TSInfo);
1299 /// Retrieve the location of the 'super' keyword for a class
1300 /// or instance message to 'super', otherwise an invalid source location.
1301 SourceLocation getSuperLoc() const {
1302 if (getReceiverKind() == SuperInstance || getReceiverKind() == SuperClass)
1305 return SourceLocation();
1308 /// Retrieve the receiver type to which this message is being directed.
1310 /// This routine cross-cuts all of the different kinds of message
1311 /// sends to determine what the underlying (statically known) type
1312 /// of the receiver will be; use \c getReceiverKind() to determine
1313 /// whether the message is a class or an instance method, whether it
1314 /// is a send to super or not, etc.
1316 /// \returns The type of the receiver.
1317 QualType getReceiverType() const;
1319 /// Retrieve the Objective-C interface to which this message
1320 /// is being directed, if known.
1322 /// This routine cross-cuts all of the different kinds of message
1323 /// sends to determine what the underlying (statically known) type
1324 /// of the receiver will be; use \c getReceiverKind() to determine
1325 /// whether the message is a class or an instance method, whether it
1326 /// is a send to super or not, etc.
1328 /// \returns The Objective-C interface if known, otherwise nullptr.
1329 ObjCInterfaceDecl *getReceiverInterface() const;
1331 /// Retrieve the type referred to by 'super'.
1333 /// The returned type will either be an ObjCInterfaceType (for an
1334 /// class message to super) or an ObjCObjectPointerType that refers
1335 /// to a class (for an instance message to super);
1336 QualType getSuperType() const {
1337 if (getReceiverKind() == SuperInstance || getReceiverKind() == SuperClass)
1338 return QualType::getFromOpaquePtr(getReceiverPointer());
1343 void setSuper(SourceLocation Loc, QualType T, bool IsInstanceSuper) {
1344 Kind = IsInstanceSuper? SuperInstance : SuperClass;
1346 setReceiverPointer(T.getAsOpaquePtr());
1349 Selector getSelector() const;
1351 void setSelector(Selector S) {
1353 SelectorOrMethod = reinterpret_cast<uintptr_t>(S.getAsOpaquePtr());
1356 const ObjCMethodDecl *getMethodDecl() const {
1358 return reinterpret_cast<const ObjCMethodDecl *>(SelectorOrMethod);
1363 ObjCMethodDecl *getMethodDecl() {
1365 return reinterpret_cast<ObjCMethodDecl *>(SelectorOrMethod);
1370 void setMethodDecl(ObjCMethodDecl *MD) {
1372 SelectorOrMethod = reinterpret_cast<uintptr_t>(MD);
1375 ObjCMethodFamily getMethodFamily() const {
1376 if (HasMethod) return getMethodDecl()->getMethodFamily();
1377 return getSelector().getMethodFamily();
1380 /// Return the number of actual arguments in this message,
1381 /// not counting the receiver.
1382 unsigned getNumArgs() const { return NumArgs; }
1384 /// Retrieve the arguments to this message, not including the
1387 return reinterpret_cast<Expr **>(getTrailingObjects<void *>() + 1);
1389 const Expr * const *getArgs() const {
1390 return reinterpret_cast<const Expr *const *>(getTrailingObjects<void *>() +
1394 /// getArg - Return the specified argument.
1395 Expr *getArg(unsigned Arg) {
1396 assert(Arg < NumArgs && "Arg access out of range!");
1397 return getArgs()[Arg];
1399 const Expr *getArg(unsigned Arg) const {
1400 assert(Arg < NumArgs && "Arg access out of range!");
1401 return getArgs()[Arg];
1404 /// setArg - Set the specified argument.
1405 void setArg(unsigned Arg, Expr *ArgExpr) {
1406 assert(Arg < NumArgs && "Arg access out of range!");
1407 getArgs()[Arg] = ArgExpr;
1410 /// isDelegateInitCall - Answers whether this message send has been
1411 /// tagged as a "delegate init call", i.e. a call to a method in the
1412 /// -init family on self from within an -init method implementation.
1413 bool isDelegateInitCall() const { return IsDelegateInitCall; }
1414 void setDelegateInitCall(bool isDelegate) { IsDelegateInitCall = isDelegate; }
1416 SourceLocation getLeftLoc() const { return LBracLoc; }
1417 SourceLocation getRightLoc() const { return RBracLoc; }
1419 SourceLocation getSelectorStartLoc() const {
1421 return getBeginLoc();
1422 return getSelectorLoc(0);
1425 SourceLocation getSelectorLoc(unsigned Index) const {
1426 assert(Index < getNumSelectorLocs() && "Index out of range!");
1427 if (hasStandardSelLocs())
1428 return getStandardSelectorLoc(Index, getSelector(),
1429 getSelLocsKind() == SelLoc_StandardWithSpace,
1430 llvm::makeArrayRef(const_cast<Expr**>(getArgs()),
1433 return getStoredSelLocs()[Index];
1436 void getSelectorLocs(SmallVectorImpl<SourceLocation> &SelLocs) const;
1438 unsigned getNumSelectorLocs() const {
1441 Selector Sel = getSelector();
1442 if (Sel.isUnarySelector())
1444 return Sel.getNumArgs();
1447 void setSourceRange(SourceRange R) {
1448 LBracLoc = R.getBegin();
1449 RBracLoc = R.getEnd();
1452 SourceLocation getBeginLoc() const LLVM_READONLY { return LBracLoc; }
1453 SourceLocation getEndLoc() const LLVM_READONLY { return RBracLoc; }
1456 child_range children();
1458 const_child_range children() const;
1460 using arg_iterator = ExprIterator;
1461 using const_arg_iterator = ConstExprIterator;
1463 llvm::iterator_range<arg_iterator> arguments() {
1464 return llvm::make_range(arg_begin(), arg_end());
1467 llvm::iterator_range<const_arg_iterator> arguments() const {
1468 return llvm::make_range(arg_begin(), arg_end());
1471 arg_iterator arg_begin() { return reinterpret_cast<Stmt **>(getArgs()); }
1473 arg_iterator arg_end() {
1474 return reinterpret_cast<Stmt **>(getArgs() + NumArgs);
1477 const_arg_iterator arg_begin() const {
1478 return reinterpret_cast<Stmt const * const*>(getArgs());
1481 const_arg_iterator arg_end() const {
1482 return reinterpret_cast<Stmt const * const*>(getArgs() + NumArgs);
1485 static bool classof(const Stmt *T) {
1486 return T->getStmtClass() == ObjCMessageExprClass;
1490 /// ObjCIsaExpr - Represent X->isa and X.isa when X is an ObjC 'id' type.
1491 /// (similar in spirit to MemberExpr).
1492 class ObjCIsaExpr : public Expr {
1493 /// Base - the expression for the base object pointer.
1496 /// IsaMemberLoc - This is the location of the 'isa'.
1497 SourceLocation IsaMemberLoc;
1499 /// OpLoc - This is the location of '.' or '->'
1500 SourceLocation OpLoc;
1502 /// IsArrow - True if this is "X->F", false if this is "X.F".
1506 ObjCIsaExpr(Expr *base, bool isarrow, SourceLocation l, SourceLocation oploc,
1508 : Expr(ObjCIsaExprClass, ty, VK_LValue, OK_Ordinary,
1509 /*TypeDependent=*/false, base->isValueDependent(),
1510 base->isInstantiationDependent(),
1511 /*ContainsUnexpandedParameterPack=*/false),
1512 Base(base), IsaMemberLoc(l), OpLoc(oploc), IsArrow(isarrow) {}
1514 /// Build an empty expression.
1515 explicit ObjCIsaExpr(EmptyShell Empty) : Expr(ObjCIsaExprClass, Empty) {}
1517 void setBase(Expr *E) { Base = E; }
1518 Expr *getBase() const { return cast<Expr>(Base); }
1520 bool isArrow() const { return IsArrow; }
1521 void setArrow(bool A) { IsArrow = A; }
1523 /// getMemberLoc - Return the location of the "member", in X->F, it is the
1524 /// location of 'F'.
1525 SourceLocation getIsaMemberLoc() const { return IsaMemberLoc; }
1526 void setIsaMemberLoc(SourceLocation L) { IsaMemberLoc = L; }
1528 SourceLocation getOpLoc() const { return OpLoc; }
1529 void setOpLoc(SourceLocation L) { OpLoc = L; }
1531 SourceLocation getBeginLoc() const LLVM_READONLY {
1532 return getBase()->getBeginLoc();
1535 SourceLocation getBaseLocEnd() const LLVM_READONLY {
1536 return getBase()->getEndLoc();
1539 SourceLocation getEndLoc() const LLVM_READONLY { return IsaMemberLoc; }
1541 SourceLocation getExprLoc() const LLVM_READONLY { return IsaMemberLoc; }
1544 child_range children() { return child_range(&Base, &Base+1); }
1546 const_child_range children() const {
1547 return const_child_range(&Base, &Base + 1);
1550 static bool classof(const Stmt *T) {
1551 return T->getStmtClass() == ObjCIsaExprClass;
1555 /// ObjCIndirectCopyRestoreExpr - Represents the passing of a function
1556 /// argument by indirect copy-restore in ARC. This is used to support
1557 /// passing indirect arguments with the wrong lifetime, e.g. when
1558 /// passing the address of a __strong local variable to an 'out'
1559 /// parameter. This expression kind is only valid in an "argument"
1560 /// position to some sort of call expression.
1562 /// The parameter must have type 'pointer to T', and the argument must
1563 /// have type 'pointer to U', where T and U agree except possibly in
1564 /// qualification. If the argument value is null, then a null pointer
1565 /// is passed; otherwise it points to an object A, and:
1566 /// 1. A temporary object B of type T is initialized, either by
1567 /// zero-initialization (used when initializing an 'out' parameter)
1568 /// or copy-initialization (used when initializing an 'inout'
1570 /// 2. The address of the temporary is passed to the function.
1571 /// 3. If the call completes normally, A is move-assigned from B.
1572 /// 4. Finally, A is destroyed immediately.
1574 /// Currently 'T' must be a retainable object lifetime and must be
1575 /// __autoreleasing; this qualifier is ignored when initializing
1577 class ObjCIndirectCopyRestoreExpr : public Expr {
1578 friend class ASTReader;
1579 friend class ASTStmtReader;
1583 // unsigned ObjCIndirectCopyRestoreBits.ShouldCopy : 1;
1585 explicit ObjCIndirectCopyRestoreExpr(EmptyShell Empty)
1586 : Expr(ObjCIndirectCopyRestoreExprClass, Empty) {}
1588 void setShouldCopy(bool shouldCopy) {
1589 ObjCIndirectCopyRestoreExprBits.ShouldCopy = shouldCopy;
1593 ObjCIndirectCopyRestoreExpr(Expr *operand, QualType type, bool shouldCopy)
1594 : Expr(ObjCIndirectCopyRestoreExprClass, type, VK_LValue, OK_Ordinary,
1595 operand->isTypeDependent(), operand->isValueDependent(),
1596 operand->isInstantiationDependent(),
1597 operand->containsUnexpandedParameterPack()),
1599 setShouldCopy(shouldCopy);
1602 Expr *getSubExpr() { return cast<Expr>(Operand); }
1603 const Expr *getSubExpr() const { return cast<Expr>(Operand); }
1605 /// shouldCopy - True if we should do the 'copy' part of the
1606 /// copy-restore. If false, the temporary will be zero-initialized.
1607 bool shouldCopy() const { return ObjCIndirectCopyRestoreExprBits.ShouldCopy; }
1609 child_range children() { return child_range(&Operand, &Operand+1); }
1611 const_child_range children() const {
1612 return const_child_range(&Operand, &Operand + 1);
1615 // Source locations are determined by the subexpression.
1616 SourceLocation getBeginLoc() const LLVM_READONLY {
1617 return Operand->getBeginLoc();
1619 SourceLocation getEndLoc() const LLVM_READONLY {
1620 return Operand->getEndLoc();
1623 SourceLocation getExprLoc() const LLVM_READONLY {
1624 return getSubExpr()->getExprLoc();
1627 static bool classof(const Stmt *s) {
1628 return s->getStmtClass() == ObjCIndirectCopyRestoreExprClass;
1632 /// An Objective-C "bridged" cast expression, which casts between
1633 /// Objective-C pointers and C pointers, transferring ownership in the process.
1636 /// NSString *str = (__bridge_transfer NSString *)CFCreateString();
1638 class ObjCBridgedCastExpr final
1639 : public ExplicitCastExpr,
1640 private llvm::TrailingObjects<ObjCBridgedCastExpr, CXXBaseSpecifier *> {
1641 friend class ASTStmtReader;
1642 friend class ASTStmtWriter;
1643 friend class CastExpr;
1644 friend TrailingObjects;
1646 SourceLocation LParenLoc;
1647 SourceLocation BridgeKeywordLoc;
1651 ObjCBridgedCastExpr(SourceLocation LParenLoc, ObjCBridgeCastKind Kind,
1652 CastKind CK, SourceLocation BridgeKeywordLoc,
1653 TypeSourceInfo *TSInfo, Expr *Operand)
1654 : ExplicitCastExpr(ObjCBridgedCastExprClass, TSInfo->getType(), VK_RValue,
1655 CK, Operand, 0, TSInfo),
1656 LParenLoc(LParenLoc), BridgeKeywordLoc(BridgeKeywordLoc), Kind(Kind) {}
1658 /// Construct an empty Objective-C bridged cast.
1659 explicit ObjCBridgedCastExpr(EmptyShell Shell)
1660 : ExplicitCastExpr(ObjCBridgedCastExprClass, Shell, 0) {}
1662 SourceLocation getLParenLoc() const { return LParenLoc; }
1664 /// Determine which kind of bridge is being performed via this cast.
1665 ObjCBridgeCastKind getBridgeKind() const {
1666 return static_cast<ObjCBridgeCastKind>(Kind);
1669 /// Retrieve the kind of bridge being performed as a string.
1670 StringRef getBridgeKindName() const;
1672 /// The location of the bridge keyword.
1673 SourceLocation getBridgeKeywordLoc() const { return BridgeKeywordLoc; }
1675 SourceLocation getBeginLoc() const LLVM_READONLY { return LParenLoc; }
1677 SourceLocation getEndLoc() const LLVM_READONLY {
1678 return getSubExpr()->getEndLoc();
1681 static bool classof(const Stmt *T) {
1682 return T->getStmtClass() == ObjCBridgedCastExprClass;
1686 /// A runtime availability query.
1688 /// There are 2 ways to spell this node:
1690 /// @available(macos 10.10, ios 8, *); // Objective-C
1691 /// __builtin_available(macos 10.10, ios 8, *); // C, C++, and Objective-C
1694 /// Note that we only need to keep track of one \c VersionTuple here, which is
1695 /// the one that corresponds to the current deployment target. This is meant to
1696 /// be used in the condition of an \c if, but it is also usable as top level
1699 class ObjCAvailabilityCheckExpr : public Expr {
1700 friend class ASTStmtReader;
1702 VersionTuple VersionToCheck;
1703 SourceLocation AtLoc, RParen;
1706 ObjCAvailabilityCheckExpr(VersionTuple VersionToCheck, SourceLocation AtLoc,
1707 SourceLocation RParen, QualType Ty)
1708 : Expr(ObjCAvailabilityCheckExprClass, Ty, VK_RValue, OK_Ordinary, false,
1709 false, false, false),
1710 VersionToCheck(VersionToCheck), AtLoc(AtLoc), RParen(RParen) {}
1712 explicit ObjCAvailabilityCheckExpr(EmptyShell Shell)
1713 : Expr(ObjCAvailabilityCheckExprClass, Shell) {}
1715 SourceLocation getBeginLoc() const { return AtLoc; }
1716 SourceLocation getEndLoc() const { return RParen; }
1717 SourceRange getSourceRange() const { return {AtLoc, RParen}; }
1719 /// This may be '*', in which case this should fold to true.
1720 bool hasVersion() const { return !VersionToCheck.empty(); }
1721 VersionTuple getVersion() { return VersionToCheck; }
1723 child_range children() {
1724 return child_range(child_iterator(), child_iterator());
1727 const_child_range children() const {
1728 return const_child_range(const_child_iterator(), const_child_iterator());
1731 static bool classof(const Stmt *T) {
1732 return T->getStmtClass() == ObjCAvailabilityCheckExprClass;
1736 } // namespace clang
1738 #endif // LLVM_CLANG_AST_EXPROBJC_H