1 1.1 joerg //===- ExprObjC.cpp - (ObjC) Expression AST Node Implementation -----------===// 2 1.1 joerg // 3 1.1 joerg // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 1.1 joerg // See https://llvm.org/LICENSE.txt for license information. 5 1.1 joerg // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 1.1 joerg // 7 1.1 joerg //===----------------------------------------------------------------------===// 8 1.1 joerg // 9 1.1 joerg // This file implements the subclesses of Expr class declared in ExprObjC.h 10 1.1 joerg // 11 1.1 joerg //===----------------------------------------------------------------------===// 12 1.1 joerg 13 1.1 joerg #include "clang/AST/ExprObjC.h" 14 1.1 joerg #include "clang/AST/ASTContext.h" 15 1.1.1.2 joerg #include "clang/AST/ComputeDependence.h" 16 1.1.1.2 joerg #include "clang/AST/DependenceFlags.h" 17 1.1 joerg #include "clang/AST/SelectorLocationsKind.h" 18 1.1 joerg #include "clang/AST/Type.h" 19 1.1 joerg #include "clang/AST/TypeLoc.h" 20 1.1 joerg #include "llvm/ADT/SmallVector.h" 21 1.1 joerg #include "llvm/Support/ErrorHandling.h" 22 1.1 joerg #include <algorithm> 23 1.1 joerg #include <cassert> 24 1.1 joerg #include <cstdint> 25 1.1 joerg 26 1.1 joerg using namespace clang; 27 1.1 joerg 28 1.1 joerg ObjCArrayLiteral::ObjCArrayLiteral(ArrayRef<Expr *> Elements, QualType T, 29 1.1 joerg ObjCMethodDecl *Method, SourceRange SR) 30 1.1.1.2 joerg : Expr(ObjCArrayLiteralClass, T, VK_RValue, OK_Ordinary), 31 1.1 joerg NumElements(Elements.size()), Range(SR), ArrayWithObjectsMethod(Method) { 32 1.1 joerg Expr **SaveElements = getElements(); 33 1.1.1.2 joerg for (unsigned I = 0, N = Elements.size(); I != N; ++I) 34 1.1 joerg SaveElements[I] = Elements[I]; 35 1.1.1.2 joerg 36 1.1.1.2 joerg setDependence(computeDependence(this)); 37 1.1 joerg } 38 1.1 joerg 39 1.1 joerg ObjCArrayLiteral *ObjCArrayLiteral::Create(const ASTContext &C, 40 1.1 joerg ArrayRef<Expr *> Elements, 41 1.1 joerg QualType T, ObjCMethodDecl *Method, 42 1.1 joerg SourceRange SR) { 43 1.1 joerg void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(Elements.size())); 44 1.1 joerg return new (Mem) ObjCArrayLiteral(Elements, T, Method, SR); 45 1.1 joerg } 46 1.1 joerg 47 1.1 joerg ObjCArrayLiteral *ObjCArrayLiteral::CreateEmpty(const ASTContext &C, 48 1.1 joerg unsigned NumElements) { 49 1.1 joerg void *Mem = C.Allocate(totalSizeToAlloc<Expr *>(NumElements)); 50 1.1 joerg return new (Mem) ObjCArrayLiteral(EmptyShell(), NumElements); 51 1.1 joerg } 52 1.1 joerg 53 1.1 joerg ObjCDictionaryLiteral::ObjCDictionaryLiteral(ArrayRef<ObjCDictionaryElement> VK, 54 1.1 joerg bool HasPackExpansions, QualType T, 55 1.1 joerg ObjCMethodDecl *method, 56 1.1 joerg SourceRange SR) 57 1.1.1.2 joerg : Expr(ObjCDictionaryLiteralClass, T, VK_RValue, OK_Ordinary), 58 1.1 joerg NumElements(VK.size()), HasPackExpansions(HasPackExpansions), Range(SR), 59 1.1 joerg DictWithObjectsMethod(method) { 60 1.1 joerg KeyValuePair *KeyValues = getTrailingObjects<KeyValuePair>(); 61 1.1 joerg ExpansionData *Expansions = 62 1.1 joerg HasPackExpansions ? getTrailingObjects<ExpansionData>() : nullptr; 63 1.1 joerg for (unsigned I = 0; I < NumElements; I++) { 64 1.1 joerg KeyValues[I].Key = VK[I].Key; 65 1.1 joerg KeyValues[I].Value = VK[I].Value; 66 1.1 joerg if (Expansions) { 67 1.1 joerg Expansions[I].EllipsisLoc = VK[I].EllipsisLoc; 68 1.1 joerg if (VK[I].NumExpansions) 69 1.1 joerg Expansions[I].NumExpansionsPlusOne = *VK[I].NumExpansions + 1; 70 1.1 joerg else 71 1.1 joerg Expansions[I].NumExpansionsPlusOne = 0; 72 1.1 joerg } 73 1.1 joerg } 74 1.1.1.2 joerg setDependence(computeDependence(this)); 75 1.1 joerg } 76 1.1 joerg 77 1.1 joerg ObjCDictionaryLiteral * 78 1.1 joerg ObjCDictionaryLiteral::Create(const ASTContext &C, 79 1.1 joerg ArrayRef<ObjCDictionaryElement> VK, 80 1.1 joerg bool HasPackExpansions, QualType T, 81 1.1 joerg ObjCMethodDecl *method, SourceRange SR) { 82 1.1 joerg void *Mem = C.Allocate(totalSizeToAlloc<KeyValuePair, ExpansionData>( 83 1.1 joerg VK.size(), HasPackExpansions ? VK.size() : 0)); 84 1.1 joerg return new (Mem) ObjCDictionaryLiteral(VK, HasPackExpansions, T, method, SR); 85 1.1 joerg } 86 1.1 joerg 87 1.1 joerg ObjCDictionaryLiteral * 88 1.1 joerg ObjCDictionaryLiteral::CreateEmpty(const ASTContext &C, unsigned NumElements, 89 1.1 joerg bool HasPackExpansions) { 90 1.1 joerg void *Mem = C.Allocate(totalSizeToAlloc<KeyValuePair, ExpansionData>( 91 1.1 joerg NumElements, HasPackExpansions ? NumElements : 0)); 92 1.1 joerg return new (Mem) 93 1.1 joerg ObjCDictionaryLiteral(EmptyShell(), NumElements, HasPackExpansions); 94 1.1 joerg } 95 1.1 joerg 96 1.1 joerg QualType ObjCPropertyRefExpr::getReceiverType(const ASTContext &ctx) const { 97 1.1 joerg if (isClassReceiver()) 98 1.1 joerg return ctx.getObjCInterfaceType(getClassReceiver()); 99 1.1 joerg 100 1.1 joerg if (isSuperReceiver()) 101 1.1 joerg return getSuperReceiverType(); 102 1.1 joerg 103 1.1 joerg return getBase()->getType(); 104 1.1 joerg } 105 1.1 joerg 106 1.1 joerg ObjCMessageExpr::ObjCMessageExpr(QualType T, ExprValueKind VK, 107 1.1 joerg SourceLocation LBracLoc, 108 1.1 joerg SourceLocation SuperLoc, bool IsInstanceSuper, 109 1.1 joerg QualType SuperType, Selector Sel, 110 1.1 joerg ArrayRef<SourceLocation> SelLocs, 111 1.1 joerg SelectorLocationsKind SelLocsK, 112 1.1 joerg ObjCMethodDecl *Method, ArrayRef<Expr *> Args, 113 1.1 joerg SourceLocation RBracLoc, bool isImplicit) 114 1.1.1.2 joerg : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary), 115 1.1 joerg SelectorOrMethod( 116 1.1 joerg reinterpret_cast<uintptr_t>(Method ? Method : Sel.getAsOpaquePtr())), 117 1.1 joerg Kind(IsInstanceSuper ? SuperInstance : SuperClass), 118 1.1 joerg HasMethod(Method != nullptr), IsDelegateInitCall(false), 119 1.1 joerg IsImplicit(isImplicit), SuperLoc(SuperLoc), LBracLoc(LBracLoc), 120 1.1 joerg RBracLoc(RBracLoc) { 121 1.1 joerg initArgsAndSelLocs(Args, SelLocs, SelLocsK); 122 1.1 joerg setReceiverPointer(SuperType.getAsOpaquePtr()); 123 1.1.1.2 joerg setDependence(computeDependence(this)); 124 1.1 joerg } 125 1.1 joerg 126 1.1 joerg ObjCMessageExpr::ObjCMessageExpr(QualType T, ExprValueKind VK, 127 1.1 joerg SourceLocation LBracLoc, 128 1.1 joerg TypeSourceInfo *Receiver, Selector Sel, 129 1.1 joerg ArrayRef<SourceLocation> SelLocs, 130 1.1 joerg SelectorLocationsKind SelLocsK, 131 1.1 joerg ObjCMethodDecl *Method, ArrayRef<Expr *> Args, 132 1.1 joerg SourceLocation RBracLoc, bool isImplicit) 133 1.1.1.2 joerg : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary), 134 1.1 joerg SelectorOrMethod( 135 1.1 joerg reinterpret_cast<uintptr_t>(Method ? Method : Sel.getAsOpaquePtr())), 136 1.1 joerg Kind(Class), HasMethod(Method != nullptr), IsDelegateInitCall(false), 137 1.1 joerg IsImplicit(isImplicit), LBracLoc(LBracLoc), RBracLoc(RBracLoc) { 138 1.1 joerg initArgsAndSelLocs(Args, SelLocs, SelLocsK); 139 1.1 joerg setReceiverPointer(Receiver); 140 1.1.1.2 joerg setDependence(computeDependence(this)); 141 1.1 joerg } 142 1.1 joerg 143 1.1 joerg ObjCMessageExpr::ObjCMessageExpr(QualType T, ExprValueKind VK, 144 1.1 joerg SourceLocation LBracLoc, Expr *Receiver, 145 1.1 joerg Selector Sel, ArrayRef<SourceLocation> SelLocs, 146 1.1 joerg SelectorLocationsKind SelLocsK, 147 1.1 joerg ObjCMethodDecl *Method, ArrayRef<Expr *> Args, 148 1.1 joerg SourceLocation RBracLoc, bool isImplicit) 149 1.1.1.2 joerg : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary), 150 1.1 joerg SelectorOrMethod( 151 1.1 joerg reinterpret_cast<uintptr_t>(Method ? Method : Sel.getAsOpaquePtr())), 152 1.1 joerg Kind(Instance), HasMethod(Method != nullptr), IsDelegateInitCall(false), 153 1.1 joerg IsImplicit(isImplicit), LBracLoc(LBracLoc), RBracLoc(RBracLoc) { 154 1.1 joerg initArgsAndSelLocs(Args, SelLocs, SelLocsK); 155 1.1 joerg setReceiverPointer(Receiver); 156 1.1.1.2 joerg setDependence(computeDependence(this)); 157 1.1 joerg } 158 1.1 joerg 159 1.1 joerg void ObjCMessageExpr::initArgsAndSelLocs(ArrayRef<Expr *> Args, 160 1.1 joerg ArrayRef<SourceLocation> SelLocs, 161 1.1 joerg SelectorLocationsKind SelLocsK) { 162 1.1 joerg setNumArgs(Args.size()); 163 1.1 joerg Expr **MyArgs = getArgs(); 164 1.1.1.2 joerg for (unsigned I = 0; I != Args.size(); ++I) 165 1.1 joerg MyArgs[I] = Args[I]; 166 1.1 joerg 167 1.1 joerg SelLocsKind = SelLocsK; 168 1.1 joerg if (!isImplicit()) { 169 1.1 joerg if (SelLocsK == SelLoc_NonStandard) 170 1.1 joerg std::copy(SelLocs.begin(), SelLocs.end(), getStoredSelLocs()); 171 1.1 joerg } 172 1.1 joerg } 173 1.1 joerg 174 1.1 joerg ObjCMessageExpr * 175 1.1 joerg ObjCMessageExpr::Create(const ASTContext &Context, QualType T, ExprValueKind VK, 176 1.1 joerg SourceLocation LBracLoc, SourceLocation SuperLoc, 177 1.1 joerg bool IsInstanceSuper, QualType SuperType, Selector Sel, 178 1.1 joerg ArrayRef<SourceLocation> SelLocs, 179 1.1 joerg ObjCMethodDecl *Method, ArrayRef<Expr *> Args, 180 1.1 joerg SourceLocation RBracLoc, bool isImplicit) { 181 1.1 joerg assert((!SelLocs.empty() || isImplicit) && 182 1.1 joerg "No selector locs for non-implicit message"); 183 1.1 joerg ObjCMessageExpr *Mem; 184 1.1 joerg SelectorLocationsKind SelLocsK = SelectorLocationsKind(); 185 1.1 joerg if (isImplicit) 186 1.1 joerg Mem = alloc(Context, Args.size(), 0); 187 1.1 joerg else 188 1.1 joerg Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK); 189 1.1 joerg return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, SuperLoc, IsInstanceSuper, 190 1.1 joerg SuperType, Sel, SelLocs, SelLocsK, Method, 191 1.1 joerg Args, RBracLoc, isImplicit); 192 1.1 joerg } 193 1.1 joerg 194 1.1 joerg ObjCMessageExpr * 195 1.1 joerg ObjCMessageExpr::Create(const ASTContext &Context, QualType T, ExprValueKind VK, 196 1.1 joerg SourceLocation LBracLoc, TypeSourceInfo *Receiver, 197 1.1 joerg Selector Sel, ArrayRef<SourceLocation> SelLocs, 198 1.1 joerg ObjCMethodDecl *Method, ArrayRef<Expr *> Args, 199 1.1 joerg SourceLocation RBracLoc, bool isImplicit) { 200 1.1 joerg assert((!SelLocs.empty() || isImplicit) && 201 1.1 joerg "No selector locs for non-implicit message"); 202 1.1 joerg ObjCMessageExpr *Mem; 203 1.1 joerg SelectorLocationsKind SelLocsK = SelectorLocationsKind(); 204 1.1 joerg if (isImplicit) 205 1.1 joerg Mem = alloc(Context, Args.size(), 0); 206 1.1 joerg else 207 1.1 joerg Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK); 208 1.1 joerg return new (Mem) 209 1.1 joerg ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel, SelLocs, SelLocsK, Method, 210 1.1 joerg Args, RBracLoc, isImplicit); 211 1.1 joerg } 212 1.1 joerg 213 1.1 joerg ObjCMessageExpr * 214 1.1 joerg ObjCMessageExpr::Create(const ASTContext &Context, QualType T, ExprValueKind VK, 215 1.1 joerg SourceLocation LBracLoc, Expr *Receiver, Selector Sel, 216 1.1 joerg ArrayRef<SourceLocation> SelLocs, 217 1.1 joerg ObjCMethodDecl *Method, ArrayRef<Expr *> Args, 218 1.1 joerg SourceLocation RBracLoc, bool isImplicit) { 219 1.1 joerg assert((!SelLocs.empty() || isImplicit) && 220 1.1 joerg "No selector locs for non-implicit message"); 221 1.1 joerg ObjCMessageExpr *Mem; 222 1.1 joerg SelectorLocationsKind SelLocsK = SelectorLocationsKind(); 223 1.1 joerg if (isImplicit) 224 1.1 joerg Mem = alloc(Context, Args.size(), 0); 225 1.1 joerg else 226 1.1 joerg Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK); 227 1.1 joerg return new (Mem) 228 1.1 joerg ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel, SelLocs, SelLocsK, Method, 229 1.1 joerg Args, RBracLoc, isImplicit); 230 1.1 joerg } 231 1.1 joerg 232 1.1 joerg ObjCMessageExpr *ObjCMessageExpr::CreateEmpty(const ASTContext &Context, 233 1.1 joerg unsigned NumArgs, 234 1.1 joerg unsigned NumStoredSelLocs) { 235 1.1 joerg ObjCMessageExpr *Mem = alloc(Context, NumArgs, NumStoredSelLocs); 236 1.1 joerg return new (Mem) ObjCMessageExpr(EmptyShell(), NumArgs); 237 1.1 joerg } 238 1.1 joerg 239 1.1 joerg ObjCMessageExpr *ObjCMessageExpr::alloc(const ASTContext &C, 240 1.1 joerg ArrayRef<Expr *> Args, 241 1.1 joerg SourceLocation RBraceLoc, 242 1.1 joerg ArrayRef<SourceLocation> SelLocs, 243 1.1 joerg Selector Sel, 244 1.1 joerg SelectorLocationsKind &SelLocsK) { 245 1.1 joerg SelLocsK = hasStandardSelectorLocs(Sel, SelLocs, Args, RBraceLoc); 246 1.1 joerg unsigned NumStoredSelLocs = 247 1.1 joerg (SelLocsK == SelLoc_NonStandard) ? SelLocs.size() : 0; 248 1.1 joerg return alloc(C, Args.size(), NumStoredSelLocs); 249 1.1 joerg } 250 1.1 joerg 251 1.1 joerg ObjCMessageExpr *ObjCMessageExpr::alloc(const ASTContext &C, unsigned NumArgs, 252 1.1 joerg unsigned NumStoredSelLocs) { 253 1.1 joerg return (ObjCMessageExpr *)C.Allocate( 254 1.1 joerg totalSizeToAlloc<void *, SourceLocation>(NumArgs + 1, NumStoredSelLocs), 255 1.1 joerg alignof(ObjCMessageExpr)); 256 1.1 joerg } 257 1.1 joerg 258 1.1 joerg void ObjCMessageExpr::getSelectorLocs( 259 1.1 joerg SmallVectorImpl<SourceLocation> &SelLocs) const { 260 1.1 joerg for (unsigned i = 0, e = getNumSelectorLocs(); i != e; ++i) 261 1.1 joerg SelLocs.push_back(getSelectorLoc(i)); 262 1.1 joerg } 263 1.1 joerg 264 1.1 joerg 265 1.1 joerg QualType ObjCMessageExpr::getCallReturnType(ASTContext &Ctx) const { 266 1.1 joerg if (const ObjCMethodDecl *MD = getMethodDecl()) { 267 1.1 joerg QualType QT = MD->getReturnType(); 268 1.1 joerg if (QT == Ctx.getObjCInstanceType()) { 269 1.1 joerg // instancetype corresponds to expression types. 270 1.1 joerg return getType(); 271 1.1 joerg } 272 1.1 joerg return QT; 273 1.1 joerg } 274 1.1 joerg 275 1.1 joerg // Expression type might be different from an expected call return type, 276 1.1 joerg // as expression type would never be a reference even if call returns a 277 1.1 joerg // reference. Reconstruct the original expression type. 278 1.1 joerg QualType QT = getType(); 279 1.1 joerg switch (getValueKind()) { 280 1.1 joerg case VK_LValue: 281 1.1 joerg return Ctx.getLValueReferenceType(QT); 282 1.1 joerg case VK_XValue: 283 1.1 joerg return Ctx.getRValueReferenceType(QT); 284 1.1 joerg case VK_RValue: 285 1.1 joerg return QT; 286 1.1 joerg } 287 1.1 joerg llvm_unreachable("Unsupported ExprValueKind"); 288 1.1 joerg } 289 1.1 joerg 290 1.1 joerg SourceRange ObjCMessageExpr::getReceiverRange() const { 291 1.1 joerg switch (getReceiverKind()) { 292 1.1 joerg case Instance: 293 1.1 joerg return getInstanceReceiver()->getSourceRange(); 294 1.1 joerg 295 1.1 joerg case Class: 296 1.1 joerg return getClassReceiverTypeInfo()->getTypeLoc().getSourceRange(); 297 1.1 joerg 298 1.1 joerg case SuperInstance: 299 1.1 joerg case SuperClass: 300 1.1 joerg return getSuperLoc(); 301 1.1 joerg } 302 1.1 joerg 303 1.1 joerg llvm_unreachable("Invalid ReceiverKind!"); 304 1.1 joerg } 305 1.1 joerg 306 1.1 joerg Selector ObjCMessageExpr::getSelector() const { 307 1.1 joerg if (HasMethod) 308 1.1 joerg return reinterpret_cast<const ObjCMethodDecl *>(SelectorOrMethod) 309 1.1 joerg ->getSelector(); 310 1.1 joerg return Selector(SelectorOrMethod); 311 1.1 joerg } 312 1.1 joerg 313 1.1 joerg QualType ObjCMessageExpr::getReceiverType() const { 314 1.1 joerg switch (getReceiverKind()) { 315 1.1 joerg case Instance: 316 1.1 joerg return getInstanceReceiver()->getType(); 317 1.1 joerg case Class: 318 1.1 joerg return getClassReceiver(); 319 1.1 joerg case SuperInstance: 320 1.1 joerg case SuperClass: 321 1.1 joerg return getSuperType(); 322 1.1 joerg } 323 1.1 joerg 324 1.1 joerg llvm_unreachable("unexpected receiver kind"); 325 1.1 joerg } 326 1.1 joerg 327 1.1 joerg ObjCInterfaceDecl *ObjCMessageExpr::getReceiverInterface() const { 328 1.1 joerg QualType T = getReceiverType(); 329 1.1 joerg 330 1.1 joerg if (const ObjCObjectPointerType *Ptr = T->getAs<ObjCObjectPointerType>()) 331 1.1 joerg return Ptr->getInterfaceDecl(); 332 1.1 joerg 333 1.1 joerg if (const ObjCObjectType *Ty = T->getAs<ObjCObjectType>()) 334 1.1 joerg return Ty->getInterface(); 335 1.1 joerg 336 1.1 joerg return nullptr; 337 1.1 joerg } 338 1.1 joerg 339 1.1 joerg Stmt::child_range ObjCMessageExpr::children() { 340 1.1 joerg Stmt **begin; 341 1.1 joerg if (getReceiverKind() == Instance) 342 1.1 joerg begin = reinterpret_cast<Stmt **>(getTrailingObjects<void *>()); 343 1.1 joerg else 344 1.1 joerg begin = reinterpret_cast<Stmt **>(getArgs()); 345 1.1 joerg return child_range(begin, 346 1.1 joerg reinterpret_cast<Stmt **>(getArgs() + getNumArgs())); 347 1.1 joerg } 348 1.1 joerg 349 1.1 joerg Stmt::const_child_range ObjCMessageExpr::children() const { 350 1.1 joerg auto Children = const_cast<ObjCMessageExpr *>(this)->children(); 351 1.1 joerg return const_child_range(Children.begin(), Children.end()); 352 1.1 joerg } 353 1.1 joerg 354 1.1 joerg StringRef ObjCBridgedCastExpr::getBridgeKindName() const { 355 1.1 joerg switch (getBridgeKind()) { 356 1.1 joerg case OBC_Bridge: 357 1.1 joerg return "__bridge"; 358 1.1 joerg case OBC_BridgeTransfer: 359 1.1 joerg return "__bridge_transfer"; 360 1.1 joerg case OBC_BridgeRetained: 361 1.1 joerg return "__bridge_retained"; 362 1.1 joerg } 363 1.1 joerg 364 1.1 joerg llvm_unreachable("Invalid BridgeKind!"); 365 1.1 joerg } 366