Bug Summary

File:clang/lib/AST/DeclObjC.cpp
Warning:line 1659, column 11
Called C++ object pointer is null

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name DeclObjC.cpp -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -analyzer-config-compatibility-mode=true -mrelocation-model pic -pic-level 2 -mframe-pointer=none -relaxed-aliasing -fmath-errno -fno-rounding-math -mconstructor-aliases -funwind-tables=2 -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -ffunction-sections -fdata-sections -fcoverage-compilation-dir=/build/llvm-toolchain-snapshot-14~++20211016100712+8e1d532707fd/build-llvm -resource-dir /usr/lib/llvm-14/lib/clang/14.0.0 -D CLANG_ROUND_TRIP_CC1_ARGS=ON -D _DEBUG -D _GNU_SOURCE -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -I tools/clang/lib/AST -I /build/llvm-toolchain-snapshot-14~++20211016100712+8e1d532707fd/clang/lib/AST -I /build/llvm-toolchain-snapshot-14~++20211016100712+8e1d532707fd/clang/include -I tools/clang/include -I include -I /build/llvm-toolchain-snapshot-14~++20211016100712+8e1d532707fd/llvm/include -D NDEBUG -U NDEBUG -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/x86_64-linux-gnu/c++/10 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/backward -internal-isystem /usr/lib/llvm-14/lib/clang/14.0.0/include -internal-isystem /usr/local/include -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/10/../../../../x86_64-linux-gnu/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -O2 -Wno-unused-command-line-argument -Wno-unknown-warning-option -Wno-unused-parameter -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wno-maybe-uninitialized -Wno-class-memaccess -Wno-redundant-move -Wno-pessimizing-move -Wno-noexcept-type -Wno-comment -std=c++14 -fdeprecated-macro -fdebug-compilation-dir=/build/llvm-toolchain-snapshot-14~++20211016100712+8e1d532707fd/build-llvm -ferror-limit 19 -fvisibility-inlines-hidden -fgnuc-version=4.2.1 -fcolor-diagnostics -vectorize-loops -vectorize-slp -analyzer-output=html -analyzer-config stable-report-filename=true -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /tmp/scan-build-2021-10-17-004846-21170-1 -x c++ /build/llvm-toolchain-snapshot-14~++20211016100712+8e1d532707fd/clang/lib/AST/DeclObjC.cpp

/build/llvm-toolchain-snapshot-14~++20211016100712+8e1d532707fd/clang/lib/AST/DeclObjC.cpp

1//===- DeclObjC.cpp - ObjC Declaration AST Node Implementation ------------===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8//
9// This file implements the Objective-C related Decl classes.
10//
11//===----------------------------------------------------------------------===//
12
13#include "clang/AST/DeclObjC.h"
14#include "clang/AST/ASTContext.h"
15#include "clang/AST/ASTMutationListener.h"
16#include "clang/AST/Attr.h"
17#include "clang/AST/Decl.h"
18#include "clang/AST/DeclBase.h"
19#include "clang/AST/Stmt.h"
20#include "clang/AST/Type.h"
21#include "clang/AST/TypeLoc.h"
22#include "clang/Basic/IdentifierTable.h"
23#include "clang/Basic/LLVM.h"
24#include "clang/Basic/LangOptions.h"
25#include "clang/Basic/SourceLocation.h"
26#include "llvm/ADT/None.h"
27#include "llvm/ADT/SmallString.h"
28#include "llvm/ADT/SmallVector.h"
29#include "llvm/Support/Casting.h"
30#include "llvm/Support/ErrorHandling.h"
31#include "llvm/Support/raw_ostream.h"
32#include <algorithm>
33#include <cassert>
34#include <cstdint>
35#include <cstring>
36#include <queue>
37#include <utility>
38
39using namespace clang;
40
41//===----------------------------------------------------------------------===//
42// ObjCListBase
43//===----------------------------------------------------------------------===//
44
45void ObjCListBase::set(void *const* InList, unsigned Elts, ASTContext &Ctx) {
46 List = nullptr;
47 if (Elts == 0) return; // Setting to an empty list is a noop.
48
49 List = new (Ctx) void*[Elts];
50 NumElts = Elts;
51 memcpy(List, InList, sizeof(void*)*Elts);
52}
53
54void ObjCProtocolList::set(ObjCProtocolDecl* const* InList, unsigned Elts,
55 const SourceLocation *Locs, ASTContext &Ctx) {
56 if (Elts == 0)
57 return;
58
59 Locations = new (Ctx) SourceLocation[Elts];
60 memcpy(Locations, Locs, sizeof(SourceLocation) * Elts);
61 set(InList, Elts, Ctx);
62}
63
64//===----------------------------------------------------------------------===//
65// ObjCInterfaceDecl
66//===----------------------------------------------------------------------===//
67
68ObjCContainerDecl::ObjCContainerDecl(Kind DK, DeclContext *DC,
69 IdentifierInfo *Id, SourceLocation nameLoc,
70 SourceLocation atStartLoc)
71 : NamedDecl(DK, DC, nameLoc, Id), DeclContext(DK) {
72 setAtStartLoc(atStartLoc);
73}
74
75void ObjCContainerDecl::anchor() {}
76
77/// getIvarDecl - This method looks up an ivar in this ContextDecl.
78///
79ObjCIvarDecl *
80ObjCContainerDecl::getIvarDecl(IdentifierInfo *Id) const {
81 lookup_result R = lookup(Id);
82 for (lookup_iterator Ivar = R.begin(), IvarEnd = R.end();
83 Ivar != IvarEnd; ++Ivar) {
84 if (auto *ivar = dyn_cast<ObjCIvarDecl>(*Ivar))
85 return ivar;
86 }
87 return nullptr;
88}
89
90// Get the local instance/class method declared in this interface.
91ObjCMethodDecl *
92ObjCContainerDecl::getMethod(Selector Sel, bool isInstance,
93 bool AllowHidden) const {
94 // If this context is a hidden protocol definition, don't find any
95 // methods there.
96 if (const auto *Proto = dyn_cast<ObjCProtocolDecl>(this)) {
97 if (const ObjCProtocolDecl *Def = Proto->getDefinition())
98 if (!Def->isUnconditionallyVisible() && !AllowHidden)
99 return nullptr;
100 }
101
102 // Since instance & class methods can have the same name, the loop below
103 // ensures we get the correct method.
104 //
105 // @interface Whatever
106 // - (int) class_method;
107 // + (float) class_method;
108 // @end
109 lookup_result R = lookup(Sel);
110 for (lookup_iterator Meth = R.begin(), MethEnd = R.end();
111 Meth != MethEnd; ++Meth) {
112 auto *MD = dyn_cast<ObjCMethodDecl>(*Meth);
113 if (MD && MD->isInstanceMethod() == isInstance)
114 return MD;
115 }
116 return nullptr;
117}
118
119/// This routine returns 'true' if a user declared setter method was
120/// found in the class, its protocols, its super classes or categories.
121/// It also returns 'true' if one of its categories has declared a 'readwrite'
122/// property. This is because, user must provide a setter method for the
123/// category's 'readwrite' property.
124bool ObjCContainerDecl::HasUserDeclaredSetterMethod(
125 const ObjCPropertyDecl *Property) const {
126 Selector Sel = Property->getSetterName();
127 lookup_result R = lookup(Sel);
128 for (lookup_iterator Meth = R.begin(), MethEnd = R.end();
129 Meth != MethEnd; ++Meth) {
130 auto *MD = dyn_cast<ObjCMethodDecl>(*Meth);
131 if (MD && MD->isInstanceMethod() && !MD->isImplicit())
132 return true;
133 }
134
135 if (const auto *ID = dyn_cast<ObjCInterfaceDecl>(this)) {
136 // Also look into categories, including class extensions, looking
137 // for a user declared instance method.
138 for (const auto *Cat : ID->visible_categories()) {
139 if (ObjCMethodDecl *MD = Cat->getInstanceMethod(Sel))
140 if (!MD->isImplicit())
141 return true;
142 if (Cat->IsClassExtension())
143 continue;
144 // Also search through the categories looking for a 'readwrite'
145 // declaration of this property. If one found, presumably a setter will
146 // be provided (properties declared in categories will not get
147 // auto-synthesized).
148 for (const auto *P : Cat->properties())
149 if (P->getIdentifier() == Property->getIdentifier()) {
150 if (P->getPropertyAttributes() &
151 ObjCPropertyAttribute::kind_readwrite)
152 return true;
153 break;
154 }
155 }
156
157 // Also look into protocols, for a user declared instance method.
158 for (const auto *Proto : ID->all_referenced_protocols())
159 if (Proto->HasUserDeclaredSetterMethod(Property))
160 return true;
161
162 // And in its super class.
163 ObjCInterfaceDecl *OSC = ID->getSuperClass();
164 while (OSC) {
165 if (OSC->HasUserDeclaredSetterMethod(Property))
166 return true;
167 OSC = OSC->getSuperClass();
168 }
169 }
170 if (const auto *PD = dyn_cast<ObjCProtocolDecl>(this))
171 for (const auto *PI : PD->protocols())
172 if (PI->HasUserDeclaredSetterMethod(Property))
173 return true;
174 return false;
175}
176
177ObjCPropertyDecl *
178ObjCPropertyDecl::findPropertyDecl(const DeclContext *DC,
179 const IdentifierInfo *propertyID,
180 ObjCPropertyQueryKind queryKind) {
181 // If this context is a hidden protocol definition, don't find any
182 // property.
183 if (const auto *Proto = dyn_cast<ObjCProtocolDecl>(DC)) {
184 if (const ObjCProtocolDecl *Def = Proto->getDefinition())
185 if (!Def->isUnconditionallyVisible())
186 return nullptr;
187 }
188
189 // If context is class, then lookup property in its visible extensions.
190 // This comes before property is looked up in primary class.
191 if (auto *IDecl = dyn_cast<ObjCInterfaceDecl>(DC)) {
192 for (const auto *Ext : IDecl->visible_extensions())
193 if (ObjCPropertyDecl *PD = ObjCPropertyDecl::findPropertyDecl(Ext,
194 propertyID,
195 queryKind))
196 return PD;
197 }
198
199 DeclContext::lookup_result R = DC->lookup(propertyID);
200 ObjCPropertyDecl *classProp = nullptr;
201 for (DeclContext::lookup_iterator I = R.begin(), E = R.end(); I != E;
202 ++I)
203 if (auto *PD = dyn_cast<ObjCPropertyDecl>(*I)) {
204 // If queryKind is unknown, we return the instance property if one
205 // exists; otherwise we return the class property.
206 if ((queryKind == ObjCPropertyQueryKind::OBJC_PR_query_unknown &&
207 !PD->isClassProperty()) ||
208 (queryKind == ObjCPropertyQueryKind::OBJC_PR_query_class &&
209 PD->isClassProperty()) ||
210 (queryKind == ObjCPropertyQueryKind::OBJC_PR_query_instance &&
211 !PD->isClassProperty()))
212 return PD;
213
214 if (PD->isClassProperty())
215 classProp = PD;
216 }
217
218 if (queryKind == ObjCPropertyQueryKind::OBJC_PR_query_unknown)
219 // We can't find the instance property, return the class property.
220 return classProp;
221
222 return nullptr;
223}
224
225IdentifierInfo *
226ObjCPropertyDecl::getDefaultSynthIvarName(ASTContext &Ctx) const {
227 SmallString<128> ivarName;
228 {
229 llvm::raw_svector_ostream os(ivarName);
230 os << '_' << getIdentifier()->getName();
231 }
232 return &Ctx.Idents.get(ivarName.str());
233}
234
235/// FindPropertyDeclaration - Finds declaration of the property given its name
236/// in 'PropertyId' and returns it. It returns 0, if not found.
237ObjCPropertyDecl *ObjCContainerDecl::FindPropertyDeclaration(
238 const IdentifierInfo *PropertyId,
239 ObjCPropertyQueryKind QueryKind) const {
240 // Don't find properties within hidden protocol definitions.
241 if (const auto *Proto = dyn_cast<ObjCProtocolDecl>(this)) {
242 if (const ObjCProtocolDecl *Def = Proto->getDefinition())
243 if (!Def->isUnconditionallyVisible())
244 return nullptr;
245 }
246
247 // Search the extensions of a class first; they override what's in
248 // the class itself.
249 if (const auto *ClassDecl = dyn_cast<ObjCInterfaceDecl>(this)) {
250 for (const auto *Ext : ClassDecl->visible_extensions()) {
251 if (auto *P = Ext->FindPropertyDeclaration(PropertyId, QueryKind))
252 return P;
253 }
254 }
255
256 if (ObjCPropertyDecl *PD =
257 ObjCPropertyDecl::findPropertyDecl(cast<DeclContext>(this), PropertyId,
258 QueryKind))
259 return PD;
260
261 switch (getKind()) {
262 default:
263 break;
264 case Decl::ObjCProtocol: {
265 const auto *PID = cast<ObjCProtocolDecl>(this);
266 for (const auto *I : PID->protocols())
267 if (ObjCPropertyDecl *P = I->FindPropertyDeclaration(PropertyId,
268 QueryKind))
269 return P;
270 break;
271 }
272 case Decl::ObjCInterface: {
273 const auto *OID = cast<ObjCInterfaceDecl>(this);
274 // Look through categories (but not extensions; they were handled above).
275 for (const auto *Cat : OID->visible_categories()) {
276 if (!Cat->IsClassExtension())
277 if (ObjCPropertyDecl *P = Cat->FindPropertyDeclaration(
278 PropertyId, QueryKind))
279 return P;
280 }
281
282 // Look through protocols.
283 for (const auto *I : OID->all_referenced_protocols())
284 if (ObjCPropertyDecl *P = I->FindPropertyDeclaration(PropertyId,
285 QueryKind))
286 return P;
287
288 // Finally, check the super class.
289 if (const ObjCInterfaceDecl *superClass = OID->getSuperClass())
290 return superClass->FindPropertyDeclaration(PropertyId, QueryKind);
291 break;
292 }
293 case Decl::ObjCCategory: {
294 const auto *OCD = cast<ObjCCategoryDecl>(this);
295 // Look through protocols.
296 if (!OCD->IsClassExtension())
297 for (const auto *I : OCD->protocols())
298 if (ObjCPropertyDecl *P = I->FindPropertyDeclaration(PropertyId,
299 QueryKind))
300 return P;
301 break;
302 }
303 }
304 return nullptr;
305}
306
307void ObjCInterfaceDecl::anchor() {}
308
309ObjCTypeParamList *ObjCInterfaceDecl::getTypeParamList() const {
310 // If this particular declaration has a type parameter list, return it.
311 if (ObjCTypeParamList *written = getTypeParamListAsWritten())
312 return written;
313
314 // If there is a definition, return its type parameter list.
315 if (const ObjCInterfaceDecl *def = getDefinition())
316 return def->getTypeParamListAsWritten();
317
318 // Otherwise, look at previous declarations to determine whether any
319 // of them has a type parameter list, skipping over those
320 // declarations that do not.
321 for (const ObjCInterfaceDecl *decl = getMostRecentDecl(); decl;
322 decl = decl->getPreviousDecl()) {
323 if (ObjCTypeParamList *written = decl->getTypeParamListAsWritten())
324 return written;
325 }
326
327 return nullptr;
328}
329
330void ObjCInterfaceDecl::setTypeParamList(ObjCTypeParamList *TPL) {
331 TypeParamList = TPL;
332 if (!TPL)
333 return;
334 // Set the declaration context of each of the type parameters.
335 for (auto *typeParam : *TypeParamList)
336 typeParam->setDeclContext(this);
337}
338
339ObjCInterfaceDecl *ObjCInterfaceDecl::getSuperClass() const {
340 // FIXME: Should make sure no callers ever do this.
341 if (!hasDefinition())
342 return nullptr;
343
344 if (data().ExternallyCompleted)
345 LoadExternalDefinition();
346
347 if (const ObjCObjectType *superType = getSuperClassType()) {
348 if (ObjCInterfaceDecl *superDecl = superType->getInterface()) {
349 if (ObjCInterfaceDecl *superDef = superDecl->getDefinition())
350 return superDef;
351
352 return superDecl;
353 }
354 }
355
356 return nullptr;
357}
358
359SourceLocation ObjCInterfaceDecl::getSuperClassLoc() const {
360 if (TypeSourceInfo *superTInfo = getSuperClassTInfo())
361 return superTInfo->getTypeLoc().getBeginLoc();
362
363 return SourceLocation();
364}
365
366/// FindPropertyVisibleInPrimaryClass - Finds declaration of the property
367/// with name 'PropertyId' in the primary class; including those in protocols
368/// (direct or indirect) used by the primary class.
369ObjCPropertyDecl *
370ObjCInterfaceDecl::FindPropertyVisibleInPrimaryClass(
371 IdentifierInfo *PropertyId,
372 ObjCPropertyQueryKind QueryKind) const {
373 // FIXME: Should make sure no callers ever do this.
374 if (!hasDefinition())
375 return nullptr;
376
377 if (data().ExternallyCompleted)
378 LoadExternalDefinition();
379
380 if (ObjCPropertyDecl *PD =
381 ObjCPropertyDecl::findPropertyDecl(cast<DeclContext>(this), PropertyId,
382 QueryKind))
383 return PD;
384
385 // Look through protocols.
386 for (const auto *I : all_referenced_protocols())
387 if (ObjCPropertyDecl *P = I->FindPropertyDeclaration(PropertyId,
388 QueryKind))
389 return P;
390
391 return nullptr;
392}
393
394void ObjCInterfaceDecl::collectPropertiesToImplement(PropertyMap &PM,
395 PropertyDeclOrder &PO) const {
396 for (auto *Prop : properties()) {
397 PM[std::make_pair(Prop->getIdentifier(), Prop->isClassProperty())] = Prop;
398 PO.push_back(Prop);
399 }
400 for (const auto *Ext : known_extensions()) {
401 const ObjCCategoryDecl *ClassExt = Ext;
402 for (auto *Prop : ClassExt->properties()) {
403 PM[std::make_pair(Prop->getIdentifier(), Prop->isClassProperty())] = Prop;
404 PO.push_back(Prop);
405 }
406 }
407 for (const auto *PI : all_referenced_protocols())
408 PI->collectPropertiesToImplement(PM, PO);
409 // Note, the properties declared only in class extensions are still copied
410 // into the main @interface's property list, and therefore we don't
411 // explicitly, have to search class extension properties.
412}
413
414bool ObjCInterfaceDecl::isArcWeakrefUnavailable() const {
415 const ObjCInterfaceDecl *Class = this;
416 while (Class) {
417 if (Class->hasAttr<ArcWeakrefUnavailableAttr>())
418 return true;
419 Class = Class->getSuperClass();
420 }
421 return false;
422}
423
424const ObjCInterfaceDecl *ObjCInterfaceDecl::isObjCRequiresPropertyDefs() const {
425 const ObjCInterfaceDecl *Class = this;
426 while (Class) {
427 if (Class->hasAttr<ObjCRequiresPropertyDefsAttr>())
428 return Class;
429 Class = Class->getSuperClass();
430 }
431 return nullptr;
432}
433
434void ObjCInterfaceDecl::mergeClassExtensionProtocolList(
435 ObjCProtocolDecl *const* ExtList, unsigned ExtNum,
436 ASTContext &C) {
437 if (data().ExternallyCompleted)
438 LoadExternalDefinition();
439
440 if (data().AllReferencedProtocols.empty() &&
441 data().ReferencedProtocols.empty()) {
442 data().AllReferencedProtocols.set(ExtList, ExtNum, C);
443 return;
444 }
445
446 // Check for duplicate protocol in class's protocol list.
447 // This is O(n*m). But it is extremely rare and number of protocols in
448 // class or its extension are very few.
449 SmallVector<ObjCProtocolDecl *, 8> ProtocolRefs;
450 for (unsigned i = 0; i < ExtNum; i++) {
451 bool protocolExists = false;
452 ObjCProtocolDecl *ProtoInExtension = ExtList[i];
453 for (auto *Proto : all_referenced_protocols()) {
454 if (C.ProtocolCompatibleWithProtocol(ProtoInExtension, Proto)) {
455 protocolExists = true;
456 break;
457 }
458 }
459 // Do we want to warn on a protocol in extension class which
460 // already exist in the class? Probably not.
461 if (!protocolExists)
462 ProtocolRefs.push_back(ProtoInExtension);
463 }
464
465 if (ProtocolRefs.empty())
466 return;
467
468 // Merge ProtocolRefs into class's protocol list;
469 ProtocolRefs.append(all_referenced_protocol_begin(),
470 all_referenced_protocol_end());
471
472 data().AllReferencedProtocols.set(ProtocolRefs.data(), ProtocolRefs.size(),C);
473}
474
475const ObjCInterfaceDecl *
476ObjCInterfaceDecl::findInterfaceWithDesignatedInitializers() const {
477 const ObjCInterfaceDecl *IFace = this;
478 while (IFace) {
479 if (IFace->hasDesignatedInitializers())
480 return IFace;
481 if (!IFace->inheritsDesignatedInitializers())
482 break;
483 IFace = IFace->getSuperClass();
484 }
485 return nullptr;
486}
487
488static bool isIntroducingInitializers(const ObjCInterfaceDecl *D) {
489 for (const auto *MD : D->instance_methods()) {
490 if (MD->getMethodFamily() == OMF_init && !MD->isOverriding())
491 return true;
492 }
493 for (const auto *Ext : D->visible_extensions()) {
494 for (const auto *MD : Ext->instance_methods()) {
495 if (MD->getMethodFamily() == OMF_init && !MD->isOverriding())
496 return true;
497 }
498 }
499 if (const auto *ImplD = D->getImplementation()) {
500 for (const auto *MD : ImplD->instance_methods()) {
501 if (MD->getMethodFamily() == OMF_init && !MD->isOverriding())
502 return true;
503 }
504 }
505 return false;
506}
507
508bool ObjCInterfaceDecl::inheritsDesignatedInitializers() const {
509 switch (data().InheritedDesignatedInitializers) {
510 case DefinitionData::IDI_Inherited:
511 return true;
512 case DefinitionData::IDI_NotInherited:
513 return false;
514 case DefinitionData::IDI_Unknown:
515 // If the class introduced initializers we conservatively assume that we
516 // don't know if any of them is a designated initializer to avoid possible
517 // misleading warnings.
518 if (isIntroducingInitializers(this)) {
519 data().InheritedDesignatedInitializers = DefinitionData::IDI_NotInherited;
520 } else {
521 if (auto SuperD = getSuperClass()) {
522 data().InheritedDesignatedInitializers =
523 SuperD->declaresOrInheritsDesignatedInitializers() ?
524 DefinitionData::IDI_Inherited :
525 DefinitionData::IDI_NotInherited;
526 } else {
527 data().InheritedDesignatedInitializers =
528 DefinitionData::IDI_NotInherited;
529 }
530 }
531 assert(data().InheritedDesignatedInitializers(static_cast <bool> (data().InheritedDesignatedInitializers
!= DefinitionData::IDI_Unknown) ? void (0) : __assert_fail (
"data().InheritedDesignatedInitializers != DefinitionData::IDI_Unknown"
, "/build/llvm-toolchain-snapshot-14~++20211016100712+8e1d532707fd/clang/lib/AST/DeclObjC.cpp"
, 532, __extension__ __PRETTY_FUNCTION__))
532 != DefinitionData::IDI_Unknown)(static_cast <bool> (data().InheritedDesignatedInitializers
!= DefinitionData::IDI_Unknown) ? void (0) : __assert_fail (
"data().InheritedDesignatedInitializers != DefinitionData::IDI_Unknown"
, "/build/llvm-toolchain-snapshot-14~++20211016100712+8e1d532707fd/clang/lib/AST/DeclObjC.cpp"
, 532, __extension__ __PRETTY_FUNCTION__))
;
533 return data().InheritedDesignatedInitializers ==
534 DefinitionData::IDI_Inherited;
535 }
536
537 llvm_unreachable("unexpected InheritedDesignatedInitializers value")::llvm::llvm_unreachable_internal("unexpected InheritedDesignatedInitializers value"
, "/build/llvm-toolchain-snapshot-14~++20211016100712+8e1d532707fd/clang/lib/AST/DeclObjC.cpp"
, 537)
;
538}
539
540void ObjCInterfaceDecl::getDesignatedInitializers(
541 llvm::SmallVectorImpl<const ObjCMethodDecl *> &Methods) const {
542 // Check for a complete definition and recover if not so.
543 if (!isThisDeclarationADefinition())
544 return;
545 if (data().ExternallyCompleted)
546 LoadExternalDefinition();
547
548 const ObjCInterfaceDecl *IFace= findInterfaceWithDesignatedInitializers();
549 if (!IFace)
550 return;
551
552 for (const auto *MD : IFace->instance_methods())
553 if (MD->isThisDeclarationADesignatedInitializer())
554 Methods.push_back(MD);
555 for (const auto *Ext : IFace->visible_extensions()) {
556 for (const auto *MD : Ext->instance_methods())
557 if (MD->isThisDeclarationADesignatedInitializer())
558 Methods.push_back(MD);
559 }
560}
561
562bool ObjCInterfaceDecl::isDesignatedInitializer(Selector Sel,
563 const ObjCMethodDecl **InitMethod) const {
564 bool HasCompleteDef = isThisDeclarationADefinition();
565 // During deserialization the data record for the ObjCInterfaceDecl could
566 // be made invariant by reusing the canonical decl. Take this into account
567 // when checking for the complete definition.
568 if (!HasCompleteDef && getCanonicalDecl()->hasDefinition() &&
569 getCanonicalDecl()->getDefinition() == getDefinition())
570 HasCompleteDef = true;
571
572 // Check for a complete definition and recover if not so.
573 if (!HasCompleteDef)
574 return false;
575
576 if (data().ExternallyCompleted)
577 LoadExternalDefinition();
578
579 const ObjCInterfaceDecl *IFace= findInterfaceWithDesignatedInitializers();
580 if (!IFace)
581 return false;
582
583 if (const ObjCMethodDecl *MD = IFace->getInstanceMethod(Sel)) {
584 if (MD->isThisDeclarationADesignatedInitializer()) {
585 if (InitMethod)
586 *InitMethod = MD;
587 return true;
588 }
589 }
590 for (const auto *Ext : IFace->visible_extensions()) {
591 if (const ObjCMethodDecl *MD = Ext->getInstanceMethod(Sel)) {
592 if (MD->isThisDeclarationADesignatedInitializer()) {
593 if (InitMethod)
594 *InitMethod = MD;
595 return true;
596 }
597 }
598 }
599 return false;
600}
601
602void ObjCInterfaceDecl::allocateDefinitionData() {
603 assert(!hasDefinition() && "ObjC class already has a definition")(static_cast <bool> (!hasDefinition() && "ObjC class already has a definition"
) ? void (0) : __assert_fail ("!hasDefinition() && \"ObjC class already has a definition\""
, "/build/llvm-toolchain-snapshot-14~++20211016100712+8e1d532707fd/clang/lib/AST/DeclObjC.cpp"
, 603, __extension__ __PRETTY_FUNCTION__))
;
604 Data.setPointer(new (getASTContext()) DefinitionData());
605 Data.getPointer()->Definition = this;
606
607 // Make the type point at the definition, now that we have one.
608 if (TypeForDecl)
609 cast<ObjCInterfaceType>(TypeForDecl)->Decl = this;
610}
611
612void ObjCInterfaceDecl::startDefinition() {
613 allocateDefinitionData();
614
615 // Update all of the declarations with a pointer to the definition.
616 for (auto *RD : redecls()) {
617 if (RD != this)
618 RD->Data = Data;
619 }
620}
621
622ObjCIvarDecl *ObjCInterfaceDecl::lookupInstanceVariable(IdentifierInfo *ID,
623 ObjCInterfaceDecl *&clsDeclared) {
624 // FIXME: Should make sure no callers ever do this.
625 if (!hasDefinition())
626 return nullptr;
627
628 if (data().ExternallyCompleted)
629 LoadExternalDefinition();
630
631 ObjCInterfaceDecl* ClassDecl = this;
632 while (ClassDecl != nullptr) {
633 if (ObjCIvarDecl *I = ClassDecl->getIvarDecl(ID)) {
634 clsDeclared = ClassDecl;
635 return I;
636 }
637
638 for (const auto *Ext : ClassDecl->visible_extensions()) {
639 if (ObjCIvarDecl *I = Ext->getIvarDecl(ID)) {
640 clsDeclared = ClassDecl;
641 return I;
642 }
643 }
644
645 ClassDecl = ClassDecl->getSuperClass();
646 }
647 return nullptr;
648}
649
650/// lookupInheritedClass - This method returns ObjCInterfaceDecl * of the super
651/// class whose name is passed as argument. If it is not one of the super classes
652/// the it returns NULL.
653ObjCInterfaceDecl *ObjCInterfaceDecl::lookupInheritedClass(
654 const IdentifierInfo*ICName) {
655 // FIXME: Should make sure no callers ever do this.
656 if (!hasDefinition())
657 return nullptr;
658
659 if (data().ExternallyCompleted)
660 LoadExternalDefinition();
661
662 ObjCInterfaceDecl* ClassDecl = this;
663 while (ClassDecl != nullptr) {
664 if (ClassDecl->getIdentifier() == ICName)
665 return ClassDecl;
666 ClassDecl = ClassDecl->getSuperClass();
667 }
668 return nullptr;
669}
670
671ObjCProtocolDecl *
672ObjCInterfaceDecl::lookupNestedProtocol(IdentifierInfo *Name) {
673 for (auto *P : all_referenced_protocols())
674 if (P->lookupProtocolNamed(Name))
675 return P;
676 ObjCInterfaceDecl *SuperClass = getSuperClass();
677 return SuperClass ? SuperClass->lookupNestedProtocol(Name) : nullptr;
678}
679
680/// lookupMethod - This method returns an instance/class method by looking in
681/// the class, its categories, and its super classes (using a linear search).
682/// When argument category "C" is specified, any implicit method found
683/// in this category is ignored.
684ObjCMethodDecl *ObjCInterfaceDecl::lookupMethod(Selector Sel,
685 bool isInstance,
686 bool shallowCategoryLookup,
687 bool followSuper,
688 const ObjCCategoryDecl *C) const
689{
690 // FIXME: Should make sure no callers ever do this.
691 if (!hasDefinition())
692 return nullptr;
693
694 const ObjCInterfaceDecl* ClassDecl = this;
695 ObjCMethodDecl *MethodDecl = nullptr;
696
697 if (data().ExternallyCompleted)
698 LoadExternalDefinition();
699
700 while (ClassDecl) {
701 // 1. Look through primary class.
702 if ((MethodDecl = ClassDecl->getMethod(Sel, isInstance)))
703 return MethodDecl;
704
705 // 2. Didn't find one yet - now look through categories.
706 for (const auto *Cat : ClassDecl->visible_categories())
707 if ((MethodDecl = Cat->getMethod(Sel, isInstance)))
708 if (C != Cat || !MethodDecl->isImplicit())
709 return MethodDecl;
710
711 // 3. Didn't find one yet - look through primary class's protocols.
712 for (const auto *I : ClassDecl->protocols())
713 if ((MethodDecl = I->lookupMethod(Sel, isInstance)))
714 return MethodDecl;
715
716 // 4. Didn't find one yet - now look through categories' protocols
717 if (!shallowCategoryLookup)
718 for (const auto *Cat : ClassDecl->visible_categories()) {
719 // Didn't find one yet - look through protocols.
720 const ObjCList<ObjCProtocolDecl> &Protocols =
721 Cat->getReferencedProtocols();
722 for (auto *Protocol : Protocols)
723 if ((MethodDecl = Protocol->lookupMethod(Sel, isInstance)))
724 if (C != Cat || !MethodDecl->isImplicit())
725 return MethodDecl;
726 }
727
728
729 if (!followSuper)
730 return nullptr;
731
732 // 5. Get to the super class (if any).
733 ClassDecl = ClassDecl->getSuperClass();
734 }
735 return nullptr;
736}
737
738// Will search "local" class/category implementations for a method decl.
739// If failed, then we search in class's root for an instance method.
740// Returns 0 if no method is found.
741ObjCMethodDecl *ObjCInterfaceDecl::lookupPrivateMethod(
742 const Selector &Sel,
743 bool Instance) const {
744 // FIXME: Should make sure no callers ever do this.
745 if (!hasDefinition())
746 return nullptr;
747
748 if (data().ExternallyCompleted)
749 LoadExternalDefinition();
750
751 ObjCMethodDecl *Method = nullptr;
752 if (ObjCImplementationDecl *ImpDecl = getImplementation())
753 Method = Instance ? ImpDecl->getInstanceMethod(Sel)
754 : ImpDecl->getClassMethod(Sel);
755
756 // Look through local category implementations associated with the class.
757 if (!Method)
758 Method = getCategoryMethod(Sel, Instance);
759
760 // Before we give up, check if the selector is an instance method.
761 // But only in the root. This matches gcc's behavior and what the
762 // runtime expects.
763 if (!Instance && !Method && !getSuperClass()) {
764 Method = lookupInstanceMethod(Sel);
765 // Look through local category implementations associated
766 // with the root class.
767 if (!Method)
768 Method = lookupPrivateMethod(Sel, true);
769 }
770
771 if (!Method && getSuperClass())
772 return getSuperClass()->lookupPrivateMethod(Sel, Instance);
773 return Method;
774}
775
776//===----------------------------------------------------------------------===//
777// ObjCMethodDecl
778//===----------------------------------------------------------------------===//
779
780ObjCMethodDecl::ObjCMethodDecl(
781 SourceLocation beginLoc, SourceLocation endLoc, Selector SelInfo,
782 QualType T, TypeSourceInfo *ReturnTInfo, DeclContext *contextDecl,
783 bool isInstance, bool isVariadic, bool isPropertyAccessor,
784 bool isSynthesizedAccessorStub, bool isImplicitlyDeclared, bool isDefined,
785 ImplementationControl impControl, bool HasRelatedResultType)
786 : NamedDecl(ObjCMethod, contextDecl, beginLoc, SelInfo),
787 DeclContext(ObjCMethod), MethodDeclType(T), ReturnTInfo(ReturnTInfo),
788 DeclEndLoc(endLoc) {
789
790 // Initialized the bits stored in DeclContext.
791 ObjCMethodDeclBits.Family =
792 static_cast<ObjCMethodFamily>(InvalidObjCMethodFamily);
793 setInstanceMethod(isInstance);
794 setVariadic(isVariadic);
795 setPropertyAccessor(isPropertyAccessor);
796 setSynthesizedAccessorStub(isSynthesizedAccessorStub);
797 setDefined(isDefined);
798 setIsRedeclaration(false);
799 setHasRedeclaration(false);
800 setDeclImplementation(impControl);
801 setObjCDeclQualifier(OBJC_TQ_None);
802 setRelatedResultType(HasRelatedResultType);
803 setSelLocsKind(SelLoc_StandardNoSpace);
804 setOverriding(false);
805 setHasSkippedBody(false);
806
807 setImplicit(isImplicitlyDeclared);
808}
809
810ObjCMethodDecl *ObjCMethodDecl::Create(
811 ASTContext &C, SourceLocation beginLoc, SourceLocation endLoc,
812 Selector SelInfo, QualType T, TypeSourceInfo *ReturnTInfo,
813 DeclContext *contextDecl, bool isInstance, bool isVariadic,
814 bool isPropertyAccessor, bool isSynthesizedAccessorStub,
815 bool isImplicitlyDeclared, bool isDefined, ImplementationControl impControl,
816 bool HasRelatedResultType) {
817 return new (C, contextDecl) ObjCMethodDecl(
818 beginLoc, endLoc, SelInfo, T, ReturnTInfo, contextDecl, isInstance,
819 isVariadic, isPropertyAccessor, isSynthesizedAccessorStub,
820 isImplicitlyDeclared, isDefined, impControl, HasRelatedResultType);
821}
822
823ObjCMethodDecl *ObjCMethodDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
824 return new (C, ID) ObjCMethodDecl(SourceLocation(), SourceLocation(),
825 Selector(), QualType(), nullptr, nullptr);
826}
827
828bool ObjCMethodDecl::isDirectMethod() const {
829 return hasAttr<ObjCDirectAttr>() &&
830 !getASTContext().getLangOpts().ObjCDisableDirectMethodsForTesting;
831}
832
833bool ObjCMethodDecl::isThisDeclarationADesignatedInitializer() const {
834 return getMethodFamily() == OMF_init &&
835 hasAttr<ObjCDesignatedInitializerAttr>();
836}
837
838bool ObjCMethodDecl::definedInNSObject(const ASTContext &Ctx) const {
839 if (const auto *PD = dyn_cast<const ObjCProtocolDecl>(getDeclContext()))
840 return PD->getIdentifier() == Ctx.getNSObjectName();
841 if (const auto *ID = dyn_cast<const ObjCInterfaceDecl>(getDeclContext()))
842 return ID->getIdentifier() == Ctx.getNSObjectName();
843 return false;
844}
845
846bool ObjCMethodDecl::isDesignatedInitializerForTheInterface(
847 const ObjCMethodDecl **InitMethod) const {
848 if (getMethodFamily() != OMF_init)
849 return false;
850 const DeclContext *DC = getDeclContext();
851 if (isa<ObjCProtocolDecl>(DC))
852 return false;
853 if (const ObjCInterfaceDecl *ID = getClassInterface())
854 return ID->isDesignatedInitializer(getSelector(), InitMethod);
855 return false;
856}
857
858bool ObjCMethodDecl::hasParamDestroyedInCallee() const {
859 for (auto param : parameters()) {
860 if (param->isDestroyedInCallee())
861 return true;
862 }
863 return false;
864}
865
866Stmt *ObjCMethodDecl::getBody() const {
867 return Body.get(getASTContext().getExternalSource());
868}
869
870void ObjCMethodDecl::setAsRedeclaration(const ObjCMethodDecl *PrevMethod) {
871 assert(PrevMethod)(static_cast <bool> (PrevMethod) ? void (0) : __assert_fail
("PrevMethod", "/build/llvm-toolchain-snapshot-14~++20211016100712+8e1d532707fd/clang/lib/AST/DeclObjC.cpp"
, 871, __extension__ __PRETTY_FUNCTION__))
;
872 getASTContext().setObjCMethodRedeclaration(PrevMethod, this);
873 setIsRedeclaration(true);
874 PrevMethod->setHasRedeclaration(true);
875}
876
877void ObjCMethodDecl::setParamsAndSelLocs(ASTContext &C,
878 ArrayRef<ParmVarDecl*> Params,
879 ArrayRef<SourceLocation> SelLocs) {
880 ParamsAndSelLocs = nullptr;
881 NumParams = Params.size();
882 if (Params.empty() && SelLocs.empty())
883 return;
884
885 static_assert(alignof(ParmVarDecl *) >= alignof(SourceLocation),
886 "Alignment not sufficient for SourceLocation");
887
888 unsigned Size = sizeof(ParmVarDecl *) * NumParams +
889 sizeof(SourceLocation) * SelLocs.size();
890 ParamsAndSelLocs = C.Allocate(Size);
891 std::copy(Params.begin(), Params.end(), getParams());
892 std::copy(SelLocs.begin(), SelLocs.end(), getStoredSelLocs());
893}
894
895void ObjCMethodDecl::getSelectorLocs(
896 SmallVectorImpl<SourceLocation> &SelLocs) const {
897 for (unsigned i = 0, e = getNumSelectorLocs(); i != e; ++i)
898 SelLocs.push_back(getSelectorLoc(i));
899}
900
901void ObjCMethodDecl::setMethodParams(ASTContext &C,
902 ArrayRef<ParmVarDecl*> Params,
903 ArrayRef<SourceLocation> SelLocs) {
904 assert((!SelLocs.empty() || isImplicit()) &&(static_cast <bool> ((!SelLocs.empty() || isImplicit())
&& "No selector locs for non-implicit method") ? void
(0) : __assert_fail ("(!SelLocs.empty() || isImplicit()) && \"No selector locs for non-implicit method\""
, "/build/llvm-toolchain-snapshot-14~++20211016100712+8e1d532707fd/clang/lib/AST/DeclObjC.cpp"
, 905, __extension__ __PRETTY_FUNCTION__))
905 "No selector locs for non-implicit method")(static_cast <bool> ((!SelLocs.empty() || isImplicit())
&& "No selector locs for non-implicit method") ? void
(0) : __assert_fail ("(!SelLocs.empty() || isImplicit()) && \"No selector locs for non-implicit method\""
, "/build/llvm-toolchain-snapshot-14~++20211016100712+8e1d532707fd/clang/lib/AST/DeclObjC.cpp"
, 905, __extension__ __PRETTY_FUNCTION__))
;
906 if (isImplicit())
907 return setParamsAndSelLocs(C, Params, llvm::None);
908
909 setSelLocsKind(hasStandardSelectorLocs(getSelector(), SelLocs, Params,
910 DeclEndLoc));
911 if (getSelLocsKind() != SelLoc_NonStandard)
912 return setParamsAndSelLocs(C, Params, llvm::None);
913
914 setParamsAndSelLocs(C, Params, SelLocs);
915}
916
917/// A definition will return its interface declaration.
918/// An interface declaration will return its definition.
919/// Otherwise it will return itself.
920ObjCMethodDecl *ObjCMethodDecl::getNextRedeclarationImpl() {
921 ASTContext &Ctx = getASTContext();
922 ObjCMethodDecl *Redecl = nullptr;
923 if (hasRedeclaration())
924 Redecl = const_cast<ObjCMethodDecl*>(Ctx.getObjCMethodRedeclaration(this));
925 if (Redecl)
926 return Redecl;
927
928 auto *CtxD = cast<Decl>(getDeclContext());
929
930 if (!CtxD->isInvalidDecl()) {
931 if (auto *IFD = dyn_cast<ObjCInterfaceDecl>(CtxD)) {
932 if (ObjCImplementationDecl *ImplD = Ctx.getObjCImplementation(IFD))
933 if (!ImplD->isInvalidDecl())
934 Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
935
936 } else if (auto *CD = dyn_cast<ObjCCategoryDecl>(CtxD)) {
937 if (ObjCCategoryImplDecl *ImplD = Ctx.getObjCImplementation(CD))
938 if (!ImplD->isInvalidDecl())
939 Redecl = ImplD->getMethod(getSelector(), isInstanceMethod());
940
941 } else if (auto *ImplD = dyn_cast<ObjCImplementationDecl>(CtxD)) {
942 if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface())
943 if (!IFD->isInvalidDecl())
944 Redecl = IFD->getMethod(getSelector(), isInstanceMethod());
945
946 } else if (auto *CImplD = dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
947 if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
948 if (!CatD->isInvalidDecl())
949 Redecl = CatD->getMethod(getSelector(), isInstanceMethod());
950 }
951 }
952
953 // Ensure that the discovered method redeclaration has a valid declaration
954 // context. Used to prevent infinite loops when iterating redeclarations in
955 // a partially invalid AST.
956 if (Redecl && cast<Decl>(Redecl->getDeclContext())->isInvalidDecl())
957 Redecl = nullptr;
958
959 if (!Redecl && isRedeclaration()) {
960 // This is the last redeclaration, go back to the first method.
961 return cast<ObjCContainerDecl>(CtxD)->getMethod(getSelector(),
962 isInstanceMethod(),
963 /*AllowHidden=*/true);
964 }
965
966 return Redecl ? Redecl : this;
967}
968
969ObjCMethodDecl *ObjCMethodDecl::getCanonicalDecl() {
970 auto *CtxD = cast<Decl>(getDeclContext());
971 const auto &Sel = getSelector();
972
973 if (auto *ImplD = dyn_cast<ObjCImplementationDecl>(CtxD)) {
974 if (ObjCInterfaceDecl *IFD = ImplD->getClassInterface()) {
975 // When the container is the ObjCImplementationDecl (the primary
976 // @implementation), then the canonical Decl is either in
977 // the class Interface, or in any of its extension.
978 //
979 // So when we don't find it in the ObjCInterfaceDecl,
980 // sift through extensions too.
981 if (ObjCMethodDecl *MD = IFD->getMethod(Sel, isInstanceMethod()))
982 return MD;
983 for (auto *Ext : IFD->known_extensions())
984 if (ObjCMethodDecl *MD = Ext->getMethod(Sel, isInstanceMethod()))
985 return MD;
986 }
987 } else if (auto *CImplD = dyn_cast<ObjCCategoryImplDecl>(CtxD)) {
988 if (ObjCCategoryDecl *CatD = CImplD->getCategoryDecl())
989 if (ObjCMethodDecl *MD = CatD->getMethod(Sel, isInstanceMethod()))
990 return MD;
991 }
992
993 if (isRedeclaration()) {
994 // It is possible that we have not done deserializing the ObjCMethod yet.
995 ObjCMethodDecl *MD =
996 cast<ObjCContainerDecl>(CtxD)->getMethod(Sel, isInstanceMethod(),
997 /*AllowHidden=*/true);
998 return MD ? MD : this;
999 }
1000
1001 return this;
1002}
1003
1004SourceLocation ObjCMethodDecl::getEndLoc() const {
1005 if (Stmt *Body = getBody())
1006 return Body->getEndLoc();
1007 return DeclEndLoc;
1008}
1009
1010ObjCMethodFamily ObjCMethodDecl::getMethodFamily() const {
1011 auto family = static_cast<ObjCMethodFamily>(ObjCMethodDeclBits.Family);
1012 if (family != static_cast<unsigned>(InvalidObjCMethodFamily))
1013 return family;
1014
1015 // Check for an explicit attribute.
1016 if (const ObjCMethodFamilyAttr *attr = getAttr<ObjCMethodFamilyAttr>()) {
1017 // The unfortunate necessity of mapping between enums here is due
1018 // to the attributes framework.
1019 switch (attr->getFamily()) {
1020 case ObjCMethodFamilyAttr::OMF_None: family = OMF_None; break;
1021 case ObjCMethodFamilyAttr::OMF_alloc: family = OMF_alloc; break;
1022 case ObjCMethodFamilyAttr::OMF_copy: family = OMF_copy; break;
1023 case ObjCMethodFamilyAttr::OMF_init: family = OMF_init; break;
1024 case ObjCMethodFamilyAttr::OMF_mutableCopy: family = OMF_mutableCopy; break;
1025 case ObjCMethodFamilyAttr::OMF_new: family = OMF_new; break;
1026 }
1027 ObjCMethodDeclBits.Family = family;
1028 return family;
1029 }
1030
1031 family = getSelector().getMethodFamily();
1032 switch (family) {
1033 case OMF_None: break;
1034
1035 // init only has a conventional meaning for an instance method, and
1036 // it has to return an object.
1037 case OMF_init:
1038 if (!isInstanceMethod() || !getReturnType()->isObjCObjectPointerType())
1039 family = OMF_None;
1040 break;
1041
1042 // alloc/copy/new have a conventional meaning for both class and
1043 // instance methods, but they require an object return.
1044 case OMF_alloc:
1045 case OMF_copy:
1046 case OMF_mutableCopy:
1047 case OMF_new:
1048 if (!getReturnType()->isObjCObjectPointerType())
1049 family = OMF_None;
1050 break;
1051
1052 // These selectors have a conventional meaning only for instance methods.
1053 case OMF_dealloc:
1054 case OMF_finalize:
1055 case OMF_retain:
1056 case OMF_release:
1057 case OMF_autorelease:
1058 case OMF_retainCount:
1059 case OMF_self:
1060 if (!isInstanceMethod())
1061 family = OMF_None;
1062 break;
1063
1064 case OMF_initialize:
1065 if (isInstanceMethod() || !getReturnType()->isVoidType())
1066 family = OMF_None;
1067 break;
1068
1069 case OMF_performSelector:
1070 if (!isInstanceMethod() || !getReturnType()->isObjCIdType())
1071 family = OMF_None;
1072 else {
1073 unsigned noParams = param_size();
1074 if (noParams < 1 || noParams > 3)
1075 family = OMF_None;
1076 else {
1077 ObjCMethodDecl::param_type_iterator it = param_type_begin();
1078 QualType ArgT = (*it);
1079 if (!ArgT->isObjCSelType()) {
1080 family = OMF_None;
1081 break;
1082 }
1083 while (--noParams) {
1084 it++;
1085 ArgT = (*it);
1086 if (!ArgT->isObjCIdType()) {
1087 family = OMF_None;
1088 break;
1089 }
1090 }
1091 }
1092 }
1093 break;
1094
1095 }
1096
1097 // Cache the result.
1098 ObjCMethodDeclBits.Family = family;
1099 return family;
1100}
1101
1102QualType ObjCMethodDecl::getSelfType(ASTContext &Context,
1103 const ObjCInterfaceDecl *OID,
1104 bool &selfIsPseudoStrong,
1105 bool &selfIsConsumed) const {
1106 QualType selfTy;
1107 selfIsPseudoStrong = false;
1108 selfIsConsumed = false;
1109 if (isInstanceMethod()) {
1110 // There may be no interface context due to error in declaration
1111 // of the interface (which has been reported). Recover gracefully.
1112 if (OID) {
1113 selfTy = Context.getObjCInterfaceType(OID);
1114 selfTy = Context.getObjCObjectPointerType(selfTy);
1115 } else {
1116 selfTy = Context.getObjCIdType();
1117 }
1118 } else // we have a factory method.
1119 selfTy = Context.getObjCClassType();
1120
1121 if (Context.getLangOpts().ObjCAutoRefCount) {
1122 if (isInstanceMethod()) {
1123 selfIsConsumed = hasAttr<NSConsumesSelfAttr>();
1124
1125 // 'self' is always __strong. It's actually pseudo-strong except
1126 // in init methods (or methods labeled ns_consumes_self), though.
1127 Qualifiers qs;
1128 qs.setObjCLifetime(Qualifiers::OCL_Strong);
1129 selfTy = Context.getQualifiedType(selfTy, qs);
1130
1131 // In addition, 'self' is const unless this is an init method.
1132 if (getMethodFamily() != OMF_init && !selfIsConsumed) {
1133 selfTy = selfTy.withConst();
1134 selfIsPseudoStrong = true;
1135 }
1136 }
1137 else {
1138 assert(isClassMethod())(static_cast <bool> (isClassMethod()) ? void (0) : __assert_fail
("isClassMethod()", "/build/llvm-toolchain-snapshot-14~++20211016100712+8e1d532707fd/clang/lib/AST/DeclObjC.cpp"
, 1138, __extension__ __PRETTY_FUNCTION__))
;
1139 // 'self' is always const in class methods.
1140 selfTy = selfTy.withConst();
1141 selfIsPseudoStrong = true;
1142 }
1143 }
1144 return selfTy;
1145}
1146
1147void ObjCMethodDecl::createImplicitParams(ASTContext &Context,
1148 const ObjCInterfaceDecl *OID) {
1149 bool selfIsPseudoStrong, selfIsConsumed;
1150 QualType selfTy =
1151 getSelfType(Context, OID, selfIsPseudoStrong, selfIsConsumed);
1152 auto *Self = ImplicitParamDecl::Create(Context, this, SourceLocation(),
1153 &Context.Idents.get("self"), selfTy,
1154 ImplicitParamDecl::ObjCSelf);
1155 setSelfDecl(Self);
1156
1157 if (selfIsConsumed)
1158 Self->addAttr(NSConsumedAttr::CreateImplicit(Context));
1159
1160 if (selfIsPseudoStrong)
1161 Self->setARCPseudoStrong(true);
1162
1163 setCmdDecl(ImplicitParamDecl::Create(
1164 Context, this, SourceLocation(), &Context.Idents.get("_cmd"),
1165 Context.getObjCSelType(), ImplicitParamDecl::ObjCCmd));
1166}
1167
1168ObjCInterfaceDecl *ObjCMethodDecl::getClassInterface() {
1169 if (auto *ID = dyn_cast<ObjCInterfaceDecl>(getDeclContext()))
1170 return ID;
1171 if (auto *CD = dyn_cast<ObjCCategoryDecl>(getDeclContext()))
1172 return CD->getClassInterface();
1173 if (auto *IMD = dyn_cast<ObjCImplDecl>(getDeclContext()))
1174 return IMD->getClassInterface();
1175 if (isa<ObjCProtocolDecl>(getDeclContext()))
1176 return nullptr;
1177 llvm_unreachable("unknown method context")::llvm::llvm_unreachable_internal("unknown method context", "/build/llvm-toolchain-snapshot-14~++20211016100712+8e1d532707fd/clang/lib/AST/DeclObjC.cpp"
, 1177)
;
1178}
1179
1180ObjCCategoryDecl *ObjCMethodDecl::getCategory() {
1181 if (auto *CD = dyn_cast<ObjCCategoryDecl>(getDeclContext()))
1182 return CD;
1183 if (auto *IMD = dyn_cast<ObjCCategoryImplDecl>(getDeclContext()))
1184 return IMD->getCategoryDecl();
1185 return nullptr;
1186}
1187
1188SourceRange ObjCMethodDecl::getReturnTypeSourceRange() const {
1189 const auto *TSI = getReturnTypeSourceInfo();
1190 if (TSI)
1191 return TSI->getTypeLoc().getSourceRange();
1192 return SourceRange();
1193}
1194
1195QualType ObjCMethodDecl::getSendResultType() const {
1196 ASTContext &Ctx = getASTContext();
1197 return getReturnType().getNonLValueExprType(Ctx)
1198 .substObjCTypeArgs(Ctx, {}, ObjCSubstitutionContext::Result);
1199}
1200
1201QualType ObjCMethodDecl::getSendResultType(QualType receiverType) const {
1202 // FIXME: Handle related result types here.
1203
1204 return getReturnType().getNonLValueExprType(getASTContext())
1205 .substObjCMemberType(receiverType, getDeclContext(),
1206 ObjCSubstitutionContext::Result);
1207}
1208
1209static void CollectOverriddenMethodsRecurse(const ObjCContainerDecl *Container,
1210 const ObjCMethodDecl *Method,
1211 SmallVectorImpl<const ObjCMethodDecl *> &Methods,
1212 bool MovedToSuper) {
1213 if (!Container)
1214 return;
1215
1216 // In categories look for overridden methods from protocols. A method from
1217 // category is not "overridden" since it is considered as the "same" method
1218 // (same USR) as the one from the interface.
1219 if (const auto *Category = dyn_cast<ObjCCategoryDecl>(Container)) {
1220 // Check whether we have a matching method at this category but only if we
1221 // are at the super class level.
1222 if (MovedToSuper)
1223 if (ObjCMethodDecl *
1224 Overridden = Container->getMethod(Method->getSelector(),
1225 Method->isInstanceMethod(),
1226 /*AllowHidden=*/true))
1227 if (Method != Overridden) {
1228 // We found an override at this category; there is no need to look
1229 // into its protocols.
1230 Methods.push_back(Overridden);
1231 return;
1232 }
1233
1234 for (const auto *P : Category->protocols())
1235 CollectOverriddenMethodsRecurse(P, Method, Methods, MovedToSuper);
1236 return;
1237 }
1238
1239 // Check whether we have a matching method at this level.
1240 if (const ObjCMethodDecl *
1241 Overridden = Container->getMethod(Method->getSelector(),
1242 Method->isInstanceMethod(),
1243 /*AllowHidden=*/true))
1244 if (Method != Overridden) {
1245 // We found an override at this level; there is no need to look
1246 // into other protocols or categories.
1247 Methods.push_back(Overridden);
1248 return;
1249 }
1250
1251 if (const auto *Protocol = dyn_cast<ObjCProtocolDecl>(Container)){
1252 for (const auto *P : Protocol->protocols())
1253 CollectOverriddenMethodsRecurse(P, Method, Methods, MovedToSuper);
1254 }
1255
1256 if (const auto *Interface = dyn_cast<ObjCInterfaceDecl>(Container)) {
1257 for (const auto *P : Interface->protocols())
1258 CollectOverriddenMethodsRecurse(P, Method, Methods, MovedToSuper);
1259
1260 for (const auto *Cat : Interface->known_categories())
1261 CollectOverriddenMethodsRecurse(Cat, Method, Methods, MovedToSuper);
1262
1263 if (const ObjCInterfaceDecl *Super = Interface->getSuperClass())
1264 return CollectOverriddenMethodsRecurse(Super, Method, Methods,
1265 /*MovedToSuper=*/true);
1266 }
1267}
1268
1269static inline void CollectOverriddenMethods(const ObjCContainerDecl *Container,
1270 const ObjCMethodDecl *Method,
1271 SmallVectorImpl<const ObjCMethodDecl *> &Methods) {
1272 CollectOverriddenMethodsRecurse(Container, Method, Methods,
1273 /*MovedToSuper=*/false);
1274}
1275
1276static void collectOverriddenMethodsSlow(const ObjCMethodDecl *Method,
1277 SmallVectorImpl<const ObjCMethodDecl *> &overridden) {
1278 assert(Method->isOverriding())(static_cast <bool> (Method->isOverriding()) ? void (
0) : __assert_fail ("Method->isOverriding()", "/build/llvm-toolchain-snapshot-14~++20211016100712+8e1d532707fd/clang/lib/AST/DeclObjC.cpp"
, 1278, __extension__ __PRETTY_FUNCTION__))
;
1279
1280 if (const auto *ProtD =
1281 dyn_cast<ObjCProtocolDecl>(Method->getDeclContext())) {
1282 CollectOverriddenMethods(ProtD, Method, overridden);
1283
1284 } else if (const auto *IMD =
1285 dyn_cast<ObjCImplDecl>(Method->getDeclContext())) {
1286 const ObjCInterfaceDecl *ID = IMD->getClassInterface();
1287 if (!ID)
1288 return;
1289 // Start searching for overridden methods using the method from the
1290 // interface as starting point.
1291 if (const ObjCMethodDecl *IFaceMeth = ID->getMethod(Method->getSelector(),
1292 Method->isInstanceMethod(),
1293 /*AllowHidden=*/true))
1294 Method = IFaceMeth;
1295 CollectOverriddenMethods(ID, Method, overridden);
1296
1297 } else if (const auto *CatD =
1298 dyn_cast<ObjCCategoryDecl>(Method->getDeclContext())) {
1299 const ObjCInterfaceDecl *ID = CatD->getClassInterface();
1300 if (!ID)
1301 return;
1302 // Start searching for overridden methods using the method from the
1303 // interface as starting point.
1304 if (const ObjCMethodDecl *IFaceMeth = ID->getMethod(Method->getSelector(),
1305 Method->isInstanceMethod(),
1306 /*AllowHidden=*/true))
1307 Method = IFaceMeth;
1308 CollectOverriddenMethods(ID, Method, overridden);
1309
1310 } else {
1311 CollectOverriddenMethods(
1312 dyn_cast_or_null<ObjCContainerDecl>(Method->getDeclContext()),
1313 Method, overridden);
1314 }
1315}
1316
1317void ObjCMethodDecl::getOverriddenMethods(
1318 SmallVectorImpl<const ObjCMethodDecl *> &Overridden) const {
1319 const ObjCMethodDecl *Method = this;
1320
1321 if (Method->isRedeclaration()) {
1322 Method = cast<ObjCContainerDecl>(Method->getDeclContext())
1323 ->getMethod(Method->getSelector(), Method->isInstanceMethod(),
1324 /*AllowHidden=*/true);
1325 }
1326
1327 if (Method->isOverriding()) {
1328 collectOverriddenMethodsSlow(Method, Overridden);
1329 assert(!Overridden.empty() &&(static_cast <bool> (!Overridden.empty() && "ObjCMethodDecl's overriding bit is not as expected"
) ? void (0) : __assert_fail ("!Overridden.empty() && \"ObjCMethodDecl's overriding bit is not as expected\""
, "/build/llvm-toolchain-snapshot-14~++20211016100712+8e1d532707fd/clang/lib/AST/DeclObjC.cpp"
, 1330, __extension__ __PRETTY_FUNCTION__))
1330 "ObjCMethodDecl's overriding bit is not as expected")(static_cast <bool> (!Overridden.empty() && "ObjCMethodDecl's overriding bit is not as expected"
) ? void (0) : __assert_fail ("!Overridden.empty() && \"ObjCMethodDecl's overriding bit is not as expected\""
, "/build/llvm-toolchain-snapshot-14~++20211016100712+8e1d532707fd/clang/lib/AST/DeclObjC.cpp"
, 1330, __extension__ __PRETTY_FUNCTION__))
;
1331 }
1332}
1333
1334const ObjCPropertyDecl *
1335ObjCMethodDecl::findPropertyDecl(bool CheckOverrides) const {
1336 Selector Sel = getSelector();
1337 unsigned NumArgs = Sel.getNumArgs();
1338 if (NumArgs > 1)
1339 return nullptr;
1340
1341 if (isPropertyAccessor()) {
1342 const auto *Container = cast<ObjCContainerDecl>(getParent());
1343 // For accessor stubs, go back to the interface.
1344 if (auto *ImplDecl = dyn_cast<ObjCImplDecl>(Container))
1345 if (isSynthesizedAccessorStub())
1346 Container = ImplDecl->getClassInterface();
1347
1348 bool IsGetter = (NumArgs == 0);
1349 bool IsInstance = isInstanceMethod();
1350
1351 /// Local function that attempts to find a matching property within the
1352 /// given Objective-C container.
1353 auto findMatchingProperty =
1354 [&](const ObjCContainerDecl *Container) -> const ObjCPropertyDecl * {
1355 if (IsInstance) {
1356 for (const auto *I : Container->instance_properties()) {
1357 Selector NextSel = IsGetter ? I->getGetterName()
1358 : I->getSetterName();
1359 if (NextSel == Sel)
1360 return I;
1361 }
1362 } else {
1363 for (const auto *I : Container->class_properties()) {
1364 Selector NextSel = IsGetter ? I->getGetterName()
1365 : I->getSetterName();
1366 if (NextSel == Sel)
1367 return I;
1368 }
1369 }
1370
1371 return nullptr;
1372 };
1373
1374 // Look in the container we were given.
1375 if (const auto *Found = findMatchingProperty(Container))
1376 return Found;
1377
1378 // If we're in a category or extension, look in the main class.
1379 const ObjCInterfaceDecl *ClassDecl = nullptr;
1380 if (const auto *Category = dyn_cast<ObjCCategoryDecl>(Container)) {
1381 ClassDecl = Category->getClassInterface();
1382 if (const auto *Found = findMatchingProperty(ClassDecl))
1383 return Found;
1384 } else {
1385 // Determine whether the container is a class.
1386 ClassDecl = cast<ObjCInterfaceDecl>(Container);
1387 }
1388 assert(ClassDecl && "Failed to find main class")(static_cast <bool> (ClassDecl && "Failed to find main class"
) ? void (0) : __assert_fail ("ClassDecl && \"Failed to find main class\""
, "/build/llvm-toolchain-snapshot-14~++20211016100712+8e1d532707fd/clang/lib/AST/DeclObjC.cpp"
, 1388, __extension__ __PRETTY_FUNCTION__))
;
1389
1390 // If we have a class, check its visible extensions.
1391 for (const auto *Ext : ClassDecl->visible_extensions()) {
1392 if (Ext == Container)
1393 continue;
1394 if (const auto *Found = findMatchingProperty(Ext))
1395 return Found;
1396 }
1397
1398 assert(isSynthesizedAccessorStub() && "expected an accessor stub")(static_cast <bool> (isSynthesizedAccessorStub() &&
"expected an accessor stub") ? void (0) : __assert_fail ("isSynthesizedAccessorStub() && \"expected an accessor stub\""
, "/build/llvm-toolchain-snapshot-14~++20211016100712+8e1d532707fd/clang/lib/AST/DeclObjC.cpp"
, 1398, __extension__ __PRETTY_FUNCTION__))
;
1399
1400 for (const auto *Cat : ClassDecl->known_categories()) {
1401 if (Cat == Container)
1402 continue;
1403 if (const auto *Found = findMatchingProperty(Cat))
1404 return Found;
1405 }
1406
1407 llvm_unreachable("Marked as a property accessor but no property found!")::llvm::llvm_unreachable_internal("Marked as a property accessor but no property found!"
, "/build/llvm-toolchain-snapshot-14~++20211016100712+8e1d532707fd/clang/lib/AST/DeclObjC.cpp"
, 1407)
;
1408 }
1409
1410 if (!CheckOverrides)
1411 return nullptr;
1412
1413 using OverridesTy = SmallVector<const ObjCMethodDecl *, 8>;
1414
1415 OverridesTy Overrides;
1416 getOverriddenMethods(Overrides);
1417 for (const auto *Override : Overrides)
1418 if (const ObjCPropertyDecl *Prop = Override->findPropertyDecl(false))
1419 return Prop;
1420
1421 return nullptr;
1422}
1423
1424//===----------------------------------------------------------------------===//
1425// ObjCTypeParamDecl
1426//===----------------------------------------------------------------------===//
1427
1428void ObjCTypeParamDecl::anchor() {}
1429
1430ObjCTypeParamDecl *ObjCTypeParamDecl::Create(ASTContext &ctx, DeclContext *dc,
1431 ObjCTypeParamVariance variance,
1432 SourceLocation varianceLoc,
1433 unsigned index,
1434 SourceLocation nameLoc,
1435 IdentifierInfo *name,
1436 SourceLocation colonLoc,
1437 TypeSourceInfo *boundInfo) {
1438 auto *TPDecl =
1439 new (ctx, dc) ObjCTypeParamDecl(ctx, dc, variance, varianceLoc, index,
1440 nameLoc, name, colonLoc, boundInfo);
1441 QualType TPType = ctx.getObjCTypeParamType(TPDecl, {});
1442 TPDecl->setTypeForDecl(TPType.getTypePtr());
1443 return TPDecl;
1444}
1445
1446ObjCTypeParamDecl *ObjCTypeParamDecl::CreateDeserialized(ASTContext &ctx,
1447 unsigned ID) {
1448 return new (ctx, ID) ObjCTypeParamDecl(ctx, nullptr,
1449 ObjCTypeParamVariance::Invariant,
1450 SourceLocation(), 0, SourceLocation(),
1451 nullptr, SourceLocation(), nullptr);
1452}
1453
1454SourceRange ObjCTypeParamDecl::getSourceRange() const {
1455 SourceLocation startLoc = VarianceLoc;
1456 if (startLoc.isInvalid())
1457 startLoc = getLocation();
1458
1459 if (hasExplicitBound()) {
1460 return SourceRange(startLoc,
1461 getTypeSourceInfo()->getTypeLoc().getEndLoc());
1462 }
1463
1464 return SourceRange(startLoc);
1465}
1466
1467//===----------------------------------------------------------------------===//
1468// ObjCTypeParamList
1469//===----------------------------------------------------------------------===//
1470ObjCTypeParamList::ObjCTypeParamList(SourceLocation lAngleLoc,
1471 ArrayRef<ObjCTypeParamDecl *> typeParams,
1472 SourceLocation rAngleLoc)
1473 : Brackets(lAngleLoc, rAngleLoc), NumParams(typeParams.size()) {
1474 std::copy(typeParams.begin(), typeParams.end(), begin());
1475}
1476
1477ObjCTypeParamList *ObjCTypeParamList::create(
1478 ASTContext &ctx,
1479 SourceLocation lAngleLoc,
1480 ArrayRef<ObjCTypeParamDecl *> typeParams,
1481 SourceLocation rAngleLoc) {
1482 void *mem =
1483 ctx.Allocate(totalSizeToAlloc<ObjCTypeParamDecl *>(typeParams.size()),
1484 alignof(ObjCTypeParamList));
1485 return new (mem) ObjCTypeParamList(lAngleLoc, typeParams, rAngleLoc);
1486}
1487
1488void ObjCTypeParamList::gatherDefaultTypeArgs(
1489 SmallVectorImpl<QualType> &typeArgs) const {
1490 typeArgs.reserve(size());
1491 for (auto typeParam : *this)
1492 typeArgs.push_back(typeParam->getUnderlyingType());
1493}
1494
1495//===----------------------------------------------------------------------===//
1496// ObjCInterfaceDecl
1497//===----------------------------------------------------------------------===//
1498
1499ObjCInterfaceDecl *ObjCInterfaceDecl::Create(const ASTContext &C,
1500 DeclContext *DC,
1501 SourceLocation atLoc,
1502 IdentifierInfo *Id,
1503 ObjCTypeParamList *typeParamList,
1504 ObjCInterfaceDecl *PrevDecl,
1505 SourceLocation ClassLoc,
1506 bool isInternal){
1507 auto *Result = new (C, DC)
1508 ObjCInterfaceDecl(C, DC, atLoc, Id, typeParamList, ClassLoc, PrevDecl,
1509 isInternal);
1510 Result->Data.setInt(!C.getLangOpts().Modules);
1511 C.getObjCInterfaceType(Result, PrevDecl);
1512 return Result;
1513}
1514
1515ObjCInterfaceDecl *ObjCInterfaceDecl::CreateDeserialized(const ASTContext &C,
1516 unsigned ID) {
1517 auto *Result = new (C, ID)
1518 ObjCInterfaceDecl(C, nullptr, SourceLocation(), nullptr, nullptr,
1519 SourceLocation(), nullptr, false);
1520 Result->Data.setInt(!C.getLangOpts().Modules);
1521 return Result;
1522}
1523
1524ObjCInterfaceDecl::ObjCInterfaceDecl(const ASTContext &C, DeclContext *DC,
1525 SourceLocation AtLoc, IdentifierInfo *Id,
1526 ObjCTypeParamList *typeParamList,
1527 SourceLocation CLoc,
1528 ObjCInterfaceDecl *PrevDecl,
1529 bool IsInternal)
1530 : ObjCContainerDecl(ObjCInterface, DC, Id, CLoc, AtLoc),
1531 redeclarable_base(C) {
1532 setPreviousDecl(PrevDecl);
1533
1534 // Copy the 'data' pointer over.
1535 if (PrevDecl)
1536 Data = PrevDecl->Data;
1537
1538 setImplicit(IsInternal);
1539
1540 setTypeParamList(typeParamList);
1541}
1542
1543void ObjCInterfaceDecl::LoadExternalDefinition() const {
1544 assert(data().ExternallyCompleted && "Class is not externally completed")(static_cast <bool> (data().ExternallyCompleted &&
"Class is not externally completed") ? void (0) : __assert_fail
("data().ExternallyCompleted && \"Class is not externally completed\""
, "/build/llvm-toolchain-snapshot-14~++20211016100712+8e1d532707fd/clang/lib/AST/DeclObjC.cpp"
, 1544, __extension__ __PRETTY_FUNCTION__))
;
1545 data().ExternallyCompleted = false;
1546 getASTContext().getExternalSource()->CompleteType(
1547 const_cast<ObjCInterfaceDecl *>(this));
1548}
1549
1550void ObjCInterfaceDecl::setExternallyCompleted() {
1551 assert(getASTContext().getExternalSource() &&(static_cast <bool> (getASTContext().getExternalSource(
) && "Class can't be externally completed without an external source"
) ? void (0) : __assert_fail ("getASTContext().getExternalSource() && \"Class can't be externally completed without an external source\""
, "/build/llvm-toolchain-snapshot-14~++20211016100712+8e1d532707fd/clang/lib/AST/DeclObjC.cpp"
, 1552, __extension__ __PRETTY_FUNCTION__))
1552 "Class can't be externally completed without an external source")(static_cast <bool> (getASTContext().getExternalSource(
) && "Class can't be externally completed without an external source"
) ? void (0) : __assert_fail ("getASTContext().getExternalSource() && \"Class can't be externally completed without an external source\""
, "/build/llvm-toolchain-snapshot-14~++20211016100712+8e1d532707fd/clang/lib/AST/DeclObjC.cpp"
, 1552, __extension__ __PRETTY_FUNCTION__))
;
1553 assert(hasDefinition() &&(static_cast <bool> (hasDefinition() && "Forward declarations can't be externally completed"
) ? void (0) : __assert_fail ("hasDefinition() && \"Forward declarations can't be externally completed\""
, "/build/llvm-toolchain-snapshot-14~++20211016100712+8e1d532707fd/clang/lib/AST/DeclObjC.cpp"
, 1554, __extension__ __PRETTY_FUNCTION__))
1554 "Forward declarations can't be externally completed")(static_cast <bool> (hasDefinition() && "Forward declarations can't be externally completed"
) ? void (0) : __assert_fail ("hasDefinition() && \"Forward declarations can't be externally completed\""
, "/build/llvm-toolchain-snapshot-14~++20211016100712+8e1d532707fd/clang/lib/AST/DeclObjC.cpp"
, 1554, __extension__ __PRETTY_FUNCTION__))
;
1555 data().ExternallyCompleted = true;
1556}
1557
1558void ObjCInterfaceDecl::setHasDesignatedInitializers() {
1559 // Check for a complete definition and recover if not so.
1560 if (!isThisDeclarationADefinition())
1561 return;
1562 data().HasDesignatedInitializers = true;
1563}
1564
1565bool ObjCInterfaceDecl::hasDesignatedInitializers() const {
1566 // Check for a complete definition and recover if not so.
1567 if (!isThisDeclarationADefinition())
1568 return false;
1569 if (data().ExternallyCompleted)
1570 LoadExternalDefinition();
1571
1572 return data().HasDesignatedInitializers;
1573}
1574
1575StringRef
1576ObjCInterfaceDecl::getObjCRuntimeNameAsString() const {
1577 if (const auto *ObjCRTName = getAttr<ObjCRuntimeNameAttr>())
1578 return ObjCRTName->getMetadataName();
1579
1580 return getName();
1581}
1582
1583StringRef
1584ObjCImplementationDecl::getObjCRuntimeNameAsString() const {
1585 if (ObjCInterfaceDecl *ID =
1586 const_cast<ObjCImplementationDecl*>(this)->getClassInterface())
1587 return ID->getObjCRuntimeNameAsString();
1588
1589 return getName();
1590}
1591
1592ObjCImplementationDecl *ObjCInterfaceDecl::getImplementation() const {
1593 if (const ObjCInterfaceDecl *Def = getDefinition()) {
1594 if (data().ExternallyCompleted)
1595 LoadExternalDefinition();
1596
1597 return getASTContext().getObjCImplementation(
1598 const_cast<ObjCInterfaceDecl*>(Def));
1599 }
1600
1601 // FIXME: Should make sure no callers ever do this.
1602 return nullptr;
1603}
1604
1605void ObjCInterfaceDecl::setImplementation(ObjCImplementationDecl *ImplD) {
1606 getASTContext().setObjCImplementation(getDefinition(), ImplD);
1607}
1608
1609namespace {
1610
1611struct SynthesizeIvarChunk {
1612 uint64_t Size;
1613 ObjCIvarDecl *Ivar;
1614
1615 SynthesizeIvarChunk(uint64_t size, ObjCIvarDecl *ivar)
1616 : Size(size), Ivar(ivar) {}
1617};
1618
1619bool operator<(const SynthesizeIvarChunk & LHS,
1620 const SynthesizeIvarChunk &RHS) {
1621 return LHS.Size < RHS.Size;
1622}
1623
1624} // namespace
1625
1626/// all_declared_ivar_begin - return first ivar declared in this class,
1627/// its extensions and its implementation. Lazily build the list on first
1628/// access.
1629///
1630/// Caveat: The list returned by this method reflects the current
1631/// state of the parser. The cache will be updated for every ivar
1632/// added by an extension or the implementation when they are
1633/// encountered.
1634/// See also ObjCIvarDecl::Create().
1635ObjCIvarDecl *ObjCInterfaceDecl::all_declared_ivar_begin() {
1636 // FIXME: Should make sure no callers ever do this.
1637 if (!hasDefinition())
1
Calling 'ObjCInterfaceDecl::hasDefinition'
5
Returning from 'ObjCInterfaceDecl::hasDefinition'
6
Assuming the condition is false
7
Taking false branch
1638 return nullptr;
1639
1640 ObjCIvarDecl *curIvar = nullptr;
8
'curIvar' initialized to a null pointer value
1641 if (!data().IvarList) {
9
Assuming field 'IvarList' is null
10
Taking true branch
1642 if (!ivar_empty()) {
11
Taking false branch
1643 ObjCInterfaceDecl::ivar_iterator I = ivar_begin(), E = ivar_end();
1644 data().IvarList = *I; ++I;
1645 for (curIvar = data().IvarList; I != E; curIvar = *I, ++I)
1646 curIvar->setNextIvar(*I);
1647 }
1648
1649 for (const auto *Ext : known_extensions()) {
1650 if (!Ext->ivar_empty()) {
12
Calling 'ObjCCategoryDecl::ivar_empty'
21
Returning from 'ObjCCategoryDecl::ivar_empty'
22
Taking true branch
1651 ObjCCategoryDecl::ivar_iterator
1652 I = Ext->ivar_begin(),
1653 E = Ext->ivar_end();
1654 if (!data().IvarList) {
23
Assuming field 'IvarList' is non-null
24
Taking false branch
1655 data().IvarList = *I; ++I;
1656 curIvar = data().IvarList;
1657 }
1658 for ( ;I != E; curIvar = *I, ++I)
25
Calling 'operator!='
31
Returning from 'operator!='
32
Loop condition is true. Entering loop body
1659 curIvar->setNextIvar(*I);
33
Called C++ object pointer is null
1660 }
1661 }
1662 data().IvarListMissingImplementation = true;
1663 }
1664
1665 // cached and complete!
1666 if (!data().IvarListMissingImplementation)
1667 return data().IvarList;
1668
1669 if (ObjCImplementationDecl *ImplDecl = getImplementation()) {
1670 data().IvarListMissingImplementation = false;
1671 if (!ImplDecl->ivar_empty()) {
1672 SmallVector<SynthesizeIvarChunk, 16> layout;
1673 for (auto *IV : ImplDecl->ivars()) {
1674 if (IV->getSynthesize() && !IV->isInvalidDecl()) {
1675 layout.push_back(SynthesizeIvarChunk(
1676 IV->getASTContext().getTypeSize(IV->getType()), IV));
1677 continue;
1678 }
1679 if (!data().IvarList)
1680 data().IvarList = IV;
1681 else
1682 curIvar->setNextIvar(IV);
1683 curIvar = IV;
1684 }
1685
1686 if (!layout.empty()) {
1687 // Order synthesized ivars by their size.
1688 llvm::stable_sort(layout);
1689 unsigned Ix = 0, EIx = layout.size();
1690 if (!data().IvarList) {
1691 data().IvarList = layout[0].Ivar; Ix++;
1692 curIvar = data().IvarList;
1693 }
1694 for ( ; Ix != EIx; curIvar = layout[Ix].Ivar, Ix++)
1695 curIvar->setNextIvar(layout[Ix].Ivar);
1696 }
1697 }
1698 }
1699 return data().IvarList;
1700}
1701
1702/// FindCategoryDeclaration - Finds category declaration in the list of
1703/// categories for this class and returns it. Name of the category is passed
1704/// in 'CategoryId'. If category not found, return 0;
1705///
1706ObjCCategoryDecl *
1707ObjCInterfaceDecl::FindCategoryDeclaration(IdentifierInfo *CategoryId) const {
1708 // FIXME: Should make sure no callers ever do this.
1709 if (!hasDefinition())
1710 return nullptr;
1711
1712 if (data().ExternallyCompleted)
1713 LoadExternalDefinition();
1714
1715 for (auto *Cat : visible_categories())
1716 if (Cat->getIdentifier() == CategoryId)
1717 return Cat;
1718
1719 return nullptr;
1720}
1721
1722ObjCMethodDecl *
1723ObjCInterfaceDecl::getCategoryInstanceMethod(Selector Sel) const {
1724 for (const auto *Cat : visible_categories()) {
1725 if (ObjCCategoryImplDecl *Impl = Cat->getImplementation())
1726 if (ObjCMethodDecl *MD = Impl->getInstanceMethod(Sel))
1727 return MD;
1728 }
1729
1730 return nullptr;
1731}
1732
1733ObjCMethodDecl *ObjCInterfaceDecl::getCategoryClassMethod(Selector Sel) const {
1734 for (const auto *Cat : visible_categories()) {
1735 if (ObjCCategoryImplDecl *Impl = Cat->getImplementation())
1736 if (ObjCMethodDecl *MD = Impl->getClassMethod(Sel))
1737 return MD;
1738 }
1739
1740 return nullptr;
1741}
1742
1743/// ClassImplementsProtocol - Checks that 'lProto' protocol
1744/// has been implemented in IDecl class, its super class or categories (if
1745/// lookupCategory is true).
1746bool ObjCInterfaceDecl::ClassImplementsProtocol(ObjCProtocolDecl *lProto,
1747 bool lookupCategory,
1748 bool RHSIsQualifiedID) {
1749 if (!hasDefinition())
1750 return false;
1751
1752 ObjCInterfaceDecl *IDecl = this;
1753 // 1st, look up the class.
1754 for (auto *PI : IDecl->protocols()){
1755 if (getASTContext().ProtocolCompatibleWithProtocol(lProto, PI))
1756 return true;
1757 // This is dubious and is added to be compatible with gcc. In gcc, it is
1758 // also allowed assigning a protocol-qualified 'id' type to a LHS object
1759 // when protocol in qualified LHS is in list of protocols in the rhs 'id'
1760 // object. This IMO, should be a bug.
1761 // FIXME: Treat this as an extension, and flag this as an error when GCC
1762 // extensions are not enabled.
1763 if (RHSIsQualifiedID &&
1764 getASTContext().ProtocolCompatibleWithProtocol(PI, lProto))
1765 return true;
1766 }
1767
1768 // 2nd, look up the category.
1769 if (lookupCategory)
1770 for (const auto *Cat : visible_categories()) {
1771 for (auto *PI : Cat->protocols())
1772 if (getASTContext().ProtocolCompatibleWithProtocol(lProto, PI))
1773 return true;
1774 }
1775
1776 // 3rd, look up the super class(s)
1777 if (IDecl->getSuperClass())
1778 return
1779 IDecl->getSuperClass()->ClassImplementsProtocol(lProto, lookupCategory,
1780 RHSIsQualifiedID);
1781
1782 return false;
1783}
1784
1785//===----------------------------------------------------------------------===//
1786// ObjCIvarDecl
1787//===----------------------------------------------------------------------===//
1788
1789void ObjCIvarDecl::anchor() {}
1790
1791ObjCIvarDecl *ObjCIvarDecl::Create(ASTContext &C, ObjCContainerDecl *DC,
1792 SourceLocation StartLoc,
1793 SourceLocation IdLoc, IdentifierInfo *Id,
1794 QualType T, TypeSourceInfo *TInfo,
1795 AccessControl ac, Expr *BW,
1796 bool synthesized) {
1797 if (DC) {
1798 // Ivar's can only appear in interfaces, implementations (via synthesized
1799 // properties), and class extensions (via direct declaration, or synthesized
1800 // properties).
1801 //
1802 // FIXME: This should really be asserting this:
1803 // (isa<ObjCCategoryDecl>(DC) &&
1804 // cast<ObjCCategoryDecl>(DC)->IsClassExtension()))
1805 // but unfortunately we sometimes place ivars into non-class extension
1806 // categories on error. This breaks an AST invariant, and should not be
1807 // fixed.
1808 assert((isa<ObjCInterfaceDecl>(DC) || isa<ObjCImplementationDecl>(DC) ||(static_cast <bool> ((isa<ObjCInterfaceDecl>(DC) ||
isa<ObjCImplementationDecl>(DC) || isa<ObjCCategoryDecl
>(DC)) && "Invalid ivar decl context!") ? void (0)
: __assert_fail ("(isa<ObjCInterfaceDecl>(DC) || isa<ObjCImplementationDecl>(DC) || isa<ObjCCategoryDecl>(DC)) && \"Invalid ivar decl context!\""
, "/build/llvm-toolchain-snapshot-14~++20211016100712+8e1d532707fd/clang/lib/AST/DeclObjC.cpp"
, 1810, __extension__ __PRETTY_FUNCTION__))
1809 isa<ObjCCategoryDecl>(DC)) &&(static_cast <bool> ((isa<ObjCInterfaceDecl>(DC) ||
isa<ObjCImplementationDecl>(DC) || isa<ObjCCategoryDecl
>(DC)) && "Invalid ivar decl context!") ? void (0)
: __assert_fail ("(isa<ObjCInterfaceDecl>(DC) || isa<ObjCImplementationDecl>(DC) || isa<ObjCCategoryDecl>(DC)) && \"Invalid ivar decl context!\""
, "/build/llvm-toolchain-snapshot-14~++20211016100712+8e1d532707fd/clang/lib/AST/DeclObjC.cpp"
, 1810, __extension__ __PRETTY_FUNCTION__))
1810 "Invalid ivar decl context!")(static_cast <bool> ((isa<ObjCInterfaceDecl>(DC) ||
isa<ObjCImplementationDecl>(DC) || isa<ObjCCategoryDecl
>(DC)) && "Invalid ivar decl context!") ? void (0)
: __assert_fail ("(isa<ObjCInterfaceDecl>(DC) || isa<ObjCImplementationDecl>(DC) || isa<ObjCCategoryDecl>(DC)) && \"Invalid ivar decl context!\""
, "/build/llvm-toolchain-snapshot-14~++20211016100712+8e1d532707fd/clang/lib/AST/DeclObjC.cpp"
, 1810, __extension__ __PRETTY_FUNCTION__))
;
1811 // Once a new ivar is created in any of class/class-extension/implementation
1812 // decl contexts, the previously built IvarList must be rebuilt.
1813 auto *ID = dyn_cast<ObjCInterfaceDecl>(DC);
1814 if (!ID) {
1815 if (auto *IM = dyn_cast<ObjCImplementationDecl>(DC))
1816 ID = IM->getClassInterface();
1817 else
1818 ID = cast<ObjCCategoryDecl>(DC)->getClassInterface();
1819 }
1820 ID->setIvarList(nullptr);
1821 }
1822
1823 return new (C, DC) ObjCIvarDecl(DC, StartLoc, IdLoc, Id, T, TInfo, ac, BW,
1824 synthesized);
1825}
1826
1827ObjCIvarDecl *ObjCIvarDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
1828 return new (C, ID) ObjCIvarDecl(nullptr, SourceLocation(), SourceLocation(),
1829 nullptr, QualType(), nullptr,
1830 ObjCIvarDecl::None, nullptr, false);
1831}
1832
1833const ObjCInterfaceDecl *ObjCIvarDecl::getContainingInterface() const {
1834 const auto *DC = cast<ObjCContainerDecl>(getDeclContext());
1835
1836 switch (DC->getKind()) {
1837 default:
1838 case ObjCCategoryImpl:
1839 case ObjCProtocol:
1840 llvm_unreachable("invalid ivar container!")::llvm::llvm_unreachable_internal("invalid ivar container!", "/build/llvm-toolchain-snapshot-14~++20211016100712+8e1d532707fd/clang/lib/AST/DeclObjC.cpp"
, 1840)
;
1841
1842 // Ivars can only appear in class extension categories.
1843 case ObjCCategory: {
1844 const auto *CD = cast<ObjCCategoryDecl>(DC);
1845 assert(CD->IsClassExtension() && "invalid container for ivar!")(static_cast <bool> (CD->IsClassExtension() &&
"invalid container for ivar!") ? void (0) : __assert_fail ("CD->IsClassExtension() && \"invalid container for ivar!\""
, "/build/llvm-toolchain-snapshot-14~++20211016100712+8e1d532707fd/clang/lib/AST/DeclObjC.cpp"
, 1845, __extension__ __PRETTY_FUNCTION__))
;
1846 return CD->getClassInterface();
1847 }
1848
1849 case ObjCImplementation:
1850 return cast<ObjCImplementationDecl>(DC)->getClassInterface();
1851
1852 case ObjCInterface:
1853 return cast<ObjCInterfaceDecl>(DC);
1854 }
1855}
1856
1857QualType ObjCIvarDecl::getUsageType(QualType objectType) const {
1858 return getType().substObjCMemberType(objectType, getDeclContext(),
1859 ObjCSubstitutionContext::Property);
1860}
1861
1862//===----------------------------------------------------------------------===//
1863// ObjCAtDefsFieldDecl
1864//===----------------------------------------------------------------------===//
1865
1866void ObjCAtDefsFieldDecl::anchor() {}
1867
1868ObjCAtDefsFieldDecl
1869*ObjCAtDefsFieldDecl::Create(ASTContext &C, DeclContext *DC,
1870 SourceLocation StartLoc, SourceLocation IdLoc,
1871 IdentifierInfo *Id, QualType T, Expr *BW) {
1872 return new (C, DC) ObjCAtDefsFieldDecl(DC, StartLoc, IdLoc, Id, T, BW);
1873}
1874
1875ObjCAtDefsFieldDecl *ObjCAtDefsFieldDecl::CreateDeserialized(ASTContext &C,
1876 unsigned ID) {
1877 return new (C, ID) ObjCAtDefsFieldDecl(nullptr, SourceLocation(),
1878 SourceLocation(), nullptr, QualType(),
1879 nullptr);
1880}
1881
1882//===----------------------------------------------------------------------===//
1883// ObjCProtocolDecl
1884//===----------------------------------------------------------------------===//
1885
1886void ObjCProtocolDecl::anchor() {}
1887
1888ObjCProtocolDecl::ObjCProtocolDecl(ASTContext &C, DeclContext *DC,
1889 IdentifierInfo *Id, SourceLocation nameLoc,
1890 SourceLocation atStartLoc,
1891 ObjCProtocolDecl *PrevDecl)
1892 : ObjCContainerDecl(ObjCProtocol, DC, Id, nameLoc, atStartLoc),
1893 redeclarable_base(C) {
1894 setPreviousDecl(PrevDecl);
1895 if (PrevDecl)
1896 Data = PrevDecl->Data;
1897}
1898
1899ObjCProtocolDecl *ObjCProtocolDecl::Create(ASTContext &C, DeclContext *DC,
1900 IdentifierInfo *Id,
1901 SourceLocation nameLoc,
1902 SourceLocation atStartLoc,
1903 ObjCProtocolDecl *PrevDecl) {
1904 auto *Result =
1905 new (C, DC) ObjCProtocolDecl(C, DC, Id, nameLoc, atStartLoc, PrevDecl);
1906 Result->Data.setInt(!C.getLangOpts().Modules);
1907 return Result;
1908}
1909
1910ObjCProtocolDecl *ObjCProtocolDecl::CreateDeserialized(ASTContext &C,
1911 unsigned ID) {
1912 ObjCProtocolDecl *Result =
1913 new (C, ID) ObjCProtocolDecl(C, nullptr, nullptr, SourceLocation(),
1914 SourceLocation(), nullptr);
1915 Result->Data.setInt(!C.getLangOpts().Modules);
1916 return Result;
1917}
1918
1919bool ObjCProtocolDecl::isNonRuntimeProtocol() const {
1920 return hasAttr<ObjCNonRuntimeProtocolAttr>();
1921}
1922
1923void ObjCProtocolDecl::getImpliedProtocols(
1924 llvm::DenseSet<const ObjCProtocolDecl *> &IPs) const {
1925 std::queue<const ObjCProtocolDecl *> WorkQueue;
1926 WorkQueue.push(this);
1927
1928 while (!WorkQueue.empty()) {
1929 const auto *PD = WorkQueue.front();
1930 WorkQueue.pop();
1931 for (const auto *Parent : PD->protocols()) {
1932 const auto *Can = Parent->getCanonicalDecl();
1933 auto Result = IPs.insert(Can);
1934 if (Result.second)
1935 WorkQueue.push(Parent);
1936 }
1937 }
1938}
1939
1940ObjCProtocolDecl *ObjCProtocolDecl::lookupProtocolNamed(IdentifierInfo *Name) {
1941 ObjCProtocolDecl *PDecl = this;
1942
1943 if (Name == getIdentifier())
1944 return PDecl;
1945
1946 for (auto *I : protocols())
1947 if ((PDecl = I->lookupProtocolNamed(Name)))
1948 return PDecl;
1949
1950 return nullptr;
1951}
1952
1953// lookupMethod - Lookup a instance/class method in the protocol and protocols
1954// it inherited.
1955ObjCMethodDecl *ObjCProtocolDecl::lookupMethod(Selector Sel,
1956 bool isInstance) const {
1957 ObjCMethodDecl *MethodDecl = nullptr;
1958
1959 // If there is no definition or the definition is hidden, we don't find
1960 // anything.
1961 const ObjCProtocolDecl *Def = getDefinition();
1962 if (!Def || !Def->isUnconditionallyVisible())
1963 return nullptr;
1964
1965 if ((MethodDecl = getMethod(Sel, isInstance)))
1966 return MethodDecl;
1967
1968 for (const auto *I : protocols())
1969 if ((MethodDecl = I->lookupMethod(Sel, isInstance)))
1970 return MethodDecl;
1971 return nullptr;
1972}
1973
1974void ObjCProtocolDecl::allocateDefinitionData() {
1975 assert(!Data.getPointer() && "Protocol already has a definition!")(static_cast <bool> (!Data.getPointer() && "Protocol already has a definition!"
) ? void (0) : __assert_fail ("!Data.getPointer() && \"Protocol already has a definition!\""
, "/build/llvm-toolchain-snapshot-14~++20211016100712+8e1d532707fd/clang/lib/AST/DeclObjC.cpp"
, 1975, __extension__ __PRETTY_FUNCTION__))
;
1976 Data.setPointer(new (getASTContext()) DefinitionData);
1977 Data.getPointer()->Definition = this;
1978}
1979
1980void ObjCProtocolDecl::startDefinition() {
1981 allocateDefinitionData();
1982
1983 // Update all of the declarations with a pointer to the definition.
1984 for (auto *RD : redecls())
1985 RD->Data = this->Data;
1986}
1987
1988void ObjCProtocolDecl::collectPropertiesToImplement(PropertyMap &PM,
1989 PropertyDeclOrder &PO) const {
1990 if (const ObjCProtocolDecl *PDecl = getDefinition()) {
1991 for (auto *Prop : PDecl->properties()) {
1992 // Insert into PM if not there already.
1993 PM.insert(std::make_pair(
1994 std::make_pair(Prop->getIdentifier(), Prop->isClassProperty()),
1995 Prop));
1996 PO.push_back(Prop);
1997 }
1998 // Scan through protocol's protocols.
1999 for (const auto *PI : PDecl->protocols())
2000 PI->collectPropertiesToImplement(PM, PO);
2001 }
2002}
2003
2004void ObjCProtocolDecl::collectInheritedProtocolProperties(
2005 const ObjCPropertyDecl *Property, ProtocolPropertySet &PS,
2006 PropertyDeclOrder &PO) const {
2007 if (const ObjCProtocolDecl *PDecl = getDefinition()) {
2008 if (!PS.insert(PDecl).second)
2009 return;
2010 for (auto *Prop : PDecl->properties()) {
2011 if (Prop == Property)
2012 continue;
2013 if (Prop->getIdentifier() == Property->getIdentifier()) {
2014 PO.push_back(Prop);
2015 return;
2016 }
2017 }
2018 // Scan through protocol's protocols which did not have a matching property.
2019 for (const auto *PI : PDecl->protocols())
2020 PI->collectInheritedProtocolProperties(Property, PS, PO);
2021 }
2022}
2023
2024StringRef
2025ObjCProtocolDecl::getObjCRuntimeNameAsString() const {
2026 if (const auto *ObjCRTName = getAttr<ObjCRuntimeNameAttr>())
2027 return ObjCRTName->getMetadataName();
2028
2029 return getName();
2030}
2031
2032//===----------------------------------------------------------------------===//
2033// ObjCCategoryDecl
2034//===----------------------------------------------------------------------===//
2035
2036void ObjCCategoryDecl::anchor() {}
2037
2038ObjCCategoryDecl::ObjCCategoryDecl(DeclContext *DC, SourceLocation AtLoc,
2039 SourceLocation ClassNameLoc,
2040 SourceLocation CategoryNameLoc,
2041 IdentifierInfo *Id, ObjCInterfaceDecl *IDecl,
2042 ObjCTypeParamList *typeParamList,
2043 SourceLocation IvarLBraceLoc,
2044 SourceLocation IvarRBraceLoc)
2045 : ObjCContainerDecl(ObjCCategory, DC, Id, ClassNameLoc, AtLoc),
2046 ClassInterface(IDecl), CategoryNameLoc(CategoryNameLoc),
2047 IvarLBraceLoc(IvarLBraceLoc), IvarRBraceLoc(IvarRBraceLoc) {
2048 setTypeParamList(typeParamList);
2049}
2050
2051ObjCCategoryDecl *ObjCCategoryDecl::Create(ASTContext &C, DeclContext *DC,
2052 SourceLocation AtLoc,
2053 SourceLocation ClassNameLoc,
2054 SourceLocation CategoryNameLoc,
2055 IdentifierInfo *Id,
2056 ObjCInterfaceDecl *IDecl,
2057 ObjCTypeParamList *typeParamList,
2058 SourceLocation IvarLBraceLoc,
2059 SourceLocation IvarRBraceLoc) {
2060 auto *CatDecl =
2061 new (C, DC) ObjCCategoryDecl(DC, AtLoc, ClassNameLoc, CategoryNameLoc, Id,
2062 IDecl, typeParamList, IvarLBraceLoc,
2063 IvarRBraceLoc);
2064 if (IDecl) {
2065 // Link this category into its class's category list.
2066 CatDecl->NextClassCategory = IDecl->getCategoryListRaw();
2067 if (IDecl->hasDefinition()) {
2068 IDecl->setCategoryListRaw(CatDecl);
2069 if (ASTMutationListener *L = C.getASTMutationListener())
2070 L->AddedObjCCategoryToInterface(CatDecl, IDecl);
2071 }
2072 }
2073
2074 return CatDecl;
2075}
2076
2077ObjCCategoryDecl *ObjCCategoryDecl::CreateDeserialized(ASTContext &C,
2078 unsigned ID) {
2079 return new (C, ID) ObjCCategoryDecl(nullptr, SourceLocation(),
2080 SourceLocation(), SourceLocation(),
2081 nullptr, nullptr, nullptr);
2082}
2083
2084ObjCCategoryImplDecl *ObjCCategoryDecl::getImplementation() const {
2085 return getASTContext().getObjCImplementation(
2086 const_cast<ObjCCategoryDecl*>(this));
2087}
2088
2089void ObjCCategoryDecl::setImplementation(ObjCCategoryImplDecl *ImplD) {
2090 getASTContext().setObjCImplementation(this, ImplD);
2091}
2092
2093void ObjCCategoryDecl::setTypeParamList(ObjCTypeParamList *TPL) {
2094 TypeParamList = TPL;
2095 if (!TPL)
2096 return;
2097 // Set the declaration context of each of the type parameters.
2098 for (auto *typeParam : *TypeParamList)
2099 typeParam->setDeclContext(this);
2100}
2101
2102//===----------------------------------------------------------------------===//
2103// ObjCCategoryImplDecl
2104//===----------------------------------------------------------------------===//
2105
2106void ObjCCategoryImplDecl::anchor() {}
2107
2108ObjCCategoryImplDecl *
2109ObjCCategoryImplDecl::Create(ASTContext &C, DeclContext *DC,
2110 IdentifierInfo *Id,
2111 ObjCInterfaceDecl *ClassInterface,
2112 SourceLocation nameLoc,
2113 SourceLocation atStartLoc,
2114 SourceLocation CategoryNameLoc) {
2115 if (ClassInterface && ClassInterface->hasDefinition())
2116 ClassInterface = ClassInterface->getDefinition();
2117 return new (C, DC) ObjCCategoryImplDecl(DC, Id, ClassInterface, nameLoc,
2118 atStartLoc, CategoryNameLoc);
2119}
2120
2121ObjCCategoryImplDecl *ObjCCategoryImplDecl::CreateDeserialized(ASTContext &C,
2122 unsigned ID) {
2123 return new (C, ID) ObjCCategoryImplDecl(nullptr, nullptr, nullptr,
2124 SourceLocation(), SourceLocation(),
2125 SourceLocation());
2126}
2127
2128ObjCCategoryDecl *ObjCCategoryImplDecl::getCategoryDecl() const {
2129 // The class interface might be NULL if we are working with invalid code.
2130 if (const ObjCInterfaceDecl *ID = getClassInterface())
2131 return ID->FindCategoryDeclaration(getIdentifier());
2132 return nullptr;
2133}
2134
2135void ObjCImplDecl::anchor() {}
2136
2137void ObjCImplDecl::addPropertyImplementation(ObjCPropertyImplDecl *property) {
2138 // FIXME: The context should be correct before we get here.
2139 property->setLexicalDeclContext(this);
2140 addDecl(property);
2141}
2142
2143void ObjCImplDecl::setClassInterface(ObjCInterfaceDecl *IFace) {
2144 ASTContext &Ctx = getASTContext();
2145
2146 if (auto *ImplD = dyn_cast_or_null<ObjCImplementationDecl>(this)) {
2147 if (IFace)
2148 Ctx.setObjCImplementation(IFace, ImplD);
2149
2150 } else if (auto *ImplD = dyn_cast_or_null<ObjCCategoryImplDecl>(this)) {
2151 if (ObjCCategoryDecl *CD = IFace->FindCategoryDeclaration(getIdentifier()))
2152 Ctx.setObjCImplementation(CD, ImplD);
2153 }
2154
2155 ClassInterface = IFace;
2156}
2157
2158/// FindPropertyImplIvarDecl - This method lookup the ivar in the list of
2159/// properties implemented in this \@implementation block and returns
2160/// the implemented property that uses it.
2161ObjCPropertyImplDecl *ObjCImplDecl::
2162FindPropertyImplIvarDecl(IdentifierInfo *ivarId) const {
2163 for (auto *PID : property_impls())
2164 if (PID->getPropertyIvarDecl() &&
2165 PID->getPropertyIvarDecl()->getIdentifier() == ivarId)
2166 return PID;
2167 return nullptr;
2168}
2169
2170/// FindPropertyImplDecl - This method looks up a previous ObjCPropertyImplDecl
2171/// added to the list of those properties \@synthesized/\@dynamic in this
2172/// category \@implementation block.
2173ObjCPropertyImplDecl *ObjCImplDecl::
2174FindPropertyImplDecl(IdentifierInfo *Id,
2175 ObjCPropertyQueryKind QueryKind) const {
2176 ObjCPropertyImplDecl *ClassPropImpl = nullptr;
2177 for (auto *PID : property_impls())
2178 // If queryKind is unknown, we return the instance property if one
2179 // exists; otherwise we return the class property.
2180 if (PID->getPropertyDecl()->getIdentifier() == Id) {
2181 if ((QueryKind == ObjCPropertyQueryKind::OBJC_PR_query_unknown &&
2182 !PID->getPropertyDecl()->isClassProperty()) ||
2183 (QueryKind == ObjCPropertyQueryKind::OBJC_PR_query_class &&
2184 PID->getPropertyDecl()->isClassProperty()) ||
2185 (QueryKind == ObjCPropertyQueryKind::OBJC_PR_query_instance &&
2186 !PID->getPropertyDecl()->isClassProperty()))
2187 return PID;
2188
2189 if (PID->getPropertyDecl()->isClassProperty())
2190 ClassPropImpl = PID;
2191 }
2192
2193 if (QueryKind == ObjCPropertyQueryKind::OBJC_PR_query_unknown)
2194 // We can't find the instance property, return the class property.
2195 return ClassPropImpl;
2196
2197 return nullptr;
2198}
2199
2200raw_ostream &clang::operator<<(raw_ostream &OS,
2201 const ObjCCategoryImplDecl &CID) {
2202 OS << CID.getName();
2203 return OS;
2204}
2205
2206//===----------------------------------------------------------------------===//
2207// ObjCImplementationDecl
2208//===----------------------------------------------------------------------===//
2209
2210void ObjCImplementationDecl::anchor() {}
2211
2212ObjCImplementationDecl *
2213ObjCImplementationDecl::Create(ASTContext &C, DeclContext *DC,
2214 ObjCInterfaceDecl *ClassInterface,
2215 ObjCInterfaceDecl *SuperDecl,
2216 SourceLocation nameLoc,
2217 SourceLocation atStartLoc,
2218 SourceLocation superLoc,
2219 SourceLocation IvarLBraceLoc,
2220 SourceLocation IvarRBraceLoc) {
2221 if (ClassInterface && ClassInterface->hasDefinition())
2222 ClassInterface = ClassInterface->getDefinition();
2223 return new (C, DC) ObjCImplementationDecl(DC, ClassInterface, SuperDecl,
2224 nameLoc, atStartLoc, superLoc,
2225 IvarLBraceLoc, IvarRBraceLoc);
2226}
2227
2228ObjCImplementationDecl *
2229ObjCImplementationDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2230 return new (C, ID) ObjCImplementationDecl(nullptr, nullptr, nullptr,
2231 SourceLocation(), SourceLocation());
2232}
2233
2234void ObjCImplementationDecl::setIvarInitializers(ASTContext &C,
2235 CXXCtorInitializer ** initializers,
2236 unsigned numInitializers) {
2237 if (numInitializers > 0) {
2238 NumIvarInitializers = numInitializers;
2239 auto **ivarInitializers = new (C) CXXCtorInitializer*[NumIvarInitializers];
2240 memcpy(ivarInitializers, initializers,
2241 numInitializers * sizeof(CXXCtorInitializer*));
2242 IvarInitializers = ivarInitializers;
2243 }
2244}
2245
2246ObjCImplementationDecl::init_const_iterator
2247ObjCImplementationDecl::init_begin() const {
2248 return IvarInitializers.get(getASTContext().getExternalSource());
2249}
2250
2251raw_ostream &clang::operator<<(raw_ostream &OS,
2252 const ObjCImplementationDecl &ID) {
2253 OS << ID.getName();
2254 return OS;
2255}
2256
2257//===----------------------------------------------------------------------===//
2258// ObjCCompatibleAliasDecl
2259//===----------------------------------------------------------------------===//
2260
2261void ObjCCompatibleAliasDecl::anchor() {}
2262
2263ObjCCompatibleAliasDecl *
2264ObjCCompatibleAliasDecl::Create(ASTContext &C, DeclContext *DC,
2265 SourceLocation L,
2266 IdentifierInfo *Id,
2267 ObjCInterfaceDecl* AliasedClass) {
2268 return new (C, DC) ObjCCompatibleAliasDecl(DC, L, Id, AliasedClass);
2269}
2270
2271ObjCCompatibleAliasDecl *
2272ObjCCompatibleAliasDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
2273 return new (C, ID) ObjCCompatibleAliasDecl(nullptr, SourceLocation(),
2274 nullptr, nullptr);
2275}
2276
2277//===----------------------------------------------------------------------===//
2278// ObjCPropertyDecl
2279//===----------------------------------------------------------------------===//
2280
2281void ObjCPropertyDecl::anchor() {}
2282
2283ObjCPropertyDecl *ObjCPropertyDecl::Create(ASTContext &C, DeclContext *DC,
2284 SourceLocation L,
2285 IdentifierInfo *Id,
2286 SourceLocation AtLoc,
2287 SourceLocation LParenLoc,
2288 QualType T,
2289 TypeSourceInfo *TSI,
2290 PropertyControl propControl) {
2291 return new (C, DC) ObjCPropertyDecl(DC, L, Id, AtLoc, LParenLoc, T, TSI,
2292 propControl);
2293}
2294
2295ObjCPropertyDecl *ObjCPropertyDecl::CreateDeserialized(ASTContext &C,
2296 unsigned ID) {
2297 return new (C, ID) ObjCPropertyDecl(nullptr, SourceLocation(), nullptr,
2298 SourceLocation(), SourceLocation(),
2299 QualType(), nullptr, None);
2300}
2301
2302QualType ObjCPropertyDecl::getUsageType(QualType objectType) const {
2303 return DeclType.substObjCMemberType(objectType, getDeclContext(),
2304 ObjCSubstitutionContext::Property);
2305}
2306
2307bool ObjCPropertyDecl::isDirectProperty() const {
2308 return (PropertyAttributes & ObjCPropertyAttribute::kind_direct) &&
2309 !getASTContext().getLangOpts().ObjCDisableDirectMethodsForTesting;
2310}
2311
2312//===----------------------------------------------------------------------===//
2313// ObjCPropertyImplDecl
2314//===----------------------------------------------------------------------===//
2315
2316ObjCPropertyImplDecl *ObjCPropertyImplDecl::Create(ASTContext &C,
2317 DeclContext *DC,
2318 SourceLocation atLoc,
2319 SourceLocation L,
2320 ObjCPropertyDecl *property,
2321 Kind PK,
2322 ObjCIvarDecl *ivar,
2323 SourceLocation ivarLoc) {
2324 return new (C, DC) ObjCPropertyImplDecl(DC, atLoc, L, property, PK, ivar,
2325 ivarLoc);
2326}
2327
2328ObjCPropertyImplDecl *ObjCPropertyImplDecl::CreateDeserialized(ASTContext &C,
2329 unsigned ID) {
2330 return new (C, ID) ObjCPropertyImplDecl(nullptr, SourceLocation(),
2331 SourceLocation(), nullptr, Dynamic,
2332 nullptr, SourceLocation());
2333}
2334
2335SourceRange ObjCPropertyImplDecl::getSourceRange() const {
2336 SourceLocation EndLoc = getLocation();
2337 if (IvarLoc.isValid())
2338 EndLoc = IvarLoc;
2339
2340 return SourceRange(AtLoc, EndLoc);
2341}

/build/llvm-toolchain-snapshot-14~++20211016100712+8e1d532707fd/clang/include/clang/AST/DeclObjC.h

1//===- DeclObjC.h - Classes for representing declarations -------*- C++ -*-===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8//
9// This file defines the DeclObjC interface and subclasses.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_CLANG_AST_DECLOBJC_H
14#define LLVM_CLANG_AST_DECLOBJC_H
15
16#include "clang/AST/Decl.h"
17#include "clang/AST/DeclBase.h"
18#include "clang/AST/DeclObjCCommon.h"
19#include "clang/AST/ExternalASTSource.h"
20#include "clang/AST/Redeclarable.h"
21#include "clang/AST/SelectorLocationsKind.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/DenseMap.h"
29#include "llvm/ADT/DenseSet.h"
30#include "llvm/ADT/None.h"
31#include "llvm/ADT/PointerIntPair.h"
32#include "llvm/ADT/STLExtras.h"
33#include "llvm/ADT/StringRef.h"
34#include "llvm/ADT/iterator_range.h"
35#include "llvm/Support/Compiler.h"
36#include "llvm/Support/TrailingObjects.h"
37#include <cassert>
38#include <cstddef>
39#include <cstdint>
40#include <iterator>
41#include <string>
42#include <utility>
43
44namespace clang {
45
46class ASTContext;
47class CompoundStmt;
48class CXXCtorInitializer;
49class Expr;
50class ObjCCategoryDecl;
51class ObjCCategoryImplDecl;
52class ObjCImplementationDecl;
53class ObjCInterfaceDecl;
54class ObjCIvarDecl;
55class ObjCPropertyDecl;
56class ObjCPropertyImplDecl;
57class ObjCProtocolDecl;
58class Stmt;
59
60class ObjCListBase {
61protected:
62 /// List is an array of pointers to objects that are not owned by this object.
63 void **List = nullptr;
64 unsigned NumElts = 0;
65
66public:
67 ObjCListBase() = default;
68 ObjCListBase(const ObjCListBase &) = delete;
69 ObjCListBase &operator=(const ObjCListBase &) = delete;
70
71 unsigned size() const { return NumElts; }
72 bool empty() const { return NumElts == 0; }
73
74protected:
75 void set(void *const* InList, unsigned Elts, ASTContext &Ctx);
76};
77
78/// ObjCList - This is a simple template class used to hold various lists of
79/// decls etc, which is heavily used by the ObjC front-end. This only use case
80/// this supports is setting the list all at once and then reading elements out
81/// of it.
82template <typename T>
83class ObjCList : public ObjCListBase {
84public:
85 void set(T* const* InList, unsigned Elts, ASTContext &Ctx) {
86 ObjCListBase::set(reinterpret_cast<void*const*>(InList), Elts, Ctx);
87 }
88
89 using iterator = T* const *;
90
91 iterator begin() const { return (iterator)List; }
92 iterator end() const { return (iterator)List+NumElts; }
93
94 T* operator[](unsigned Idx) const {
95 assert(Idx < NumElts && "Invalid access")(static_cast <bool> (Idx < NumElts && "Invalid access"
) ? void (0) : __assert_fail ("Idx < NumElts && \"Invalid access\""
, "/build/llvm-toolchain-snapshot-14~++20211016100712+8e1d532707fd/clang/include/clang/AST/DeclObjC.h"
, 95, __extension__ __PRETTY_FUNCTION__))
;
96 return (T*)List[Idx];
97 }
98};
99
100/// A list of Objective-C protocols, along with the source
101/// locations at which they were referenced.
102class ObjCProtocolList : public ObjCList<ObjCProtocolDecl> {
103 SourceLocation *Locations = nullptr;
104
105 using ObjCList<ObjCProtocolDecl>::set;
106
107public:
108 ObjCProtocolList() = default;
109
110 using loc_iterator = const SourceLocation *;
111
112 loc_iterator loc_begin() const { return Locations; }
113 loc_iterator loc_end() const { return Locations + size(); }
114
115 void set(ObjCProtocolDecl* const* InList, unsigned Elts,
116 const SourceLocation *Locs, ASTContext &Ctx);
117};
118
119/// ObjCMethodDecl - Represents an instance or class method declaration.
120/// ObjC methods can be declared within 4 contexts: class interfaces,
121/// categories, protocols, and class implementations. While C++ member
122/// functions leverage C syntax, Objective-C method syntax is modeled after
123/// Smalltalk (using colons to specify argument types/expressions).
124/// Here are some brief examples:
125///
126/// Setter/getter instance methods:
127/// - (void)setMenu:(NSMenu *)menu;
128/// - (NSMenu *)menu;
129///
130/// Instance method that takes 2 NSView arguments:
131/// - (void)replaceSubview:(NSView *)oldView with:(NSView *)newView;
132///
133/// Getter class method:
134/// + (NSMenu *)defaultMenu;
135///
136/// A selector represents a unique name for a method. The selector names for
137/// the above methods are setMenu:, menu, replaceSubview:with:, and defaultMenu.
138///
139class ObjCMethodDecl : public NamedDecl, public DeclContext {
140 // This class stores some data in DeclContext::ObjCMethodDeclBits
141 // to save some space. Use the provided accessors to access it.
142
143public:
144 enum ImplementationControl { None, Required, Optional };
145
146private:
147 /// Return type of this method.
148 QualType MethodDeclType;
149
150 /// Type source information for the return type.
151 TypeSourceInfo *ReturnTInfo;
152
153 /// Array of ParmVarDecls for the formal parameters of this method
154 /// and optionally followed by selector locations.
155 void *ParamsAndSelLocs = nullptr;
156 unsigned NumParams = 0;
157
158 /// List of attributes for this method declaration.
159 SourceLocation DeclEndLoc; // the location of the ';' or '{'.
160
161 /// The following are only used for method definitions, null otherwise.
162 LazyDeclStmtPtr Body;
163
164 /// SelfDecl - Decl for the implicit self parameter. This is lazily
165 /// constructed by createImplicitParams.
166 ImplicitParamDecl *SelfDecl = nullptr;
167
168 /// CmdDecl - Decl for the implicit _cmd parameter. This is lazily
169 /// constructed by createImplicitParams.
170 ImplicitParamDecl *CmdDecl = nullptr;
171
172 ObjCMethodDecl(SourceLocation beginLoc, SourceLocation endLoc,
173 Selector SelInfo, QualType T, TypeSourceInfo *ReturnTInfo,
174 DeclContext *contextDecl, bool isInstance = true,
175 bool isVariadic = false, bool isPropertyAccessor = false,
176 bool isSynthesizedAccessorStub = false,
177 bool isImplicitlyDeclared = false, bool isDefined = false,
178 ImplementationControl impControl = None,
179 bool HasRelatedResultType = false);
180
181 SelectorLocationsKind getSelLocsKind() const {
182 return static_cast<SelectorLocationsKind>(ObjCMethodDeclBits.SelLocsKind);
183 }
184
185 void setSelLocsKind(SelectorLocationsKind Kind) {
186 ObjCMethodDeclBits.SelLocsKind = Kind;
187 }
188
189 bool hasStandardSelLocs() const {
190 return getSelLocsKind() != SelLoc_NonStandard;
191 }
192
193 /// Get a pointer to the stored selector identifiers locations array.
194 /// No locations will be stored if HasStandardSelLocs is true.
195 SourceLocation *getStoredSelLocs() {
196 return reinterpret_cast<SourceLocation *>(getParams() + NumParams);
197 }
198 const SourceLocation *getStoredSelLocs() const {
199 return reinterpret_cast<const SourceLocation *>(getParams() + NumParams);
200 }
201
202 /// Get a pointer to the stored selector identifiers locations array.
203 /// No locations will be stored if HasStandardSelLocs is true.
204 ParmVarDecl **getParams() {
205 return reinterpret_cast<ParmVarDecl **>(ParamsAndSelLocs);
206 }
207 const ParmVarDecl *const *getParams() const {
208 return reinterpret_cast<const ParmVarDecl *const *>(ParamsAndSelLocs);
209 }
210
211 /// Get the number of stored selector identifiers locations.
212 /// No locations will be stored if HasStandardSelLocs is true.
213 unsigned getNumStoredSelLocs() const {
214 if (hasStandardSelLocs())
215 return 0;
216 return getNumSelectorLocs();
217 }
218
219 void setParamsAndSelLocs(ASTContext &C,
220 ArrayRef<ParmVarDecl*> Params,
221 ArrayRef<SourceLocation> SelLocs);
222
223 /// A definition will return its interface declaration.
224 /// An interface declaration will return its definition.
225 /// Otherwise it will return itself.
226 ObjCMethodDecl *getNextRedeclarationImpl() override;
227
228public:
229 friend class ASTDeclReader;
230 friend class ASTDeclWriter;
231
232 static ObjCMethodDecl *
233 Create(ASTContext &C, SourceLocation beginLoc, SourceLocation endLoc,
234 Selector SelInfo, QualType T, TypeSourceInfo *ReturnTInfo,
235 DeclContext *contextDecl, bool isInstance = true,
236 bool isVariadic = false, bool isPropertyAccessor = false,
237 bool isSynthesizedAccessorStub = false,
238 bool isImplicitlyDeclared = false, bool isDefined = false,
239 ImplementationControl impControl = None,
240 bool HasRelatedResultType = false);
241
242 static ObjCMethodDecl *CreateDeserialized(ASTContext &C, unsigned ID);
243
244 ObjCMethodDecl *getCanonicalDecl() override;
245 const ObjCMethodDecl *getCanonicalDecl() const {
246 return const_cast<ObjCMethodDecl*>(this)->getCanonicalDecl();
247 }
248
249 ObjCDeclQualifier getObjCDeclQualifier() const {
250 return static_cast<ObjCDeclQualifier>(ObjCMethodDeclBits.objcDeclQualifier);
251 }
252
253 void setObjCDeclQualifier(ObjCDeclQualifier QV) {
254 ObjCMethodDeclBits.objcDeclQualifier = QV;
255 }
256
257 /// Determine whether this method has a result type that is related
258 /// to the message receiver's type.
259 bool hasRelatedResultType() const {
260 return ObjCMethodDeclBits.RelatedResultType;
261 }
262
263 /// Note whether this method has a related result type.
264 void setRelatedResultType(bool RRT = true) {
265 ObjCMethodDeclBits.RelatedResultType = RRT;
266 }
267
268 /// True if this is a method redeclaration in the same interface.
269 bool isRedeclaration() const { return ObjCMethodDeclBits.IsRedeclaration; }
270 void setIsRedeclaration(bool RD) { ObjCMethodDeclBits.IsRedeclaration = RD; }
271 void setAsRedeclaration(const ObjCMethodDecl *PrevMethod);
272
273 /// True if redeclared in the same interface.
274 bool hasRedeclaration() const { return ObjCMethodDeclBits.HasRedeclaration; }
275 void setHasRedeclaration(bool HRD) const {
276 ObjCMethodDeclBits.HasRedeclaration = HRD;
277 }
278
279 /// Returns the location where the declarator ends. It will be
280 /// the location of ';' for a method declaration and the location of '{'
281 /// for a method definition.
282 SourceLocation getDeclaratorEndLoc() const { return DeclEndLoc; }
283
284 // Location information, modeled after the Stmt API.
285 SourceLocation getBeginLoc() const LLVM_READONLY__attribute__((__pure__)) { return getLocation(); }
286 SourceLocation getEndLoc() const LLVM_READONLY__attribute__((__pure__));
287 SourceRange getSourceRange() const override LLVM_READONLY__attribute__((__pure__)) {
288 return SourceRange(getLocation(), getEndLoc());
289 }
290
291 SourceLocation getSelectorStartLoc() const {
292 if (isImplicit())
293 return getBeginLoc();
294 return getSelectorLoc(0);
295 }
296
297 SourceLocation getSelectorLoc(unsigned Index) const {
298 assert(Index < getNumSelectorLocs() && "Index out of range!")(static_cast <bool> (Index < getNumSelectorLocs() &&
"Index out of range!") ? void (0) : __assert_fail ("Index < getNumSelectorLocs() && \"Index out of range!\""
, "/build/llvm-toolchain-snapshot-14~++20211016100712+8e1d532707fd/clang/include/clang/AST/DeclObjC.h"
, 298, __extension__ __PRETTY_FUNCTION__))
;
299 if (hasStandardSelLocs())
300 return getStandardSelectorLoc(Index, getSelector(),
301 getSelLocsKind() == SelLoc_StandardWithSpace,
302 parameters(),
303 DeclEndLoc);
304 return getStoredSelLocs()[Index];
305 }
306
307 void getSelectorLocs(SmallVectorImpl<SourceLocation> &SelLocs) const;
308
309 unsigned getNumSelectorLocs() const {
310 if (isImplicit())
311 return 0;
312 Selector Sel = getSelector();
313 if (Sel.isUnarySelector())
314 return 1;
315 return Sel.getNumArgs();
316 }
317
318 ObjCInterfaceDecl *getClassInterface();
319 const ObjCInterfaceDecl *getClassInterface() const {
320 return const_cast<ObjCMethodDecl*>(this)->getClassInterface();
321 }
322
323 /// If this method is declared or implemented in a category, return
324 /// that category.
325 ObjCCategoryDecl *getCategory();
326 const ObjCCategoryDecl *getCategory() const {
327 return const_cast<ObjCMethodDecl*>(this)->getCategory();
328 }
329
330 Selector getSelector() const { return getDeclName().getObjCSelector(); }
331
332 QualType getReturnType() const { return MethodDeclType; }
333 void setReturnType(QualType T) { MethodDeclType = T; }
334 SourceRange getReturnTypeSourceRange() const;
335
336 /// Determine the type of an expression that sends a message to this
337 /// function. This replaces the type parameters with the types they would
338 /// get if the receiver was parameterless (e.g. it may replace the type
339 /// parameter with 'id').
340 QualType getSendResultType() const;
341
342 /// Determine the type of an expression that sends a message to this
343 /// function with the given receiver type.
344 QualType getSendResultType(QualType receiverType) const;
345
346 TypeSourceInfo *getReturnTypeSourceInfo() const { return ReturnTInfo; }
347 void setReturnTypeSourceInfo(TypeSourceInfo *TInfo) { ReturnTInfo = TInfo; }
348
349 // Iterator access to formal parameters.
350 unsigned param_size() const { return NumParams; }
351
352 using param_const_iterator = const ParmVarDecl *const *;
353 using param_iterator = ParmVarDecl *const *;
354 using param_range = llvm::iterator_range<param_iterator>;
355 using param_const_range = llvm::iterator_range<param_const_iterator>;
356
357 param_const_iterator param_begin() const {
358 return param_const_iterator(getParams());
359 }
360
361 param_const_iterator param_end() const {
362 return param_const_iterator(getParams() + NumParams);
363 }
364
365 param_iterator param_begin() { return param_iterator(getParams()); }
366 param_iterator param_end() { return param_iterator(getParams() + NumParams); }
367
368 // This method returns and of the parameters which are part of the selector
369 // name mangling requirements.
370 param_const_iterator sel_param_end() const {
371 return param_begin() + getSelector().getNumArgs();
372 }
373
374 // ArrayRef access to formal parameters. This should eventually
375 // replace the iterator interface above.
376 ArrayRef<ParmVarDecl*> parameters() const {
377 return llvm::makeArrayRef(const_cast<ParmVarDecl**>(getParams()),
378 NumParams);
379 }
380
381 ParmVarDecl *getParamDecl(unsigned Idx) {
382 assert(Idx < NumParams && "Index out of bounds!")(static_cast <bool> (Idx < NumParams && "Index out of bounds!"
) ? void (0) : __assert_fail ("Idx < NumParams && \"Index out of bounds!\""
, "/build/llvm-toolchain-snapshot-14~++20211016100712+8e1d532707fd/clang/include/clang/AST/DeclObjC.h"
, 382, __extension__ __PRETTY_FUNCTION__))
;
383 return getParams()[Idx];
384 }
385 const ParmVarDecl *getParamDecl(unsigned Idx) const {
386 return const_cast<ObjCMethodDecl *>(this)->getParamDecl(Idx);
387 }
388
389 /// Sets the method's parameters and selector source locations.
390 /// If the method is implicit (not coming from source) \p SelLocs is
391 /// ignored.
392 void setMethodParams(ASTContext &C,
393 ArrayRef<ParmVarDecl*> Params,
394 ArrayRef<SourceLocation> SelLocs = llvm::None);
395
396 // Iterator access to parameter types.
397 struct GetTypeFn {
398 QualType operator()(const ParmVarDecl *PD) const { return PD->getType(); }
399 };
400
401 using param_type_iterator =
402 llvm::mapped_iterator<param_const_iterator, GetTypeFn>;
403
404 param_type_iterator param_type_begin() const {
405 return llvm::map_iterator(param_begin(), GetTypeFn());
406 }
407
408 param_type_iterator param_type_end() const {
409 return llvm::map_iterator(param_end(), GetTypeFn());
410 }
411
412 /// createImplicitParams - Used to lazily create the self and cmd
413 /// implicit parameters. This must be called prior to using getSelfDecl()
414 /// or getCmdDecl(). The call is ignored if the implicit parameters
415 /// have already been created.
416 void createImplicitParams(ASTContext &Context, const ObjCInterfaceDecl *ID);
417
418 /// \return the type for \c self and set \arg selfIsPseudoStrong and
419 /// \arg selfIsConsumed accordingly.
420 QualType getSelfType(ASTContext &Context, const ObjCInterfaceDecl *OID,
421 bool &selfIsPseudoStrong, bool &selfIsConsumed) const;
422
423 ImplicitParamDecl * getSelfDecl() const { return SelfDecl; }
424 void setSelfDecl(ImplicitParamDecl *SD) { SelfDecl = SD; }
425 ImplicitParamDecl * getCmdDecl() const { return CmdDecl; }
426 void setCmdDecl(ImplicitParamDecl *CD) { CmdDecl = CD; }
427
428 /// Determines the family of this method.
429 ObjCMethodFamily getMethodFamily() const;
430
431 bool isInstanceMethod() const { return ObjCMethodDeclBits.IsInstance; }
432 void setInstanceMethod(bool isInst) {
433 ObjCMethodDeclBits.IsInstance = isInst;
434 }
435
436 bool isVariadic() const { return ObjCMethodDeclBits.IsVariadic; }
437 void setVariadic(bool isVar) { ObjCMethodDeclBits.IsVariadic = isVar; }
438
439 bool isClassMethod() const { return !isInstanceMethod(); }
440
441 bool isPropertyAccessor() const {
442 return ObjCMethodDeclBits.IsPropertyAccessor;
443 }
444
445 void setPropertyAccessor(bool isAccessor) {
446 ObjCMethodDeclBits.IsPropertyAccessor = isAccessor;
447 }
448
449 bool isSynthesizedAccessorStub() const {
450 return ObjCMethodDeclBits.IsSynthesizedAccessorStub;
451 }
452
453 void setSynthesizedAccessorStub(bool isSynthesizedAccessorStub) {
454 ObjCMethodDeclBits.IsSynthesizedAccessorStub = isSynthesizedAccessorStub;
455 }
456
457 bool isDefined() const { return ObjCMethodDeclBits.IsDefined; }
458 void setDefined(bool isDefined) { ObjCMethodDeclBits.IsDefined = isDefined; }
459
460 /// Whether this method overrides any other in the class hierarchy.
461 ///
462 /// A method is said to override any method in the class's
463 /// base classes, its protocols, or its categories' protocols, that has
464 /// the same selector and is of the same kind (class or instance).
465 /// A method in an implementation is not considered as overriding the same
466 /// method in the interface or its categories.
467 bool isOverriding() const { return ObjCMethodDeclBits.IsOverriding; }
468 void setOverriding(bool IsOver) { ObjCMethodDeclBits.IsOverriding = IsOver; }
469
470 /// Return overridden methods for the given \p Method.
471 ///
472 /// An ObjC method is considered to override any method in the class's
473 /// base classes (and base's categories), its protocols, or its categories'
474 /// protocols, that has
475 /// the same selector and is of the same kind (class or instance).
476 /// A method in an implementation is not considered as overriding the same
477 /// method in the interface or its categories.
478 void getOverriddenMethods(
479 SmallVectorImpl<const ObjCMethodDecl *> &Overridden) const;
480
481 /// True if the method was a definition but its body was skipped.
482 bool hasSkippedBody() const { return ObjCMethodDeclBits.HasSkippedBody; }
483 void setHasSkippedBody(bool Skipped = true) {
484 ObjCMethodDeclBits.HasSkippedBody = Skipped;
485 }
486
487 /// True if the method is tagged as objc_direct
488 bool isDirectMethod() const;
489
490 /// True if the method has a parameter that's destroyed in the callee.
491 bool hasParamDestroyedInCallee() const;
492
493 /// Returns the property associated with this method's selector.
494 ///
495 /// Note that even if this particular method is not marked as a property
496 /// accessor, it is still possible for it to match a property declared in a
497 /// superclass. Pass \c false if you only want to check the current class.
498 const ObjCPropertyDecl *findPropertyDecl(bool CheckOverrides = true) const;
499
500 // Related to protocols declared in \@protocol
501 void setDeclImplementation(ImplementationControl ic) {
502 ObjCMethodDeclBits.DeclImplementation = ic;
503 }
504
505 ImplementationControl getImplementationControl() const {
506 return ImplementationControl(ObjCMethodDeclBits.DeclImplementation);
507 }
508
509 bool isOptional() const {
510 return getImplementationControl() == Optional;
511 }
512
513 /// Returns true if this specific method declaration is marked with the
514 /// designated initializer attribute.
515 bool isThisDeclarationADesignatedInitializer() const;
516
517 /// Returns true if the method selector resolves to a designated initializer
518 /// in the class's interface.
519 ///
520 /// \param InitMethod if non-null and the function returns true, it receives
521 /// the method declaration that was marked with the designated initializer
522 /// attribute.
523 bool isDesignatedInitializerForTheInterface(
524 const ObjCMethodDecl **InitMethod = nullptr) const;
525
526 /// Determine whether this method has a body.
527 bool hasBody() const override { return Body.isValid(); }
528
529 /// Retrieve the body of this method, if it has one.
530 Stmt *getBody() const override;
531
532 void setLazyBody(uint64_t Offset) { Body = Offset; }
533
534 CompoundStmt *getCompoundBody() { return (CompoundStmt*)getBody(); }
535 void setBody(Stmt *B) { Body = B; }
536
537 /// Returns whether this specific method is a definition.
538 bool isThisDeclarationADefinition() const { return hasBody(); }
539
540 /// Is this method defined in the NSObject base class?
541 bool definedInNSObject(const ASTContext &) const;
542
543 // Implement isa/cast/dyncast/etc.
544 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
545 static bool classofKind(Kind K) { return K == ObjCMethod; }
546
547 static DeclContext *castToDeclContext(const ObjCMethodDecl *D) {
548 return static_cast<DeclContext *>(const_cast<ObjCMethodDecl*>(D));
549 }
550
551 static ObjCMethodDecl *castFromDeclContext(const DeclContext *DC) {
552 return static_cast<ObjCMethodDecl *>(const_cast<DeclContext*>(DC));
553 }
554};
555
556/// Describes the variance of a given generic parameter.
557enum class ObjCTypeParamVariance : uint8_t {
558 /// The parameter is invariant: must match exactly.
559 Invariant,
560
561 /// The parameter is covariant, e.g., X<T> is a subtype of X<U> when
562 /// the type parameter is covariant and T is a subtype of U.
563 Covariant,
564
565 /// The parameter is contravariant, e.g., X<T> is a subtype of X<U>
566 /// when the type parameter is covariant and U is a subtype of T.
567 Contravariant,
568};
569
570/// Represents the declaration of an Objective-C type parameter.
571///
572/// \code
573/// @interface NSDictionary<Key : id<NSCopying>, Value>
574/// @end
575/// \endcode
576///
577/// In the example above, both \c Key and \c Value are represented by
578/// \c ObjCTypeParamDecl. \c Key has an explicit bound of \c id<NSCopying>,
579/// while \c Value gets an implicit bound of \c id.
580///
581/// Objective-C type parameters are typedef-names in the grammar,
582class ObjCTypeParamDecl : public TypedefNameDecl {
583 /// Index of this type parameter in the type parameter list.
584 unsigned Index : 14;
585
586 /// The variance of the type parameter.
587 unsigned Variance : 2;
588
589 /// The location of the variance, if any.
590 SourceLocation VarianceLoc;
591
592 /// The location of the ':', which will be valid when the bound was
593 /// explicitly specified.
594 SourceLocation ColonLoc;
595
596 ObjCTypeParamDecl(ASTContext &ctx, DeclContext *dc,
597 ObjCTypeParamVariance variance, SourceLocation varianceLoc,
598 unsigned index,
599 SourceLocation nameLoc, IdentifierInfo *name,
600 SourceLocation colonLoc, TypeSourceInfo *boundInfo)
601 : TypedefNameDecl(ObjCTypeParam, ctx, dc, nameLoc, nameLoc, name,
602 boundInfo),
603 Index(index), Variance(static_cast<unsigned>(variance)),
604 VarianceLoc(varianceLoc), ColonLoc(colonLoc) {}
605
606 void anchor() override;
607
608public:
609 friend class ASTDeclReader;
610 friend class ASTDeclWriter;
611
612 static ObjCTypeParamDecl *Create(ASTContext &ctx, DeclContext *dc,
613 ObjCTypeParamVariance variance,
614 SourceLocation varianceLoc,
615 unsigned index,
616 SourceLocation nameLoc,
617 IdentifierInfo *name,
618 SourceLocation colonLoc,
619 TypeSourceInfo *boundInfo);
620 static ObjCTypeParamDecl *CreateDeserialized(ASTContext &ctx, unsigned ID);
621
622 SourceRange getSourceRange() const override LLVM_READONLY__attribute__((__pure__));
623
624 /// Determine the variance of this type parameter.
625 ObjCTypeParamVariance getVariance() const {
626 return static_cast<ObjCTypeParamVariance>(Variance);
627 }
628
629 /// Set the variance of this type parameter.
630 void setVariance(ObjCTypeParamVariance variance) {
631 Variance = static_cast<unsigned>(variance);
632 }
633
634 /// Retrieve the location of the variance keyword.
635 SourceLocation getVarianceLoc() const { return VarianceLoc; }
636
637 /// Retrieve the index into its type parameter list.
638 unsigned getIndex() const { return Index; }
639
640 /// Whether this type parameter has an explicitly-written type bound, e.g.,
641 /// "T : NSView".
642 bool hasExplicitBound() const { return ColonLoc.isValid(); }
643
644 /// Retrieve the location of the ':' separating the type parameter name
645 /// from the explicitly-specified bound.
646 SourceLocation getColonLoc() const { return ColonLoc; }
647
648 // Implement isa/cast/dyncast/etc.
649 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
650 static bool classofKind(Kind K) { return K == ObjCTypeParam; }
651};
652
653/// Stores a list of Objective-C type parameters for a parameterized class
654/// or a category/extension thereof.
655///
656/// \code
657/// @interface NSArray<T> // stores the <T>
658/// @end
659/// \endcode
660class ObjCTypeParamList final
661 : private llvm::TrailingObjects<ObjCTypeParamList, ObjCTypeParamDecl *> {
662 /// Location of the left and right angle brackets.
663 SourceRange Brackets;
664 /// The number of parameters in the list, which are tail-allocated.
665 unsigned NumParams;
666
667 ObjCTypeParamList(SourceLocation lAngleLoc,
668 ArrayRef<ObjCTypeParamDecl *> typeParams,
669 SourceLocation rAngleLoc);
670
671public:
672 friend TrailingObjects;
673
674 /// Create a new Objective-C type parameter list.
675 static ObjCTypeParamList *create(ASTContext &ctx,
676 SourceLocation lAngleLoc,
677 ArrayRef<ObjCTypeParamDecl *> typeParams,
678 SourceLocation rAngleLoc);
679
680 /// Iterate through the type parameters in the list.
681 using iterator = ObjCTypeParamDecl **;
682
683 iterator begin() { return getTrailingObjects<ObjCTypeParamDecl *>(); }
684
685 iterator end() { return begin() + size(); }
686
687 /// Determine the number of type parameters in this list.
688 unsigned size() const { return NumParams; }
689
690 // Iterate through the type parameters in the list.
691 using const_iterator = ObjCTypeParamDecl * const *;
692
693 const_iterator begin() const {
694 return getTrailingObjects<ObjCTypeParamDecl *>();
695 }
696
697 const_iterator end() const {
698 return begin() + size();
699 }
700
701 ObjCTypeParamDecl *front() const {
702 assert(size() > 0 && "empty Objective-C type parameter list")(static_cast <bool> (size() > 0 && "empty Objective-C type parameter list"
) ? void (0) : __assert_fail ("size() > 0 && \"empty Objective-C type parameter list\""
, "/build/llvm-toolchain-snapshot-14~++20211016100712+8e1d532707fd/clang/include/clang/AST/DeclObjC.h"
, 702, __extension__ __PRETTY_FUNCTION__))
;
703 return *begin();
704 }
705
706 ObjCTypeParamDecl *back() const {
707 assert(size() > 0 && "empty Objective-C type parameter list")(static_cast <bool> (size() > 0 && "empty Objective-C type parameter list"
) ? void (0) : __assert_fail ("size() > 0 && \"empty Objective-C type parameter list\""
, "/build/llvm-toolchain-snapshot-14~++20211016100712+8e1d532707fd/clang/include/clang/AST/DeclObjC.h"
, 707, __extension__ __PRETTY_FUNCTION__))
;
708 return *(end() - 1);
709 }
710
711 SourceLocation getLAngleLoc() const { return Brackets.getBegin(); }
712 SourceLocation getRAngleLoc() const { return Brackets.getEnd(); }
713 SourceRange getSourceRange() const { return Brackets; }
714
715 /// Gather the default set of type arguments to be substituted for
716 /// these type parameters when dealing with an unspecialized type.
717 void gatherDefaultTypeArgs(SmallVectorImpl<QualType> &typeArgs) const;
718};
719
720enum class ObjCPropertyQueryKind : uint8_t {
721 OBJC_PR_query_unknown = 0x00,
722 OBJC_PR_query_instance,
723 OBJC_PR_query_class
724};
725
726/// Represents one property declaration in an Objective-C interface.
727///
728/// For example:
729/// \code{.mm}
730/// \@property (assign, readwrite) int MyProperty;
731/// \endcode
732class ObjCPropertyDecl : public NamedDecl {
733 void anchor() override;
734
735public:
736 enum SetterKind { Assign, Retain, Copy, Weak };
737 enum PropertyControl { None, Required, Optional };
738
739private:
740 // location of \@property
741 SourceLocation AtLoc;
742
743 // location of '(' starting attribute list or null.
744 SourceLocation LParenLoc;
745
746 QualType DeclType;
747 TypeSourceInfo *DeclTypeSourceInfo;
748 unsigned PropertyAttributes : NumObjCPropertyAttrsBits;
749 unsigned PropertyAttributesAsWritten : NumObjCPropertyAttrsBits;
750
751 // \@required/\@optional
752 unsigned PropertyImplementation : 2;
753
754 // getter name of NULL if no getter
755 Selector GetterName;
756
757 // setter name of NULL if no setter
758 Selector SetterName;
759
760 // location of the getter attribute's value
761 SourceLocation GetterNameLoc;
762
763 // location of the setter attribute's value
764 SourceLocation SetterNameLoc;
765
766 // Declaration of getter instance method
767 ObjCMethodDecl *GetterMethodDecl = nullptr;
768
769 // Declaration of setter instance method
770 ObjCMethodDecl *SetterMethodDecl = nullptr;
771
772 // Synthesize ivar for this property
773 ObjCIvarDecl *PropertyIvarDecl = nullptr;
774
775 ObjCPropertyDecl(DeclContext *DC, SourceLocation L, IdentifierInfo *Id,
776 SourceLocation AtLocation, SourceLocation LParenLocation,
777 QualType T, TypeSourceInfo *TSI, PropertyControl propControl)
778 : NamedDecl(ObjCProperty, DC, L, Id), AtLoc(AtLocation),
779 LParenLoc(LParenLocation), DeclType(T), DeclTypeSourceInfo(TSI),
780 PropertyAttributes(ObjCPropertyAttribute::kind_noattr),
781 PropertyAttributesAsWritten(ObjCPropertyAttribute::kind_noattr),
782 PropertyImplementation(propControl), GetterName(Selector()),
783 SetterName(Selector()) {}
784
785public:
786 static ObjCPropertyDecl *Create(ASTContext &C, DeclContext *DC,
787 SourceLocation L,
788 IdentifierInfo *Id, SourceLocation AtLocation,
789 SourceLocation LParenLocation,
790 QualType T,
791 TypeSourceInfo *TSI,
792 PropertyControl propControl = None);
793
794 static ObjCPropertyDecl *CreateDeserialized(ASTContext &C, unsigned ID);
795
796 SourceLocation getAtLoc() const { return AtLoc; }
797 void setAtLoc(SourceLocation L) { AtLoc = L; }
798
799 SourceLocation getLParenLoc() const { return LParenLoc; }
800 void setLParenLoc(SourceLocation L) { LParenLoc = L; }
801
802 TypeSourceInfo *getTypeSourceInfo() const { return DeclTypeSourceInfo; }
803
804 QualType getType() const { return DeclType; }
805
806 void setType(QualType T, TypeSourceInfo *TSI) {
807 DeclType = T;
808 DeclTypeSourceInfo = TSI;
809 }
810
811 /// Retrieve the type when this property is used with a specific base object
812 /// type.
813 QualType getUsageType(QualType objectType) const;
814
815 ObjCPropertyAttribute::Kind getPropertyAttributes() const {
816 return ObjCPropertyAttribute::Kind(PropertyAttributes);
817 }
818
819 void setPropertyAttributes(ObjCPropertyAttribute::Kind PRVal) {
820 PropertyAttributes |= PRVal;
821 }
822
823 void overwritePropertyAttributes(unsigned PRVal) {
824 PropertyAttributes = PRVal;
825 }
826
827 ObjCPropertyAttribute::Kind getPropertyAttributesAsWritten() const {
828 return ObjCPropertyAttribute::Kind(PropertyAttributesAsWritten);
829 }
830
831 void setPropertyAttributesAsWritten(ObjCPropertyAttribute::Kind PRVal) {
832 PropertyAttributesAsWritten = PRVal;
833 }
834
835 // Helper methods for accessing attributes.
836
837 /// isReadOnly - Return true iff the property has a setter.
838 bool isReadOnly() const {
839 return (PropertyAttributes & ObjCPropertyAttribute::kind_readonly);
840 }
841
842 /// isAtomic - Return true if the property is atomic.
843 bool isAtomic() const {
844 return (PropertyAttributes & ObjCPropertyAttribute::kind_atomic);
845 }
846
847 /// isRetaining - Return true if the property retains its value.
848 bool isRetaining() const {
849 return (PropertyAttributes & (ObjCPropertyAttribute::kind_retain |
850 ObjCPropertyAttribute::kind_strong |
851 ObjCPropertyAttribute::kind_copy));
852 }
853
854 bool isInstanceProperty() const { return !isClassProperty(); }
855 bool isClassProperty() const {
856 return PropertyAttributes & ObjCPropertyAttribute::kind_class;
857 }
858 bool isDirectProperty() const;
859
860 ObjCPropertyQueryKind getQueryKind() const {
861 return isClassProperty() ? ObjCPropertyQueryKind::OBJC_PR_query_class :
862 ObjCPropertyQueryKind::OBJC_PR_query_instance;
863 }
864
865 static ObjCPropertyQueryKind getQueryKind(bool isClassProperty) {
866 return isClassProperty ? ObjCPropertyQueryKind::OBJC_PR_query_class :
867 ObjCPropertyQueryKind::OBJC_PR_query_instance;
868 }
869
870 /// getSetterKind - Return the method used for doing assignment in
871 /// the property setter. This is only valid if the property has been
872 /// defined to have a setter.
873 SetterKind getSetterKind() const {
874 if (PropertyAttributes & ObjCPropertyAttribute::kind_strong)
875 return getType()->isBlockPointerType() ? Copy : Retain;
876 if (PropertyAttributes & ObjCPropertyAttribute::kind_retain)
877 return Retain;
878 if (PropertyAttributes & ObjCPropertyAttribute::kind_copy)
879 return Copy;
880 if (PropertyAttributes & ObjCPropertyAttribute::kind_weak)
881 return Weak;
882 return Assign;
883 }
884
885 Selector getGetterName() const { return GetterName; }
886 SourceLocation getGetterNameLoc() const { return GetterNameLoc; }
887
888 void setGetterName(Selector Sel, SourceLocation Loc = SourceLocation()) {
889 GetterName = Sel;
890 GetterNameLoc = Loc;
891 }
892
893 Selector getSetterName() const { return SetterName; }
894 SourceLocation getSetterNameLoc() const { return SetterNameLoc; }
895
896 void setSetterName(Selector Sel, SourceLocation Loc = SourceLocation()) {
897 SetterName = Sel;
898 SetterNameLoc = Loc;
899 }
900
901 ObjCMethodDecl *getGetterMethodDecl() const { return GetterMethodDecl; }
902 void setGetterMethodDecl(ObjCMethodDecl *gDecl) { GetterMethodDecl = gDecl; }
903
904 ObjCMethodDecl *getSetterMethodDecl() const { return SetterMethodDecl; }
905 void setSetterMethodDecl(ObjCMethodDecl *gDecl) { SetterMethodDecl = gDecl; }
906
907 // Related to \@optional/\@required declared in \@protocol
908 void setPropertyImplementation(PropertyControl pc) {
909 PropertyImplementation = pc;
910 }
911
912 PropertyControl getPropertyImplementation() const {
913 return PropertyControl(PropertyImplementation);
914 }
915
916 bool isOptional() const {
917 return getPropertyImplementation() == PropertyControl::Optional;
918 }
919
920 void setPropertyIvarDecl(ObjCIvarDecl *Ivar) {
921 PropertyIvarDecl = Ivar;
922 }
923
924 ObjCIvarDecl *getPropertyIvarDecl() const {
925 return PropertyIvarDecl;
926 }
927
928 SourceRange getSourceRange() const override LLVM_READONLY__attribute__((__pure__)) {
929 return SourceRange(AtLoc, getLocation());
930 }
931
932 /// Get the default name of the synthesized ivar.
933 IdentifierInfo *getDefaultSynthIvarName(ASTContext &Ctx) const;
934
935 /// Lookup a property by name in the specified DeclContext.
936 static ObjCPropertyDecl *findPropertyDecl(const DeclContext *DC,
937 const IdentifierInfo *propertyID,
938 ObjCPropertyQueryKind queryKind);
939
940 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
941 static bool classofKind(Kind K) { return K == ObjCProperty; }
942};
943
944/// ObjCContainerDecl - Represents a container for method declarations.
945/// Current sub-classes are ObjCInterfaceDecl, ObjCCategoryDecl,
946/// ObjCProtocolDecl, and ObjCImplDecl.
947///
948class ObjCContainerDecl : public NamedDecl, public DeclContext {
949 // This class stores some data in DeclContext::ObjCContainerDeclBits
950 // to save some space. Use the provided accessors to access it.
951
952 // These two locations in the range mark the end of the method container.
953 // The first points to the '@' token, and the second to the 'end' token.
954 SourceRange AtEnd;
955
956 void anchor() override;
957
958public:
959 ObjCContainerDecl(Kind DK, DeclContext *DC, IdentifierInfo *Id,
960 SourceLocation nameLoc, SourceLocation atStartLoc);
961
962 // Iterator access to instance/class properties.
963 using prop_iterator = specific_decl_iterator<ObjCPropertyDecl>;
964 using prop_range =
965 llvm::iterator_range<specific_decl_iterator<ObjCPropertyDecl>>;
966
967 prop_range properties() const { return prop_range(prop_begin(), prop_end()); }
968
969 prop_iterator prop_begin() const {
970 return prop_iterator(decls_begin());
971 }
972
973 prop_iterator prop_end() const {
974 return prop_iterator(decls_end());
975 }
976
977 using instprop_iterator =
978 filtered_decl_iterator<ObjCPropertyDecl,
979 &ObjCPropertyDecl::isInstanceProperty>;
980 using instprop_range = llvm::iterator_range<instprop_iterator>;
981
982 instprop_range instance_properties() const {
983 return instprop_range(instprop_begin(), instprop_end());
984 }
985
986 instprop_iterator instprop_begin() const {
987 return instprop_iterator(decls_begin());
988 }
989
990 instprop_iterator instprop_end() const {
991 return instprop_iterator(decls_end());
992 }
993
994 using classprop_iterator =
995 filtered_decl_iterator<ObjCPropertyDecl,
996 &ObjCPropertyDecl::isClassProperty>;
997 using classprop_range = llvm::iterator_range<classprop_iterator>;
998
999 classprop_range class_properties() const {
1000 return classprop_range(classprop_begin(), classprop_end());
1001 }
1002
1003 classprop_iterator classprop_begin() const {
1004 return classprop_iterator(decls_begin());
1005 }
1006
1007 classprop_iterator classprop_end() const {
1008 return classprop_iterator(decls_end());
1009 }
1010
1011 // Iterator access to instance/class methods.
1012 using method_iterator = specific_decl_iterator<ObjCMethodDecl>;
1013 using method_range =
1014 llvm::iterator_range<specific_decl_iterator<ObjCMethodDecl>>;
1015
1016 method_range methods() const {
1017 return method_range(meth_begin(), meth_end());
1018 }
1019
1020 method_iterator meth_begin() const {
1021 return method_iterator(decls_begin());
1022 }
1023
1024 method_iterator meth_end() const {
1025 return method_iterator(decls_end());
1026 }
1027
1028 using instmeth_iterator =
1029 filtered_decl_iterator<ObjCMethodDecl,
1030 &ObjCMethodDecl::isInstanceMethod>;
1031 using instmeth_range = llvm::iterator_range<instmeth_iterator>;
1032
1033 instmeth_range instance_methods() const {
1034 return instmeth_range(instmeth_begin(), instmeth_end());
1035 }
1036
1037 instmeth_iterator instmeth_begin() const {
1038 return instmeth_iterator(decls_begin());
1039 }
1040
1041 instmeth_iterator instmeth_end() const {
1042 return instmeth_iterator(decls_end());
1043 }
1044
1045 using classmeth_iterator =
1046 filtered_decl_iterator<ObjCMethodDecl,
1047 &ObjCMethodDecl::isClassMethod>;
1048 using classmeth_range = llvm::iterator_range<classmeth_iterator>;
1049
1050 classmeth_range class_methods() const {
1051 return classmeth_range(classmeth_begin(), classmeth_end());
1052 }
1053
1054 classmeth_iterator classmeth_begin() const {
1055 return classmeth_iterator(decls_begin());
1056 }
1057
1058 classmeth_iterator classmeth_end() const {
1059 return classmeth_iterator(decls_end());
1060 }
1061
1062 // Get the local instance/class method declared in this interface.
1063 ObjCMethodDecl *getMethod(Selector Sel, bool isInstance,
1064 bool AllowHidden = false) const;
1065
1066 ObjCMethodDecl *getInstanceMethod(Selector Sel,
1067 bool AllowHidden = false) const {
1068 return getMethod(Sel, true/*isInstance*/, AllowHidden);
1069 }
1070
1071 ObjCMethodDecl *getClassMethod(Selector Sel, bool AllowHidden = false) const {
1072 return getMethod(Sel, false/*isInstance*/, AllowHidden);
1073 }
1074
1075 bool HasUserDeclaredSetterMethod(const ObjCPropertyDecl *P) const;
1076 ObjCIvarDecl *getIvarDecl(IdentifierInfo *Id) const;
1077
1078 ObjCPropertyDecl *
1079 FindPropertyDeclaration(const IdentifierInfo *PropertyId,
1080 ObjCPropertyQueryKind QueryKind) const;
1081
1082 using PropertyMap =
1083 llvm::DenseMap<std::pair<IdentifierInfo *, unsigned/*isClassProperty*/>,
1084 ObjCPropertyDecl *>;
1085 using ProtocolPropertySet = llvm::SmallDenseSet<const ObjCProtocolDecl *, 8>;
1086 using PropertyDeclOrder = llvm::SmallVector<ObjCPropertyDecl *, 8>;
1087
1088 /// This routine collects list of properties to be implemented in the class.
1089 /// This includes, class's and its conforming protocols' properties.
1090 /// Note, the superclass's properties are not included in the list.
1091 virtual void collectPropertiesToImplement(PropertyMap &PM,
1092 PropertyDeclOrder &PO) const {}
1093
1094 SourceLocation getAtStartLoc() const { return ObjCContainerDeclBits.AtStart; }
1095
1096 void setAtStartLoc(SourceLocation Loc) {
1097 ObjCContainerDeclBits.AtStart = Loc;
1098 }
1099
1100 // Marks the end of the container.
1101 SourceRange getAtEndRange() const { return AtEnd; }
1102
1103 void setAtEndRange(SourceRange atEnd) { AtEnd = atEnd; }
1104
1105 SourceRange getSourceRange() const override LLVM_READONLY__attribute__((__pure__)) {
1106 return SourceRange(getAtStartLoc(), getAtEndRange().getEnd());
1107 }
1108
1109 // Implement isa/cast/dyncast/etc.
1110 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1111
1112 static bool classofKind(Kind K) {
1113 return K >= firstObjCContainer &&
1114 K <= lastObjCContainer;
1115 }
1116
1117 static DeclContext *castToDeclContext(const ObjCContainerDecl *D) {
1118 return static_cast<DeclContext *>(const_cast<ObjCContainerDecl*>(D));
1119 }
1120
1121 static ObjCContainerDecl *castFromDeclContext(const DeclContext *DC) {
1122 return static_cast<ObjCContainerDecl *>(const_cast<DeclContext*>(DC));
1123 }
1124};
1125
1126/// Represents an ObjC class declaration.
1127///
1128/// For example:
1129///
1130/// \code
1131/// // MostPrimitive declares no super class (not particularly useful).
1132/// \@interface MostPrimitive
1133/// // no instance variables or methods.
1134/// \@end
1135///
1136/// // NSResponder inherits from NSObject & implements NSCoding (a protocol).
1137/// \@interface NSResponder : NSObject \<NSCoding>
1138/// { // instance variables are represented by ObjCIvarDecl.
1139/// id nextResponder; // nextResponder instance variable.
1140/// }
1141/// - (NSResponder *)nextResponder; // return a pointer to NSResponder.
1142/// - (void)mouseMoved:(NSEvent *)theEvent; // return void, takes a pointer
1143/// \@end // to an NSEvent.
1144/// \endcode
1145///
1146/// Unlike C/C++, forward class declarations are accomplished with \@class.
1147/// Unlike C/C++, \@class allows for a list of classes to be forward declared.
1148/// Unlike C++, ObjC is a single-rooted class model. In Cocoa, classes
1149/// typically inherit from NSObject (an exception is NSProxy).
1150///
1151class ObjCInterfaceDecl : public ObjCContainerDecl
1152 , public Redeclarable<ObjCInterfaceDecl> {
1153 friend class ASTContext;
1154
1155 /// TypeForDecl - This indicates the Type object that represents this
1156 /// TypeDecl. It is a cache maintained by ASTContext::getObjCInterfaceType
1157 mutable const Type *TypeForDecl = nullptr;
1158
1159 struct DefinitionData {
1160 /// The definition of this class, for quick access from any
1161 /// declaration.
1162 ObjCInterfaceDecl *Definition = nullptr;
1163
1164 /// When non-null, this is always an ObjCObjectType.
1165 TypeSourceInfo *SuperClassTInfo = nullptr;
1166
1167 /// Protocols referenced in the \@interface declaration
1168 ObjCProtocolList ReferencedProtocols;
1169
1170 /// Protocols reference in both the \@interface and class extensions.
1171 ObjCList<ObjCProtocolDecl> AllReferencedProtocols;
1172
1173 /// List of categories and class extensions defined for this class.
1174 ///
1175 /// Categories are stored as a linked list in the AST, since the categories
1176 /// and class extensions come long after the initial interface declaration,
1177 /// and we avoid dynamically-resized arrays in the AST wherever possible.
1178 ObjCCategoryDecl *CategoryList = nullptr;
1179
1180 /// IvarList - List of all ivars defined by this class; including class
1181 /// extensions and implementation. This list is built lazily.
1182 ObjCIvarDecl *IvarList = nullptr;
1183
1184 /// Indicates that the contents of this Objective-C class will be
1185 /// completed by the external AST source when required.
1186 mutable unsigned ExternallyCompleted : 1;
1187
1188 /// Indicates that the ivar cache does not yet include ivars
1189 /// declared in the implementation.
1190 mutable unsigned IvarListMissingImplementation : 1;
1191
1192 /// Indicates that this interface decl contains at least one initializer
1193 /// marked with the 'objc_designated_initializer' attribute.
1194 unsigned HasDesignatedInitializers : 1;
1195
1196 enum InheritedDesignatedInitializersState {
1197 /// We didn't calculate whether the designated initializers should be
1198 /// inherited or not.
1199 IDI_Unknown = 0,
1200
1201 /// Designated initializers are inherited for the super class.
1202 IDI_Inherited = 1,
1203
1204 /// The class does not inherit designated initializers.
1205 IDI_NotInherited = 2
1206 };
1207
1208 /// One of the \c InheritedDesignatedInitializersState enumeratos.
1209 mutable unsigned InheritedDesignatedInitializers : 2;
1210
1211 /// The location of the last location in this declaration, before
1212 /// the properties/methods. For example, this will be the '>', '}', or
1213 /// identifier,
1214 SourceLocation EndLoc;
1215
1216 DefinitionData()
1217 : ExternallyCompleted(false), IvarListMissingImplementation(true),
1218 HasDesignatedInitializers(false),
1219 InheritedDesignatedInitializers(IDI_Unknown) {}
1220 };
1221
1222 /// The type parameters associated with this class, if any.
1223 ObjCTypeParamList *TypeParamList = nullptr;
1224
1225 /// Contains a pointer to the data associated with this class,
1226 /// which will be NULL if this class has not yet been defined.
1227 ///
1228 /// The bit indicates when we don't need to check for out-of-date
1229 /// declarations. It will be set unless modules are enabled.
1230 llvm::PointerIntPair<DefinitionData *, 1, bool> Data;
1231
1232 ObjCInterfaceDecl(const ASTContext &C, DeclContext *DC, SourceLocation AtLoc,
1233 IdentifierInfo *Id, ObjCTypeParamList *typeParamList,
1234 SourceLocation CLoc, ObjCInterfaceDecl *PrevDecl,
1235 bool IsInternal);
1236
1237 void anchor() override;
1238
1239 void LoadExternalDefinition() const;
1240
1241 DefinitionData &data() const {
1242 assert(Data.getPointer() && "Declaration has no definition!")(static_cast <bool> (Data.getPointer() && "Declaration has no definition!"
) ? void (0) : __assert_fail ("Data.getPointer() && \"Declaration has no definition!\""
, "/build/llvm-toolchain-snapshot-14~++20211016100712+8e1d532707fd/clang/include/clang/AST/DeclObjC.h"
, 1242, __extension__ __PRETTY_FUNCTION__))
;
1243 return *Data.getPointer();
1244 }
1245
1246 /// Allocate the definition data for this class.
1247 void allocateDefinitionData();
1248
1249 using redeclarable_base = Redeclarable<ObjCInterfaceDecl>;
1250
1251 ObjCInterfaceDecl *getNextRedeclarationImpl() override {
1252 return getNextRedeclaration();
1253 }
1254
1255 ObjCInterfaceDecl *getPreviousDeclImpl() override {
1256 return getPreviousDecl();
1257 }
1258
1259 ObjCInterfaceDecl *getMostRecentDeclImpl() override {
1260 return getMostRecentDecl();
1261 }
1262
1263public:
1264 static ObjCInterfaceDecl *Create(const ASTContext &C, DeclContext *DC,
1265 SourceLocation atLoc,
1266 IdentifierInfo *Id,
1267 ObjCTypeParamList *typeParamList,
1268 ObjCInterfaceDecl *PrevDecl,
1269 SourceLocation ClassLoc = SourceLocation(),
1270 bool isInternal = false);
1271
1272 static ObjCInterfaceDecl *CreateDeserialized(const ASTContext &C, unsigned ID);
1273
1274 /// Retrieve the type parameters of this class.
1275 ///
1276 /// This function looks for a type parameter list for the given
1277 /// class; if the class has been declared (with \c \@class) but not
1278 /// defined (with \c \@interface), it will search for a declaration that
1279 /// has type parameters, skipping any declarations that do not.
1280 ObjCTypeParamList *getTypeParamList() const;
1281
1282 /// Set the type parameters of this class.
1283 ///
1284 /// This function is used by the AST importer, which must import the type
1285 /// parameters after creating their DeclContext to avoid loops.
1286 void setTypeParamList(ObjCTypeParamList *TPL);
1287
1288 /// Retrieve the type parameters written on this particular declaration of
1289 /// the class.
1290 ObjCTypeParamList *getTypeParamListAsWritten() const {
1291 return TypeParamList;
1292 }
1293
1294 SourceRange getSourceRange() const override LLVM_READONLY__attribute__((__pure__)) {
1295 if (isThisDeclarationADefinition())
1296 return ObjCContainerDecl::getSourceRange();
1297
1298 return SourceRange(getAtStartLoc(), getLocation());
1299 }
1300
1301 /// Indicate that this Objective-C class is complete, but that
1302 /// the external AST source will be responsible for filling in its contents
1303 /// when a complete class is required.
1304 void setExternallyCompleted();
1305
1306 /// Indicate that this interface decl contains at least one initializer
1307 /// marked with the 'objc_designated_initializer' attribute.
1308 void setHasDesignatedInitializers();
1309
1310 /// Returns true if this interface decl contains at least one initializer
1311 /// marked with the 'objc_designated_initializer' attribute.
1312 bool hasDesignatedInitializers() const;
1313
1314 /// Returns true if this interface decl declares a designated initializer
1315 /// or it inherites one from its super class.
1316 bool declaresOrInheritsDesignatedInitializers() const {
1317 return hasDesignatedInitializers() || inheritsDesignatedInitializers();
1318 }
1319
1320 const ObjCProtocolList &getReferencedProtocols() const {
1321 assert(hasDefinition() && "Caller did not check for forward reference!")(static_cast <bool> (hasDefinition() && "Caller did not check for forward reference!"
) ? void (0) : __assert_fail ("hasDefinition() && \"Caller did not check for forward reference!\""
, "/build/llvm-toolchain-snapshot-14~++20211016100712+8e1d532707fd/clang/include/clang/AST/DeclObjC.h"
, 1321, __extension__ __PRETTY_FUNCTION__))
;
1322 if (data().ExternallyCompleted)
1323 LoadExternalDefinition();
1324
1325 return data().ReferencedProtocols;
1326 }
1327
1328 ObjCImplementationDecl *getImplementation() const;
1329 void setImplementation(ObjCImplementationDecl *ImplD);
1330
1331 ObjCCategoryDecl *FindCategoryDeclaration(IdentifierInfo *CategoryId) const;
1332
1333 // Get the local instance/class method declared in a category.
1334 ObjCMethodDecl *getCategoryInstanceMethod(Selector Sel) const;
1335 ObjCMethodDecl *getCategoryClassMethod(Selector Sel) const;
1336
1337 ObjCMethodDecl *getCategoryMethod(Selector Sel, bool isInstance) const {
1338 return isInstance ? getCategoryInstanceMethod(Sel)
1339 : getCategoryClassMethod(Sel);
1340 }
1341
1342 using protocol_iterator = ObjCProtocolList::iterator;
1343 using protocol_range = llvm::iterator_range<protocol_iterator>;
1344
1345 protocol_range protocols() const {
1346 return protocol_range(protocol_begin(), protocol_end());
1347 }
1348
1349 protocol_iterator protocol_begin() const {
1350 // FIXME: Should make sure no callers ever do this.
1351 if (!hasDefinition())
1352 return protocol_iterator();
1353
1354 if (data().ExternallyCompleted)
1355 LoadExternalDefinition();
1356
1357 return data().ReferencedProtocols.begin();
1358 }
1359
1360 protocol_iterator protocol_end() const {
1361 // FIXME: Should make sure no callers ever do this.
1362 if (!hasDefinition())
1363 return protocol_iterator();
1364
1365 if (data().ExternallyCompleted)
1366 LoadExternalDefinition();
1367
1368 return data().ReferencedProtocols.end();
1369 }
1370
1371 using protocol_loc_iterator = ObjCProtocolList::loc_iterator;
1372 using protocol_loc_range = llvm::iterator_range<protocol_loc_iterator>;
1373
1374 protocol_loc_range protocol_locs() const {
1375 return protocol_loc_range(protocol_loc_begin(), protocol_loc_end());
1376 }
1377
1378 protocol_loc_iterator protocol_loc_begin() const {
1379 // FIXME: Should make sure no callers ever do this.
1380 if (!hasDefinition())
1381 return protocol_loc_iterator();
1382
1383 if (data().ExternallyCompleted)
1384 LoadExternalDefinition();
1385
1386 return data().ReferencedProtocols.loc_begin();
1387 }
1388
1389 protocol_loc_iterator protocol_loc_end() const {
1390 // FIXME: Should make sure no callers ever do this.
1391 if (!hasDefinition())
1392 return protocol_loc_iterator();
1393
1394 if (data().ExternallyCompleted)
1395 LoadExternalDefinition();
1396
1397 return data().ReferencedProtocols.loc_end();
1398 }
1399
1400 using all_protocol_iterator = ObjCList<ObjCProtocolDecl>::iterator;
1401 using all_protocol_range = llvm::iterator_range<all_protocol_iterator>;
1402
1403 all_protocol_range all_referenced_protocols() const {
1404 return all_protocol_range(all_referenced_protocol_begin(),
1405 all_referenced_protocol_end());
1406 }
1407
1408 all_protocol_iterator all_referenced_protocol_begin() const {
1409 // FIXME: Should make sure no callers ever do this.
1410 if (!hasDefinition())
1411 return all_protocol_iterator();
1412
1413 if (data().ExternallyCompleted)
1414 LoadExternalDefinition();
1415
1416 return data().AllReferencedProtocols.empty()
1417 ? protocol_begin()
1418 : data().AllReferencedProtocols.begin();
1419 }
1420
1421 all_protocol_iterator all_referenced_protocol_end() const {
1422 // FIXME: Should make sure no callers ever do this.
1423 if (!hasDefinition())
1424 return all_protocol_iterator();
1425
1426 if (data().ExternallyCompleted)
1427 LoadExternalDefinition();
1428
1429 return data().AllReferencedProtocols.empty()
1430 ? protocol_end()
1431 : data().AllReferencedProtocols.end();
1432 }
1433
1434 using ivar_iterator = specific_decl_iterator<ObjCIvarDecl>;
1435 using ivar_range = llvm::iterator_range<specific_decl_iterator<ObjCIvarDecl>>;
1436
1437 ivar_range ivars() const { return ivar_range(ivar_begin(), ivar_end()); }
1438
1439 ivar_iterator ivar_begin() const {
1440 if (const ObjCInterfaceDecl *Def = getDefinition())
1441 return ivar_iterator(Def->decls_begin());
1442
1443 // FIXME: Should make sure no callers ever do this.
1444 return ivar_iterator();
1445 }
1446
1447 ivar_iterator ivar_end() const {
1448 if (const ObjCInterfaceDecl *Def = getDefinition())
1449 return ivar_iterator(Def->decls_end());
1450
1451 // FIXME: Should make sure no callers ever do this.
1452 return ivar_iterator();
1453 }
1454
1455 unsigned ivar_size() const {
1456 return std::distance(ivar_begin(), ivar_end());
1457 }
1458
1459 bool ivar_empty() const { return ivar_begin() == ivar_end(); }
1460
1461 ObjCIvarDecl *all_declared_ivar_begin();
1462 const ObjCIvarDecl *all_declared_ivar_begin() const {
1463 // Even though this modifies IvarList, it's conceptually const:
1464 // the ivar chain is essentially a cached property of ObjCInterfaceDecl.
1465 return const_cast<ObjCInterfaceDecl *>(this)->all_declared_ivar_begin();
1466 }
1467 void setIvarList(ObjCIvarDecl *ivar) { data().IvarList = ivar; }
1468
1469 /// setProtocolList - Set the list of protocols that this interface
1470 /// implements.
1471 void setProtocolList(ObjCProtocolDecl *const* List, unsigned Num,
1472 const SourceLocation *Locs, ASTContext &C) {
1473 data().ReferencedProtocols.set(List, Num, Locs, C);
1474 }
1475
1476 /// mergeClassExtensionProtocolList - Merge class extension's protocol list
1477 /// into the protocol list for this class.
1478 void mergeClassExtensionProtocolList(ObjCProtocolDecl *const* List,
1479 unsigned Num,
1480 ASTContext &C);
1481
1482 /// Produce a name to be used for class's metadata. It comes either via
1483 /// objc_runtime_name attribute or class name.
1484 StringRef getObjCRuntimeNameAsString() const;
1485
1486 /// Returns the designated initializers for the interface.
1487 ///
1488 /// If this declaration does not have methods marked as designated
1489 /// initializers then the interface inherits the designated initializers of
1490 /// its super class.
1491 void getDesignatedInitializers(
1492 llvm::SmallVectorImpl<const ObjCMethodDecl *> &Methods) const;
1493
1494 /// Returns true if the given selector is a designated initializer for the
1495 /// interface.
1496 ///
1497 /// If this declaration does not have methods marked as designated
1498 /// initializers then the interface inherits the designated initializers of
1499 /// its super class.
1500 ///
1501 /// \param InitMethod if non-null and the function returns true, it receives
1502 /// the method that was marked as a designated initializer.
1503 bool
1504 isDesignatedInitializer(Selector Sel,
1505 const ObjCMethodDecl **InitMethod = nullptr) const;
1506
1507 /// Determine whether this particular declaration of this class is
1508 /// actually also a definition.
1509 bool isThisDeclarationADefinition() const {
1510 return getDefinition() == this;
1511 }
1512
1513 /// Determine whether this class has been defined.
1514 bool hasDefinition() const {
1515 // If the name of this class is out-of-date, bring it up-to-date, which
1516 // might bring in a definition.
1517 // Note: a null value indicates that we don't have a definition and that
1518 // modules are enabled.
1519 if (!Data.getOpaqueValue())
2
Assuming the condition is false
3
Taking false branch
1520 getMostRecentDecl();
1521
1522 return Data.getPointer();
4
Returning value, which participates in a condition later
1523 }
1524
1525 /// Retrieve the definition of this class, or NULL if this class
1526 /// has been forward-declared (with \@class) but not yet defined (with
1527 /// \@interface).
1528 ObjCInterfaceDecl *getDefinition() {
1529 return hasDefinition()? Data.getPointer()->Definition : nullptr;
1530 }
1531
1532 /// Retrieve the definition of this class, or NULL if this class
1533 /// has been forward-declared (with \@class) but not yet defined (with
1534 /// \@interface).
1535 const ObjCInterfaceDecl *getDefinition() const {
1536 return hasDefinition()? Data.getPointer()->Definition : nullptr;
1537 }
1538
1539 /// Starts the definition of this Objective-C class, taking it from
1540 /// a forward declaration (\@class) to a definition (\@interface).
1541 void startDefinition();
1542
1543 /// Retrieve the superclass type.
1544 const ObjCObjectType *getSuperClassType() const {
1545 if (TypeSourceInfo *TInfo = getSuperClassTInfo())
1546 return TInfo->getType()->castAs<ObjCObjectType>();
1547
1548 return nullptr;
1549 }
1550
1551 // Retrieve the type source information for the superclass.
1552 TypeSourceInfo *getSuperClassTInfo() const {
1553 // FIXME: Should make sure no callers ever do this.
1554 if (!hasDefinition())
1555 return nullptr;
1556
1557 if (data().ExternallyCompleted)
1558 LoadExternalDefinition();
1559
1560 return data().SuperClassTInfo;
1561 }
1562
1563 // Retrieve the declaration for the superclass of this class, which
1564 // does not include any type arguments that apply to the superclass.
1565 ObjCInterfaceDecl *getSuperClass() const;
1566
1567 void setSuperClass(TypeSourceInfo *superClass) {
1568 data().SuperClassTInfo = superClass;
1569 }
1570
1571 /// Iterator that walks over the list of categories, filtering out
1572 /// those that do not meet specific criteria.
1573 ///
1574 /// This class template is used for the various permutations of category
1575 /// and extension iterators.
1576 template<bool (*Filter)(ObjCCategoryDecl *)>
1577 class filtered_category_iterator {
1578 ObjCCategoryDecl *Current = nullptr;
1579
1580 void findAcceptableCategory();
1581
1582 public:
1583 using value_type = ObjCCategoryDecl *;
1584 using reference = value_type;
1585 using pointer = value_type;
1586 using difference_type = std::ptrdiff_t;
1587 using iterator_category = std::input_iterator_tag;
1588
1589 filtered_category_iterator() = default;
1590 explicit filtered_category_iterator(ObjCCategoryDecl *Current)
1591 : Current(Current) {
1592 findAcceptableCategory();
1593 }
1594
1595 reference operator*() const { return Current; }
1596 pointer operator->() const { return Current; }
1597
1598 filtered_category_iterator &operator++();
1599
1600 filtered_category_iterator operator++(int) {
1601 filtered_category_iterator Tmp = *this;
1602 ++(*this);
1603 return Tmp;
1604 }
1605
1606 friend bool operator==(filtered_category_iterator X,
1607 filtered_category_iterator Y) {
1608 return X.Current == Y.Current;
1609 }
1610
1611 friend bool operator!=(filtered_category_iterator X,
1612 filtered_category_iterator Y) {
1613 return X.Current != Y.Current;
1614 }
1615 };
1616
1617private:
1618 /// Test whether the given category is visible.
1619 ///
1620 /// Used in the \c visible_categories_iterator.
1621 static bool isVisibleCategory(ObjCCategoryDecl *Cat);
1622
1623public:
1624 /// Iterator that walks over the list of categories and extensions
1625 /// that are visible, i.e., not hidden in a non-imported submodule.
1626 using visible_categories_iterator =
1627 filtered_category_iterator<isVisibleCategory>;
1628
1629 using visible_categories_range =
1630 llvm::iterator_range<visible_categories_iterator>;
1631
1632 visible_categories_range visible_categories() const {
1633 return visible_categories_range(visible_categories_begin(),
1634 visible_categories_end());
1635 }
1636
1637 /// Retrieve an iterator to the beginning of the visible-categories
1638 /// list.
1639 visible_categories_iterator visible_categories_begin() const {
1640 return visible_categories_iterator(getCategoryListRaw());
1641 }
1642
1643 /// Retrieve an iterator to the end of the visible-categories list.
1644 visible_categories_iterator visible_categories_end() const {
1645 return visible_categories_iterator();
1646 }
1647
1648 /// Determine whether the visible-categories list is empty.
1649 bool visible_categories_empty() const {
1650 return visible_categories_begin() == visible_categories_end();
1651 }
1652
1653private:
1654 /// Test whether the given category... is a category.
1655 ///
1656 /// Used in the \c known_categories_iterator.
1657 static bool isKnownCategory(ObjCCategoryDecl *) { return true; }
1658
1659public:
1660 /// Iterator that walks over all of the known categories and
1661 /// extensions, including those that are hidden.
1662 using known_categories_iterator = filtered_category_iterator<isKnownCategory>;
1663 using known_categories_range =
1664 llvm::iterator_range<known_categories_iterator>;
1665
1666 known_categories_range known_categories() const {
1667 return known_categories_range(known_categories_begin(),
1668 known_categories_end());
1669 }
1670
1671 /// Retrieve an iterator to the beginning of the known-categories
1672 /// list.
1673 known_categories_iterator known_categories_begin() const {
1674 return known_categories_iterator(getCategoryListRaw());
1675 }
1676
1677 /// Retrieve an iterator to the end of the known-categories list.
1678 known_categories_iterator known_categories_end() const {
1679 return known_categories_iterator();
1680 }
1681
1682 /// Determine whether the known-categories list is empty.
1683 bool known_categories_empty() const {
1684 return known_categories_begin() == known_categories_end();
1685 }
1686
1687private:
1688 /// Test whether the given category is a visible extension.
1689 ///
1690 /// Used in the \c visible_extensions_iterator.
1691 static bool isVisibleExtension(ObjCCategoryDecl *Cat);
1692
1693public:
1694 /// Iterator that walks over all of the visible extensions, skipping
1695 /// any that are known but hidden.
1696 using visible_extensions_iterator =
1697 filtered_category_iterator<isVisibleExtension>;
1698
1699 using visible_extensions_range =
1700 llvm::iterator_range<visible_extensions_iterator>;
1701
1702 visible_extensions_range visible_extensions() const {
1703 return visible_extensions_range(visible_extensions_begin(),
1704 visible_extensions_end());
1705 }
1706
1707 /// Retrieve an iterator to the beginning of the visible-extensions
1708 /// list.
1709 visible_extensions_iterator visible_extensions_begin() const {
1710 return visible_extensions_iterator(getCategoryListRaw());
1711 }
1712
1713 /// Retrieve an iterator to the end of the visible-extensions list.
1714 visible_extensions_iterator visible_extensions_end() const {
1715 return visible_extensions_iterator();
1716 }
1717
1718 /// Determine whether the visible-extensions list is empty.
1719 bool visible_extensions_empty() const {
1720 return visible_extensions_begin() == visible_extensions_end();
1721 }
1722
1723private:
1724 /// Test whether the given category is an extension.
1725 ///
1726 /// Used in the \c known_extensions_iterator.
1727 static bool isKnownExtension(ObjCCategoryDecl *Cat);
1728
1729public:
1730 friend class ASTDeclReader;
1731 friend class ASTDeclWriter;
1732 friend class ASTReader;
1733
1734 /// Iterator that walks over all of the known extensions.
1735 using known_extensions_iterator =
1736 filtered_category_iterator<isKnownExtension>;
1737 using known_extensions_range =
1738 llvm::iterator_range<known_extensions_iterator>;
1739
1740 known_extensions_range known_extensions() const {
1741 return known_extensions_range(known_extensions_begin(),
1742 known_extensions_end());
1743 }
1744
1745 /// Retrieve an iterator to the beginning of the known-extensions
1746 /// list.
1747 known_extensions_iterator known_extensions_begin() const {
1748 return known_extensions_iterator(getCategoryListRaw());
1749 }
1750
1751 /// Retrieve an iterator to the end of the known-extensions list.
1752 known_extensions_iterator known_extensions_end() const {
1753 return known_extensions_iterator();
1754 }
1755
1756 /// Determine whether the known-extensions list is empty.
1757 bool known_extensions_empty() const {
1758 return known_extensions_begin() == known_extensions_end();
1759 }
1760
1761 /// Retrieve the raw pointer to the start of the category/extension
1762 /// list.
1763 ObjCCategoryDecl* getCategoryListRaw() const {
1764 // FIXME: Should make sure no callers ever do this.
1765 if (!hasDefinition())
1766 return nullptr;
1767
1768 if (data().ExternallyCompleted)
1769 LoadExternalDefinition();
1770
1771 return data().CategoryList;
1772 }
1773
1774 /// Set the raw pointer to the start of the category/extension
1775 /// list.
1776 void setCategoryListRaw(ObjCCategoryDecl *category) {
1777 data().CategoryList = category;
1778 }
1779
1780 ObjCPropertyDecl
1781 *FindPropertyVisibleInPrimaryClass(IdentifierInfo *PropertyId,
1782 ObjCPropertyQueryKind QueryKind) const;
1783
1784 void collectPropertiesToImplement(PropertyMap &PM,
1785 PropertyDeclOrder &PO) const override;
1786
1787 /// isSuperClassOf - Return true if this class is the specified class or is a
1788 /// super class of the specified interface class.
1789 bool isSuperClassOf(const ObjCInterfaceDecl *I) const {
1790 // If RHS is derived from LHS it is OK; else it is not OK.
1791 while (I != nullptr) {
1792 if (declaresSameEntity(this, I))
1793 return true;
1794
1795 I = I->getSuperClass();
1796 }
1797 return false;
1798 }
1799
1800 /// isArcWeakrefUnavailable - Checks for a class or one of its super classes
1801 /// to be incompatible with __weak references. Returns true if it is.
1802 bool isArcWeakrefUnavailable() const;
1803
1804 /// isObjCRequiresPropertyDefs - Checks that a class or one of its super
1805 /// classes must not be auto-synthesized. Returns class decl. if it must not
1806 /// be; 0, otherwise.
1807 const ObjCInterfaceDecl *isObjCRequiresPropertyDefs() const;
1808
1809 ObjCIvarDecl *lookupInstanceVariable(IdentifierInfo *IVarName,
1810 ObjCInterfaceDecl *&ClassDeclared);
1811 ObjCIvarDecl *lookupInstanceVariable(IdentifierInfo *IVarName) {
1812 ObjCInterfaceDecl *ClassDeclared;
1813 return lookupInstanceVariable(IVarName, ClassDeclared);
1814 }
1815
1816 ObjCProtocolDecl *lookupNestedProtocol(IdentifierInfo *Name);
1817
1818 // Lookup a method. First, we search locally. If a method isn't
1819 // found, we search referenced protocols and class categories.
1820 ObjCMethodDecl *lookupMethod(Selector Sel, bool isInstance,
1821 bool shallowCategoryLookup = false,
1822 bool followSuper = true,
1823 const ObjCCategoryDecl *C = nullptr) const;
1824
1825 /// Lookup an instance method for a given selector.
1826 ObjCMethodDecl *lookupInstanceMethod(Selector Sel) const {
1827 return lookupMethod(Sel, true/*isInstance*/);
1828 }
1829
1830 /// Lookup a class method for a given selector.
1831 ObjCMethodDecl *lookupClassMethod(Selector Sel) const {
1832 return lookupMethod(Sel, false/*isInstance*/);
1833 }
1834
1835 ObjCInterfaceDecl *lookupInheritedClass(const IdentifierInfo *ICName);
1836
1837 /// Lookup a method in the classes implementation hierarchy.
1838 ObjCMethodDecl *lookupPrivateMethod(const Selector &Sel,
1839 bool Instance=true) const;
1840
1841 ObjCMethodDecl *lookupPrivateClassMethod(const Selector &Sel) {
1842 return lookupPrivateMethod(Sel, false);
1843 }
1844
1845 /// Lookup a setter or getter in the class hierarchy,
1846 /// including in all categories except for category passed
1847 /// as argument.
1848 ObjCMethodDecl *lookupPropertyAccessor(const Selector Sel,
1849 const ObjCCategoryDecl *Cat,
1850 bool IsClassProperty) const {
1851 return lookupMethod(Sel, !IsClassProperty/*isInstance*/,
1852 false/*shallowCategoryLookup*/,
1853 true /* followsSuper */,
1854 Cat);
1855 }
1856
1857 SourceLocation getEndOfDefinitionLoc() const {
1858 if (!hasDefinition())
1859 return getLocation();
1860
1861 return data().EndLoc;
1862 }
1863
1864 void setEndOfDefinitionLoc(SourceLocation LE) { data().EndLoc = LE; }
1865
1866 /// Retrieve the starting location of the superclass.
1867 SourceLocation getSuperClassLoc() const;
1868
1869 /// isImplicitInterfaceDecl - check that this is an implicitly declared
1870 /// ObjCInterfaceDecl node. This is for legacy objective-c \@implementation
1871 /// declaration without an \@interface declaration.
1872 bool isImplicitInterfaceDecl() const {
1873 return hasDefinition() ? data().Definition->isImplicit() : isImplicit();
1874 }
1875
1876 /// ClassImplementsProtocol - Checks that 'lProto' protocol
1877 /// has been implemented in IDecl class, its super class or categories (if
1878 /// lookupCategory is true).
1879 bool ClassImplementsProtocol(ObjCProtocolDecl *lProto,
1880 bool lookupCategory,
1881 bool RHSIsQualifiedID = false);
1882
1883 using redecl_range = redeclarable_base::redecl_range;
1884 using redecl_iterator = redeclarable_base::redecl_iterator;
1885
1886 using redeclarable_base::redecls_begin;
1887 using redeclarable_base::redecls_end;
1888 using redeclarable_base::redecls;
1889 using redeclarable_base::getPreviousDecl;
1890 using redeclarable_base::getMostRecentDecl;
1891 using redeclarable_base::isFirstDecl;
1892
1893 /// Retrieves the canonical declaration of this Objective-C class.
1894 ObjCInterfaceDecl *getCanonicalDecl() override { return getFirstDecl(); }
1895 const ObjCInterfaceDecl *getCanonicalDecl() const { return getFirstDecl(); }
1896
1897 // Low-level accessor
1898 const Type *getTypeForDecl() const { return TypeForDecl; }
1899 void setTypeForDecl(const Type *TD) const { TypeForDecl = TD; }
1900
1901 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1902 static bool classofKind(Kind K) { return K == ObjCInterface; }
1903
1904private:
1905 const ObjCInterfaceDecl *findInterfaceWithDesignatedInitializers() const;
1906 bool inheritsDesignatedInitializers() const;
1907};
1908
1909/// ObjCIvarDecl - Represents an ObjC instance variable. In general, ObjC
1910/// instance variables are identical to C. The only exception is Objective-C
1911/// supports C++ style access control. For example:
1912///
1913/// \@interface IvarExample : NSObject
1914/// {
1915/// id defaultToProtected;
1916/// \@public:
1917/// id canBePublic; // same as C++.
1918/// \@protected:
1919/// id canBeProtected; // same as C++.
1920/// \@package:
1921/// id canBePackage; // framework visibility (not available in C++).
1922/// }
1923///
1924class ObjCIvarDecl : public FieldDecl {
1925 void anchor() override;
1926
1927public:
1928 enum AccessControl {
1929 None, Private, Protected, Public, Package
1930 };
1931
1932private:
1933 ObjCIvarDecl(ObjCContainerDecl *DC, SourceLocation StartLoc,
1934 SourceLocation IdLoc, IdentifierInfo *Id,
1935 QualType T, TypeSourceInfo *TInfo, AccessControl ac, Expr *BW,
1936 bool synthesized)
1937 : FieldDecl(ObjCIvar, DC, StartLoc, IdLoc, Id, T, TInfo, BW,
1938 /*Mutable=*/false, /*HasInit=*/ICIS_NoInit),
1939 DeclAccess(ac), Synthesized(synthesized) {}
1940
1941public:
1942 static ObjCIvarDecl *Create(ASTContext &C, ObjCContainerDecl *DC,
1943 SourceLocation StartLoc, SourceLocation IdLoc,
1944 IdentifierInfo *Id, QualType T,
1945 TypeSourceInfo *TInfo,
1946 AccessControl ac, Expr *BW = nullptr,
1947 bool synthesized=false);
1948
1949 static ObjCIvarDecl *CreateDeserialized(ASTContext &C, unsigned ID);
1950
1951 /// Return the class interface that this ivar is logically contained
1952 /// in; this is either the interface where the ivar was declared, or the
1953 /// interface the ivar is conceptually a part of in the case of synthesized
1954 /// ivars.
1955 const ObjCInterfaceDecl *getContainingInterface() const;
1956
1957 ObjCIvarDecl *getNextIvar() { return NextIvar; }
1958 const ObjCIvarDecl *getNextIvar() const { return NextIvar; }
1959 void setNextIvar(ObjCIvarDecl *ivar) { NextIvar = ivar; }
1960
1961 ObjCIvarDecl *getCanonicalDecl() override {
1962 return cast<ObjCIvarDecl>(FieldDecl::getCanonicalDecl());
1963 }
1964 const ObjCIvarDecl *getCanonicalDecl() const {
1965 return const_cast<ObjCIvarDecl *>(this)->getCanonicalDecl();
1966 }
1967
1968 void setAccessControl(AccessControl ac) { DeclAccess = ac; }
1969
1970 AccessControl getAccessControl() const { return AccessControl(DeclAccess); }
1971
1972 AccessControl getCanonicalAccessControl() const {
1973 return DeclAccess == None ? Protected : AccessControl(DeclAccess);
1974 }
1975
1976 void setSynthesize(bool synth) { Synthesized = synth; }
1977 bool getSynthesize() const { return Synthesized; }
1978
1979 /// Retrieve the type of this instance variable when viewed as a member of a
1980 /// specific object type.
1981 QualType getUsageType(QualType objectType) const;
1982
1983 // Implement isa/cast/dyncast/etc.
1984 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1985 static bool classofKind(Kind K) { return K == ObjCIvar; }
1986
1987private:
1988 /// NextIvar - Next Ivar in the list of ivars declared in class; class's
1989 /// extensions and class's implementation
1990 ObjCIvarDecl *NextIvar = nullptr;
1991
1992 // NOTE: VC++ treats enums as signed, avoid using the AccessControl enum
1993 unsigned DeclAccess : 3;
1994 unsigned Synthesized : 1;
1995};
1996
1997/// Represents a field declaration created by an \@defs(...).
1998class ObjCAtDefsFieldDecl : public FieldDecl {
1999 ObjCAtDefsFieldDecl(DeclContext *DC, SourceLocation StartLoc,
2000 SourceLocation IdLoc, IdentifierInfo *Id,
2001 QualType T, Expr *BW)
2002 : FieldDecl(ObjCAtDefsField, DC, StartLoc, IdLoc, Id, T,
2003 /*TInfo=*/nullptr, // FIXME: Do ObjCAtDefs have declarators ?
2004 BW, /*Mutable=*/false, /*HasInit=*/ICIS_NoInit) {}
2005
2006 void anchor() override;
2007
2008public:
2009 static ObjCAtDefsFieldDecl *Create(ASTContext &C, DeclContext *DC,
2010 SourceLocation StartLoc,
2011 SourceLocation IdLoc, IdentifierInfo *Id,
2012 QualType T, Expr *BW);
2013
2014 static ObjCAtDefsFieldDecl *CreateDeserialized(ASTContext &C, unsigned ID);
2015
2016 // Implement isa/cast/dyncast/etc.
2017 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
2018 static bool classofKind(Kind K) { return K == ObjCAtDefsField; }
2019};
2020
2021/// Represents an Objective-C protocol declaration.
2022///
2023/// Objective-C protocols declare a pure abstract type (i.e., no instance
2024/// variables are permitted). Protocols originally drew inspiration from
2025/// C++ pure virtual functions (a C++ feature with nice semantics and lousy
2026/// syntax:-). Here is an example:
2027///
2028/// \code
2029/// \@protocol NSDraggingInfo <refproto1, refproto2>
2030/// - (NSWindow *)draggingDestinationWindow;
2031/// - (NSImage *)draggedImage;
2032/// \@end
2033/// \endcode
2034///
2035/// This says that NSDraggingInfo requires two methods and requires everything
2036/// that the two "referenced protocols" 'refproto1' and 'refproto2' require as
2037/// well.
2038///
2039/// \code
2040/// \@interface ImplementsNSDraggingInfo : NSObject \<NSDraggingInfo>
2041/// \@end
2042/// \endcode
2043///
2044/// ObjC protocols inspired Java interfaces. Unlike Java, ObjC classes and
2045/// protocols are in distinct namespaces. For example, Cocoa defines both
2046/// an NSObject protocol and class (which isn't allowed in Java). As a result,
2047/// protocols are referenced using angle brackets as follows:
2048///
2049/// id \<NSDraggingInfo> anyObjectThatImplementsNSDraggingInfo;
2050class ObjCProtocolDecl : public ObjCContainerDecl,
2051 public Redeclarable<ObjCProtocolDecl> {
2052 struct DefinitionData {
2053 // The declaration that defines this protocol.
2054 ObjCProtocolDecl *Definition;
2055
2056 /// Referenced protocols
2057 ObjCProtocolList ReferencedProtocols;
2058 };
2059
2060 /// Contains a pointer to the data associated with this class,
2061 /// which will be NULL if this class has not yet been defined.
2062 ///
2063 /// The bit indicates when we don't need to check for out-of-date
2064 /// declarations. It will be set unless modules are enabled.
2065 llvm::PointerIntPair<DefinitionData *, 1, bool> Data;
2066
2067 ObjCProtocolDecl(ASTContext &C, DeclContext *DC, IdentifierInfo *Id,
2068 SourceLocation nameLoc, SourceLocation atStartLoc,
2069 ObjCProtocolDecl *PrevDecl);
2070
2071 void anchor() override;
2072
2073 DefinitionData &data() const {
2074 assert(Data.getPointer() && "Objective-C protocol has no definition!")(static_cast <bool> (Data.getPointer() && "Objective-C protocol has no definition!"
) ? void (0) : __assert_fail ("Data.getPointer() && \"Objective-C protocol has no definition!\""
, "/build/llvm-toolchain-snapshot-14~++20211016100712+8e1d532707fd/clang/include/clang/AST/DeclObjC.h"
, 2074, __extension__ __PRETTY_FUNCTION__))
;
2075 return *Data.getPointer();
2076 }
2077
2078 void allocateDefinitionData();
2079
2080 using redeclarable_base = Redeclarable<ObjCProtocolDecl>;
2081
2082 ObjCProtocolDecl *getNextRedeclarationImpl() override {
2083 return getNextRedeclaration();
2084 }
2085
2086 ObjCProtocolDecl *getPreviousDeclImpl() override {
2087 return getPreviousDecl();
2088 }
2089
2090 ObjCProtocolDecl *getMostRecentDeclImpl() override {
2091 return getMostRecentDecl();
2092 }
2093
2094public:
2095 friend class ASTDeclReader;
2096 friend class ASTDeclWriter;
2097 friend class ASTReader;
2098
2099 static ObjCProtocolDecl *Create(ASTContext &C, DeclContext *DC,
2100 IdentifierInfo *Id,
2101 SourceLocation nameLoc,
2102 SourceLocation atStartLoc,
2103 ObjCProtocolDecl *PrevDecl);
2104
2105 static ObjCProtocolDecl *CreateDeserialized(ASTContext &C, unsigned ID);
2106
2107 const ObjCProtocolList &getReferencedProtocols() const {
2108 assert(hasDefinition() && "No definition available!")(static_cast <bool> (hasDefinition() && "No definition available!"
) ? void (0) : __assert_fail ("hasDefinition() && \"No definition available!\""
, "/build/llvm-toolchain-snapshot-14~++20211016100712+8e1d532707fd/clang/include/clang/AST/DeclObjC.h"
, 2108, __extension__ __PRETTY_FUNCTION__))
;
2109 return data().ReferencedProtocols;
2110 }
2111
2112 using protocol_iterator = ObjCProtocolList::iterator;
2113 using protocol_range = llvm::iterator_range<protocol_iterator>;
2114
2115 protocol_range protocols() const {
2116 return protocol_range(protocol_begin(), protocol_end());
2117 }
2118
2119 protocol_iterator protocol_begin() const {
2120 if (!hasDefinition())
2121 return protocol_iterator();
2122
2123 return data().ReferencedProtocols.begin();
2124 }
2125
2126 protocol_iterator protocol_end() const {
2127 if (!hasDefinition())
2128 return protocol_iterator();
2129
2130 return data().ReferencedProtocols.end();
2131 }
2132
2133 using protocol_loc_iterator = ObjCProtocolList::loc_iterator;
2134 using protocol_loc_range = llvm::iterator_range<protocol_loc_iterator>;
2135
2136 protocol_loc_range protocol_locs() const {
2137 return protocol_loc_range(protocol_loc_begin(), protocol_loc_end());
2138 }
2139
2140 protocol_loc_iterator protocol_loc_begin() const {
2141 if (!hasDefinition())
2142 return protocol_loc_iterator();
2143
2144 return data().ReferencedProtocols.loc_begin();
2145 }
2146
2147 protocol_loc_iterator protocol_loc_end() const {
2148 if (!hasDefinition())
2149 return protocol_loc_iterator();
2150
2151 return data().ReferencedProtocols.loc_end();
2152 }
2153
2154 unsigned protocol_size() const {
2155 if (!hasDefinition())
2156 return 0;
2157
2158 return data().ReferencedProtocols.size();
2159 }
2160
2161 /// setProtocolList - Set the list of protocols that this interface
2162 /// implements.
2163 void setProtocolList(ObjCProtocolDecl *const*List, unsigned Num,
2164 const SourceLocation *Locs, ASTContext &C) {
2165 assert(hasDefinition() && "Protocol is not defined")(static_cast <bool> (hasDefinition() && "Protocol is not defined"
) ? void (0) : __assert_fail ("hasDefinition() && \"Protocol is not defined\""
, "/build/llvm-toolchain-snapshot-14~++20211016100712+8e1d532707fd/clang/include/clang/AST/DeclObjC.h"
, 2165, __extension__ __PRETTY_FUNCTION__))
;
2166 data().ReferencedProtocols.set(List, Num, Locs, C);
2167 }
2168
2169 /// This is true iff the protocol is tagged with the
2170 /// `objc_non_runtime_protocol` attribute.
2171 bool isNonRuntimeProtocol() const;
2172
2173 /// Get the set of all protocols implied by this protocols inheritance
2174 /// hierarchy.
2175 void getImpliedProtocols(llvm::DenseSet<const ObjCProtocolDecl *> &IPs) const;
2176
2177 ObjCProtocolDecl *lookupProtocolNamed(IdentifierInfo *PName);
2178
2179 // Lookup a method. First, we search locally. If a method isn't
2180 // found, we search referenced protocols and class categories.
2181 ObjCMethodDecl *lookupMethod(Selector Sel, bool isInstance) const;
2182
2183 ObjCMethodDecl *lookupInstanceMethod(Selector Sel) const {
2184 return lookupMethod(Sel, true/*isInstance*/);
2185 }
2186
2187 ObjCMethodDecl *lookupClassMethod(Selector Sel) const {
2188 return lookupMethod(Sel, false/*isInstance*/);
2189 }
2190
2191 /// Determine whether this protocol has a definition.
2192 bool hasDefinition() const {
2193 // If the name of this protocol is out-of-date, bring it up-to-date, which
2194 // might bring in a definition.
2195 // Note: a null value indicates that we don't have a definition and that
2196 // modules are enabled.
2197 if (!Data.getOpaqueValue())
2198 getMostRecentDecl();
2199
2200 return Data.getPointer();
2201 }
2202
2203 /// Retrieve the definition of this protocol, if any.
2204 ObjCProtocolDecl *getDefinition() {
2205 return hasDefinition()? Data.getPointer()->Definition : nullptr;
2206 }
2207
2208 /// Retrieve the definition of this protocol, if any.
2209 const ObjCProtocolDecl *getDefinition() const {
2210 return hasDefinition()? Data.getPointer()->Definition : nullptr;
2211 }
2212
2213 /// Determine whether this particular declaration is also the
2214 /// definition.
2215 bool isThisDeclarationADefinition() const {
2216 return getDefinition() == this;
2217 }
2218
2219 /// Starts the definition of this Objective-C protocol.
2220 void startDefinition();
2221
2222 /// Produce a name to be used for protocol's metadata. It comes either via
2223 /// objc_runtime_name attribute or protocol name.
2224 StringRef getObjCRuntimeNameAsString() const;
2225
2226 SourceRange getSourceRange() const override LLVM_READONLY__attribute__((__pure__)) {
2227 if (isThisDeclarationADefinition())
2228 return ObjCContainerDecl::getSourceRange();
2229
2230 return SourceRange(getAtStartLoc(), getLocation());
2231 }
2232
2233 using redecl_range = redeclarable_base::redecl_range;
2234 using redecl_iterator = redeclarable_base::redecl_iterator;
2235
2236 using redeclarable_base::redecls_begin;
2237 using redeclarable_base::redecls_end;
2238 using redeclarable_base::redecls;
2239 using redeclarable_base::getPreviousDecl;
2240 using redeclarable_base::getMostRecentDecl;
2241 using redeclarable_base::isFirstDecl;
2242
2243 /// Retrieves the canonical declaration of this Objective-C protocol.
2244 ObjCProtocolDecl *getCanonicalDecl() override { return getFirstDecl(); }
2245 const ObjCProtocolDecl *getCanonicalDecl() const { return getFirstDecl(); }
2246
2247 void collectPropertiesToImplement(PropertyMap &PM,
2248 PropertyDeclOrder &PO) const override;
2249
2250 void collectInheritedProtocolProperties(const ObjCPropertyDecl *Property,
2251 ProtocolPropertySet &PS,
2252 PropertyDeclOrder &PO) const;
2253
2254 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
2255 static bool classofKind(Kind K) { return K == ObjCProtocol; }
2256};
2257
2258/// ObjCCategoryDecl - Represents a category declaration. A category allows
2259/// you to add methods to an existing class (without subclassing or modifying
2260/// the original class interface or implementation:-). Categories don't allow
2261/// you to add instance data. The following example adds "myMethod" to all
2262/// NSView's within a process:
2263///
2264/// \@interface NSView (MyViewMethods)
2265/// - myMethod;
2266/// \@end
2267///
2268/// Categories also allow you to split the implementation of a class across
2269/// several files (a feature more naturally supported in C++).
2270///
2271/// Categories were originally inspired by dynamic languages such as Common
2272/// Lisp and Smalltalk. More traditional class-based languages (C++, Java)
2273/// don't support this level of dynamism, which is both powerful and dangerous.
2274class ObjCCategoryDecl : public ObjCContainerDecl {
2275 /// Interface belonging to this category
2276 ObjCInterfaceDecl *ClassInterface;
2277
2278 /// The type parameters associated with this category, if any.
2279 ObjCTypeParamList *TypeParamList = nullptr;
2280
2281 /// referenced protocols in this category.
2282 ObjCProtocolList ReferencedProtocols;
2283
2284 /// Next category belonging to this class.
2285 /// FIXME: this should not be a singly-linked list. Move storage elsewhere.
2286 ObjCCategoryDecl *NextClassCategory = nullptr;
2287
2288 /// The location of the category name in this declaration.
2289 SourceLocation CategoryNameLoc;
2290
2291 /// class extension may have private ivars.
2292 SourceLocation IvarLBraceLoc;
2293 SourceLocation IvarRBraceLoc;
2294
2295 ObjCCategoryDecl(DeclContext *DC, SourceLocation AtLoc,
2296 SourceLocation ClassNameLoc, SourceLocation CategoryNameLoc,
2297 IdentifierInfo *Id, ObjCInterfaceDecl *IDecl,
2298 ObjCTypeParamList *typeParamList,
2299 SourceLocation IvarLBraceLoc = SourceLocation(),
2300 SourceLocation IvarRBraceLoc = SourceLocation());
2301
2302 void anchor() override;
2303
2304public:
2305 friend class ASTDeclReader;
2306 friend class ASTDeclWriter;
2307
2308 static ObjCCategoryDecl *Create(ASTContext &C, DeclContext *DC,
2309 SourceLocation AtLoc,
2310 SourceLocation ClassNameLoc,
2311 SourceLocation CategoryNameLoc,
2312 IdentifierInfo *Id,
2313 ObjCInterfaceDecl *IDecl,
2314 ObjCTypeParamList *typeParamList,
2315 SourceLocation IvarLBraceLoc=SourceLocation(),
2316 SourceLocation IvarRBraceLoc=SourceLocation());
2317 static ObjCCategoryDecl *CreateDeserialized(ASTContext &C, unsigned ID);
2318
2319 ObjCInterfaceDecl *getClassInterface() { return ClassInterface; }
2320 const ObjCInterfaceDecl *getClassInterface() const { return ClassInterface; }
2321
2322 /// Retrieve the type parameter list associated with this category or
2323 /// extension.
2324 ObjCTypeParamList *getTypeParamList() const { return TypeParamList; }
2325
2326 /// Set the type parameters of this category.
2327 ///
2328 /// This function is used by the AST importer, which must import the type
2329 /// parameters after creating their DeclContext to avoid loops.
2330 void setTypeParamList(ObjCTypeParamList *TPL);
2331
2332
2333 ObjCCategoryImplDecl *getImplementation() const;
2334 void setImplementation(ObjCCategoryImplDecl *ImplD);
2335
2336 /// setProtocolList - Set the list of protocols that this interface
2337 /// implements.
2338 void setProtocolList(ObjCProtocolDecl *const*List, unsigned Num,
2339 const SourceLocation *Locs, ASTContext &C) {
2340 ReferencedProtocols.set(List, Num, Locs, C);
2341 }
2342
2343 const ObjCProtocolList &getReferencedProtocols() const {
2344 return ReferencedProtocols;
2345 }
2346
2347 using protocol_iterator = ObjCProtocolList::iterator;
2348 using protocol_range = llvm::iterator_range<protocol_iterator>;
2349
2350 protocol_range protocols() const {
2351 return protocol_range(protocol_begin(), protocol_end());
2352 }
2353
2354 protocol_iterator protocol_begin() const {
2355 return ReferencedProtocols.begin();
2356 }
2357
2358 protocol_iterator protocol_end() const { return ReferencedProtocols.end(); }
2359 unsigned protocol_size() const { return ReferencedProtocols.size(); }
2360
2361 using protocol_loc_iterator = ObjCProtocolList::loc_iterator;
2362 using protocol_loc_range = llvm::iterator_range<protocol_loc_iterator>;
2363
2364 protocol_loc_range protocol_locs() const {
2365 return protocol_loc_range(protocol_loc_begin(), protocol_loc_end());
2366 }
2367
2368 protocol_loc_iterator protocol_loc_begin() const {
2369 return ReferencedProtocols.loc_begin();
2370 }
2371
2372 protocol_loc_iterator protocol_loc_end() const {
2373 return ReferencedProtocols.loc_end();
2374 }
2375
2376 ObjCCategoryDecl *getNextClassCategory() const { return NextClassCategory; }
2377
2378 /// Retrieve the pointer to the next stored category (or extension),
2379 /// which may be hidden.
2380 ObjCCategoryDecl *getNextClassCategoryRaw() const {
2381 return NextClassCategory;
2382 }
2383
2384 bool IsClassExtension() const { return getIdentifier() == nullptr; }
2385
2386 using ivar_iterator = specific_decl_iterator<ObjCIvarDecl>;
2387 using ivar_range = llvm::iterator_range<specific_decl_iterator<ObjCIvarDecl>>;
2388
2389 ivar_range ivars() const { return ivar_range(ivar_begin(), ivar_end()); }
2390
2391 ivar_iterator ivar_begin() const {
2392 return ivar_iterator(decls_begin());
2393 }
2394
2395 ivar_iterator ivar_end() const {
2396 return ivar_iterator(decls_end());
2397 }
2398
2399 unsigned ivar_size() const {
2400 return std::distance(ivar_begin(), ivar_end());
2401 }
2402
2403 bool ivar_empty() const {
2404 return ivar_begin() == ivar_end();
13
Calling 'operator=='
19
Returning from 'operator=='
20
Returning zero, which participates in a condition later
2405 }
2406
2407 SourceLocation getCategoryNameLoc() const { return CategoryNameLoc; }
2408 void setCategoryNameLoc(SourceLocation Loc) { CategoryNameLoc = Loc; }
2409
2410 void setIvarLBraceLoc(SourceLocation Loc) { IvarLBraceLoc = Loc; }
2411 SourceLocation getIvarLBraceLoc() const { return IvarLBraceLoc; }
2412 void setIvarRBraceLoc(SourceLocation Loc) { IvarRBraceLoc = Loc; }
2413 SourceLocation getIvarRBraceLoc() const { return IvarRBraceLoc; }
2414
2415 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
2416 static bool classofKind(Kind K) { return K == ObjCCategory; }
2417};
2418
2419class ObjCImplDecl : public ObjCContainerDecl {
2420 /// Class interface for this class/category implementation
2421 ObjCInterfaceDecl *ClassInterface;
2422
2423 void anchor() override;
2424
2425protected:
2426 ObjCImplDecl(Kind DK, DeclContext *DC,
2427 ObjCInterfaceDecl *classInterface,
2428 IdentifierInfo *Id,
2429 SourceLocation nameLoc, SourceLocation atStartLoc)
2430 : ObjCContainerDecl(DK, DC, Id, nameLoc, atStartLoc),
2431 ClassInterface(classInterface) {}
2432
2433public:
2434 const ObjCInterfaceDecl *getClassInterface() const { return ClassInterface; }
2435 ObjCInterfaceDecl *getClassInterface() { return ClassInterface; }
2436 void setClassInterface(ObjCInterfaceDecl *IFace);
2437
2438 void addInstanceMethod(ObjCMethodDecl *method) {
2439 // FIXME: Context should be set correctly before we get here.
2440 method->setLexicalDeclContext(this);
2441 addDecl(method);
2442 }
2443
2444 void addClassMethod(ObjCMethodDecl *method) {
2445 // FIXME: Context should be set correctly before we get here.
2446 method->setLexicalDeclContext(this);
2447 addDecl(method);
2448 }
2449
2450 void addPropertyImplementation(ObjCPropertyImplDecl *property);
2451
2452 ObjCPropertyImplDecl *FindPropertyImplDecl(IdentifierInfo *propertyId,
2453 ObjCPropertyQueryKind queryKind) const;
2454 ObjCPropertyImplDecl *FindPropertyImplIvarDecl(IdentifierInfo *ivarId) const;
2455
2456 // Iterator access to properties.
2457 using propimpl_iterator = specific_decl_iterator<ObjCPropertyImplDecl>;
2458 using propimpl_range =
2459 llvm::iterator_range<specific_decl_iterator<ObjCPropertyImplDecl>>;
2460
2461 propimpl_range property_impls() const {
2462 return propimpl_range(propimpl_begin(), propimpl_end());
2463 }
2464
2465 propimpl_iterator propimpl_begin() const {
2466 return propimpl_iterator(decls_begin());
2467 }
2468
2469 propimpl_iterator propimpl_end() const {
2470 return propimpl_iterator(decls_end());
2471 }
2472
2473 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
2474
2475 static bool classofKind(Kind K) {
2476 return K >= firstObjCImpl && K <= lastObjCImpl;
2477 }
2478};
2479
2480/// ObjCCategoryImplDecl - An object of this class encapsulates a category
2481/// \@implementation declaration. If a category class has declaration of a
2482/// property, its implementation must be specified in the category's
2483/// \@implementation declaration. Example:
2484/// \@interface I \@end
2485/// \@interface I(CATEGORY)
2486/// \@property int p1, d1;
2487/// \@end
2488/// \@implementation I(CATEGORY)
2489/// \@dynamic p1,d1;
2490/// \@end
2491///
2492/// ObjCCategoryImplDecl
2493class ObjCCategoryImplDecl : public ObjCImplDecl {
2494 // Category name location
2495 SourceLocation CategoryNameLoc;
2496
2497 ObjCCategoryImplDecl(DeclContext *DC, IdentifierInfo *Id,
2498 ObjCInterfaceDecl *classInterface,
2499 SourceLocation nameLoc, SourceLocation atStartLoc,
2500 SourceLocation CategoryNameLoc)
2501 : ObjCImplDecl(ObjCCategoryImpl, DC, classInterface, Id,
2502 nameLoc, atStartLoc),
2503 CategoryNameLoc(CategoryNameLoc) {}
2504
2505 void anchor() override;
2506
2507public:
2508 friend class ASTDeclReader;
2509 friend class ASTDeclWriter;
2510
2511 static ObjCCategoryImplDecl *Create(ASTContext &C, DeclContext *DC,
2512 IdentifierInfo *Id,
2513 ObjCInterfaceDecl *classInterface,
2514 SourceLocation nameLoc,
2515 SourceLocation atStartLoc,
2516 SourceLocation CategoryNameLoc);
2517 static ObjCCategoryImplDecl *CreateDeserialized(ASTContext &C, unsigned ID);
2518
2519 ObjCCategoryDecl *getCategoryDecl() const;
2520
2521 SourceLocation getCategoryNameLoc() const { return CategoryNameLoc; }
2522
2523 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
2524 static bool classofKind(Kind K) { return K == ObjCCategoryImpl;}
2525};
2526
2527raw_ostream &operator<<(raw_ostream &OS, const ObjCCategoryImplDecl &CID);
2528
2529/// ObjCImplementationDecl - Represents a class definition - this is where
2530/// method definitions are specified. For example:
2531///
2532/// @code
2533/// \@implementation MyClass
2534/// - (void)myMethod { /* do something */ }
2535/// \@end
2536/// @endcode
2537///
2538/// In a non-fragile runtime, instance variables can appear in the class
2539/// interface, class extensions (nameless categories), and in the implementation
2540/// itself, as well as being synthesized as backing storage for properties.
2541///
2542/// In a fragile runtime, instance variables are specified in the class
2543/// interface, \em not in the implementation. Nevertheless (for legacy reasons),
2544/// we allow instance variables to be specified in the implementation. When
2545/// specified, they need to be \em identical to the interface.
2546class ObjCImplementationDecl : public ObjCImplDecl {
2547 /// Implementation Class's super class.
2548 ObjCInterfaceDecl *SuperClass;
2549 SourceLocation SuperLoc;
2550
2551 /// \@implementation may have private ivars.
2552 SourceLocation IvarLBraceLoc;
2553 SourceLocation IvarRBraceLoc;
2554
2555 /// Support for ivar initialization.
2556 /// The arguments used to initialize the ivars
2557 LazyCXXCtorInitializersPtr IvarInitializers;
2558 unsigned NumIvarInitializers = 0;
2559
2560 /// Do the ivars of this class require initialization other than
2561 /// zero-initialization?
2562 bool HasNonZeroConstructors : 1;
2563
2564 /// Do the ivars of this class require non-trivial destruction?
2565 bool HasDestructors : 1;
2566
2567 ObjCImplementationDecl(DeclContext *DC,
2568 ObjCInterfaceDecl *classInterface,
2569 ObjCInterfaceDecl *superDecl,
2570 SourceLocation nameLoc, SourceLocation atStartLoc,
2571 SourceLocation superLoc = SourceLocation(),
2572 SourceLocation IvarLBraceLoc=SourceLocation(),
2573 SourceLocation IvarRBraceLoc=SourceLocation())
2574 : ObjCImplDecl(ObjCImplementation, DC, classInterface,
2575 classInterface ? classInterface->getIdentifier()
2576 : nullptr,
2577 nameLoc, atStartLoc),
2578 SuperClass(superDecl), SuperLoc(superLoc),
2579 IvarLBraceLoc(IvarLBraceLoc), IvarRBraceLoc(IvarRBraceLoc),
2580 HasNonZeroConstructors(false), HasDestructors(false) {}
2581
2582 void anchor() override;
2583
2584public:
2585 friend class ASTDeclReader;
2586 friend class ASTDeclWriter;
2587
2588 static ObjCImplementationDecl *Create(ASTContext &C, DeclContext *DC,
2589 ObjCInterfaceDecl *classInterface,
2590 ObjCInterfaceDecl *superDecl,
2591 SourceLocation nameLoc,
2592 SourceLocation atStartLoc,
2593 SourceLocation superLoc = SourceLocation(),
2594 SourceLocation IvarLBraceLoc=SourceLocation(),
2595 SourceLocation IvarRBraceLoc=SourceLocation());
2596
2597 static ObjCImplementationDecl *CreateDeserialized(ASTContext &C, unsigned ID);
2598
2599 /// init_iterator - Iterates through the ivar initializer list.
2600 using init_iterator = CXXCtorInitializer **;
2601
2602 /// init_const_iterator - Iterates through the ivar initializer list.
2603 using init_const_iterator = CXXCtorInitializer * const *;
2604
2605 using init_range = llvm::iterator_range<init_iterator>;
2606 using init_const_range = llvm::iterator_range<init_const_iterator>;
2607
2608 init_range inits() { return init_range(init_begin(), init_end()); }
2609
2610 init_const_range inits() const {
2611 return init_const_range(init_begin(), init_end());
2612 }
2613
2614 /// init_begin() - Retrieve an iterator to the first initializer.
2615 init_iterator init_begin() {
2616 const auto *ConstThis = this;
2617 return const_cast<init_iterator>(ConstThis->init_begin());
2618 }
2619
2620 /// begin() - Retrieve an iterator to the first initializer.
2621 init_const_iterator init_begin() const;
2622
2623 /// init_end() - Retrieve an iterator past the last initializer.
2624 init_iterator init_end() {
2625 return init_begin() + NumIvarInitializers;
2626 }
2627
2628 /// end() - Retrieve an iterator past the last initializer.
2629 init_const_iterator init_end() const {
2630 return init_begin() + NumIvarInitializers;
2631 }
2632
2633 /// getNumArgs - Number of ivars which must be initialized.
2634 unsigned getNumIvarInitializers() const {
2635 return NumIvarInitializers;
2636 }
2637
2638 void setNumIvarInitializers(unsigned numNumIvarInitializers) {
2639 NumIvarInitializers = numNumIvarInitializers;
2640 }
2641
2642 void setIvarInitializers(ASTContext &C,
2643 CXXCtorInitializer ** initializers,
2644 unsigned numInitializers);
2645
2646 /// Do any of the ivars of this class (not counting its base classes)
2647 /// require construction other than zero-initialization?
2648 bool hasNonZeroConstructors() const { return HasNonZeroConstructors; }
2649 void setHasNonZeroConstructors(bool val) { HasNonZeroConstructors = val; }
2650
2651 /// Do any of the ivars of this class (not counting its base classes)
2652 /// require non-trivial destruction?
2653 bool hasDestructors() const { return HasDestructors; }
2654 void setHasDestructors(bool val) { HasDestructors = val; }
2655
2656 /// getIdentifier - Get the identifier that names the class
2657 /// interface associated with this implementation.
2658 IdentifierInfo *getIdentifier() const {
2659 return getClassInterface()->getIdentifier();
2660 }
2661
2662 /// getName - Get the name of identifier for the class interface associated
2663 /// with this implementation as a StringRef.
2664 //
2665 // FIXME: This is a bad API, we are hiding NamedDecl::getName with a different
2666 // meaning.
2667 StringRef getName() const {
2668 assert(getIdentifier() && "Name is not a simple identifier")(static_cast <bool> (getIdentifier() && "Name is not a simple identifier"
) ? void (0) : __assert_fail ("getIdentifier() && \"Name is not a simple identifier\""
, "/build/llvm-toolchain-snapshot-14~++20211016100712+8e1d532707fd/clang/include/clang/AST/DeclObjC.h"
, 2668, __extension__ __PRETTY_FUNCTION__))
;
2669 return getIdentifier()->getName();
2670 }
2671
2672 /// Get the name of the class associated with this interface.
2673 //
2674 // FIXME: Move to StringRef API.
2675 std::string getNameAsString() const { return std::string(getName()); }
2676
2677 /// Produce a name to be used for class's metadata. It comes either via
2678 /// class's objc_runtime_name attribute or class name.
2679 StringRef getObjCRuntimeNameAsString() const;
2680
2681 const ObjCInterfaceDecl *getSuperClass() const { return SuperClass; }
2682 ObjCInterfaceDecl *getSuperClass() { return SuperClass; }
2683 SourceLocation getSuperClassLoc() const { return SuperLoc; }
2684
2685 void setSuperClass(ObjCInterfaceDecl * superCls) { SuperClass = superCls; }
2686
2687 void setIvarLBraceLoc(SourceLocation Loc) { IvarLBraceLoc = Loc; }
2688 SourceLocation getIvarLBraceLoc() const { return IvarLBraceLoc; }
2689 void setIvarRBraceLoc(SourceLocation Loc) { IvarRBraceLoc = Loc; }
2690 SourceLocation getIvarRBraceLoc() const { return IvarRBraceLoc; }
2691
2692 using ivar_iterator = specific_decl_iterator<ObjCIvarDecl>;
2693 using ivar_range = llvm::iterator_range<specific_decl_iterator<ObjCIvarDecl>>;
2694
2695 ivar_range ivars() const { return ivar_range(ivar_begin(), ivar_end()); }
2696
2697 ivar_iterator ivar_begin() const {
2698 return ivar_iterator(decls_begin());
2699 }
2700
2701 ivar_iterator ivar_end() const {
2702 return ivar_iterator(decls_end());
2703 }
2704
2705 unsigned ivar_size() const {
2706 return std::distance(ivar_begin(), ivar_end());
2707 }
2708
2709 bool ivar_empty() const {
2710 return ivar_begin() == ivar_end();
2711 }
2712
2713 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
2714 static bool classofKind(Kind K) { return K == ObjCImplementation; }
2715};
2716
2717raw_ostream &operator<<(raw_ostream &OS, const ObjCImplementationDecl &ID);
2718
2719/// ObjCCompatibleAliasDecl - Represents alias of a class. This alias is
2720/// declared as \@compatibility_alias alias class.
2721class ObjCCompatibleAliasDecl : public NamedDecl {
2722 /// Class that this is an alias of.
2723 ObjCInterfaceDecl *AliasedClass;
2724
2725 ObjCCompatibleAliasDecl(DeclContext *DC, SourceLocation L, IdentifierInfo *Id,
2726 ObjCInterfaceDecl* aliasedClass)
2727 : NamedDecl(ObjCCompatibleAlias, DC, L, Id), AliasedClass(aliasedClass) {}
2728
2729 void anchor() override;
2730
2731public:
2732 static ObjCCompatibleAliasDecl *Create(ASTContext &C, DeclContext *DC,
2733 SourceLocation L, IdentifierInfo *Id,
2734 ObjCInterfaceDecl* aliasedClass);
2735
2736 static ObjCCompatibleAliasDecl *CreateDeserialized(ASTContext &C,
2737 unsigned ID);
2738
2739 const ObjCInterfaceDecl *getClassInterface() const { return AliasedClass; }
2740 ObjCInterfaceDecl *getClassInterface() { return AliasedClass; }
2741 void setClassInterface(ObjCInterfaceDecl *D) { AliasedClass = D; }
2742
2743 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
2744 static bool classofKind(Kind K) { return K == ObjCCompatibleAlias; }
2745};
2746
2747/// ObjCPropertyImplDecl - Represents implementation declaration of a property
2748/// in a class or category implementation block. For example:
2749/// \@synthesize prop1 = ivar1;
2750///
2751class ObjCPropertyImplDecl : public Decl {
2752public:
2753 enum Kind {
2754 Synthesize,
2755 Dynamic
2756 };
2757
2758private:
2759 SourceLocation AtLoc; // location of \@synthesize or \@dynamic
2760
2761 /// For \@synthesize, the location of the ivar, if it was written in
2762 /// the source code.
2763 ///
2764 /// \code
2765 /// \@synthesize int a = b
2766 /// \endcode
2767 SourceLocation IvarLoc;
2768
2769 /// Property declaration being implemented
2770 ObjCPropertyDecl *PropertyDecl;
2771
2772 /// Null for \@dynamic. Required for \@synthesize.
2773 ObjCIvarDecl *PropertyIvarDecl;
2774
2775 /// The getter's definition, which has an empty body if synthesized.
2776 ObjCMethodDecl *GetterMethodDecl = nullptr;
2777 /// The getter's definition, which has an empty body if synthesized.
2778 ObjCMethodDecl *SetterMethodDecl = nullptr;
2779
2780 /// Null for \@dynamic. Non-null if property must be copy-constructed in
2781 /// getter.
2782 Expr *GetterCXXConstructor = nullptr;
2783
2784 /// Null for \@dynamic. Non-null if property has assignment operator to call
2785 /// in Setter synthesis.
2786 Expr *SetterCXXAssignment = nullptr;
2787
2788 ObjCPropertyImplDecl(DeclContext *DC, SourceLocation atLoc, SourceLocation L,
2789 ObjCPropertyDecl *property,
2790 Kind PK,
2791 ObjCIvarDecl *ivarDecl,
2792 SourceLocation ivarLoc)
2793 : Decl(ObjCPropertyImpl, DC, L), AtLoc(atLoc),
2794 IvarLoc(ivarLoc), PropertyDecl(property), PropertyIvarDecl(ivarDecl) {
2795 assert(PK == Dynamic || PropertyIvarDecl)(static_cast <bool> (PK == Dynamic || PropertyIvarDecl)
? void (0) : __assert_fail ("PK == Dynamic || PropertyIvarDecl"
, "/build/llvm-toolchain-snapshot-14~++20211016100712+8e1d532707fd/clang/include/clang/AST/DeclObjC.h"
, 2795, __extension__ __PRETTY_FUNCTION__))
;
2796 }
2797
2798public:
2799 friend class ASTDeclReader;
2800
2801 static ObjCPropertyImplDecl *Create(ASTContext &C, DeclContext *DC,
2802 SourceLocation atLoc, SourceLocation L,
2803 ObjCPropertyDecl *property,
2804 Kind PK,
2805 ObjCIvarDecl *ivarDecl,
2806 SourceLocation ivarLoc);
2807
2808 static ObjCPropertyImplDecl *CreateDeserialized(ASTContext &C, unsigned ID);
2809
2810 SourceRange getSourceRange() const override LLVM_READONLY__attribute__((__pure__));
2811
2812 SourceLocation getBeginLoc() const LLVM_READONLY__attribute__((__pure__)) { return AtLoc; }
2813 void setAtLoc(SourceLocation Loc) { AtLoc = Loc; }
2814
2815 ObjCPropertyDecl *getPropertyDecl() const {
2816 return PropertyDecl;
2817 }
2818 void setPropertyDecl(ObjCPropertyDecl *Prop) { PropertyDecl = Prop; }
2819
2820 Kind getPropertyImplementation() const {
2821 return PropertyIvarDecl ? Synthesize : Dynamic;
2822 }
2823
2824 ObjCIvarDecl *getPropertyIvarDecl() const {
2825 return PropertyIvarDecl;
2826 }
2827 SourceLocation getPropertyIvarDeclLoc() const { return IvarLoc; }
2828
2829 void setPropertyIvarDecl(ObjCIvarDecl *Ivar,
2830 SourceLocation IvarLoc) {
2831 PropertyIvarDecl = Ivar;
2832 this->IvarLoc = IvarLoc;
2833 }
2834
2835 /// For \@synthesize, returns true if an ivar name was explicitly
2836 /// specified.
2837 ///
2838 /// \code
2839 /// \@synthesize int a = b; // true
2840 /// \@synthesize int a; // false
2841 /// \endcode
2842 bool isIvarNameSpecified() const {
2843 return IvarLoc.isValid() && IvarLoc != getLocation();
2844 }
2845
2846 ObjCMethodDecl *getGetterMethodDecl() const { return GetterMethodDecl; }
2847 void setGetterMethodDecl(ObjCMethodDecl *MD) { GetterMethodDecl = MD; }
2848
2849 ObjCMethodDecl *getSetterMethodDecl() const { return SetterMethodDecl; }
2850 void setSetterMethodDecl(ObjCMethodDecl *MD) { SetterMethodDecl = MD; }
2851
2852 Expr *getGetterCXXConstructor() const {
2853 return GetterCXXConstructor;
2854 }
2855
2856 void setGetterCXXConstructor(Expr *getterCXXConstructor) {
2857 GetterCXXConstructor = getterCXXConstructor;
2858 }
2859
2860 Expr *getSetterCXXAssignment() const {
2861 return SetterCXXAssignment;
2862 }
2863
2864 void setSetterCXXAssignment(Expr *setterCXXAssignment) {
2865 SetterCXXAssignment = setterCXXAssignment;
2866 }
2867
2868 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
2869 static bool classofKind(Decl::Kind K) { return K == ObjCPropertyImpl; }
2870};
2871
2872template<bool (*Filter)(ObjCCategoryDecl *)>
2873void
2874ObjCInterfaceDecl::filtered_category_iterator<Filter>::
2875findAcceptableCategory() {
2876 while (Current && !Filter(Current))
2877 Current = Current->getNextClassCategoryRaw();
2878}
2879
2880template<bool (*Filter)(ObjCCategoryDecl *)>
2881inline ObjCInterfaceDecl::filtered_category_iterator<Filter> &
2882ObjCInterfaceDecl::filtered_category_iterator<Filter>::operator++() {
2883 Current = Current->getNextClassCategoryRaw();
2884 findAcceptableCategory();
2885 return *this;
2886}
2887
2888inline bool ObjCInterfaceDecl::isVisibleCategory(ObjCCategoryDecl *Cat) {
2889 return Cat->isUnconditionallyVisible();
2890}
2891
2892inline bool ObjCInterfaceDecl::isVisibleExtension(ObjCCategoryDecl *Cat) {
2893 return Cat->IsClassExtension() && Cat->isUnconditionallyVisible();
2894}
2895
2896inline bool ObjCInterfaceDecl::isKnownExtension(ObjCCategoryDecl *Cat) {
2897 return Cat->IsClassExtension();
2898}
2899
2900} // namespace clang
2901
2902#endif // LLVM_CLANG_AST_DECLOBJC_H

/build/llvm-toolchain-snapshot-14~++20211016100712+8e1d532707fd/clang/include/clang/AST/DeclBase.h

1//===- DeclBase.h - Base Classes for representing declarations --*- C++ -*-===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8//
9// This file defines the Decl and DeclContext interfaces.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_CLANG_AST_DECLBASE_H
14#define LLVM_CLANG_AST_DECLBASE_H
15
16#include "clang/AST/ASTDumperUtils.h"
17#include "clang/AST/AttrIterator.h"
18#include "clang/AST/DeclarationName.h"
19#include "clang/Basic/IdentifierTable.h"
20#include "clang/Basic/LLVM.h"
21#include "clang/Basic/SourceLocation.h"
22#include "clang/Basic/Specifiers.h"
23#include "llvm/ADT/ArrayRef.h"
24#include "llvm/ADT/PointerIntPair.h"
25#include "llvm/ADT/PointerUnion.h"
26#include "llvm/ADT/iterator.h"
27#include "llvm/ADT/iterator_range.h"
28#include "llvm/Support/Casting.h"
29#include "llvm/Support/Compiler.h"
30#include "llvm/Support/PrettyStackTrace.h"
31#include "llvm/Support/VersionTuple.h"
32#include <algorithm>
33#include <cassert>
34#include <cstddef>
35#include <iterator>
36#include <string>
37#include <type_traits>
38#include <utility>
39
40namespace clang {
41
42class ASTContext;
43class ASTMutationListener;
44class Attr;
45class BlockDecl;
46class DeclContext;
47class ExternalSourceSymbolAttr;
48class FunctionDecl;
49class FunctionType;
50class IdentifierInfo;
51enum Linkage : unsigned char;
52class LinkageSpecDecl;
53class Module;
54class NamedDecl;
55class ObjCCategoryDecl;
56class ObjCCategoryImplDecl;
57class ObjCContainerDecl;
58class ObjCImplDecl;
59class ObjCImplementationDecl;
60class ObjCInterfaceDecl;
61class ObjCMethodDecl;
62class ObjCProtocolDecl;
63struct PrintingPolicy;
64class RecordDecl;
65class SourceManager;
66class Stmt;
67class StoredDeclsMap;
68class TemplateDecl;
69class TemplateParameterList;
70class TranslationUnitDecl;
71class UsingDirectiveDecl;
72
73/// Captures the result of checking the availability of a
74/// declaration.
75enum AvailabilityResult {
76 AR_Available = 0,
77 AR_NotYetIntroduced,
78 AR_Deprecated,
79 AR_Unavailable
80};
81
82/// Decl - This represents one declaration (or definition), e.g. a variable,
83/// typedef, function, struct, etc.
84///
85/// Note: There are objects tacked on before the *beginning* of Decl
86/// (and its subclasses) in its Decl::operator new(). Proper alignment
87/// of all subclasses (not requiring more than the alignment of Decl) is
88/// asserted in DeclBase.cpp.
89class alignas(8) Decl {
90public:
91 /// Lists the kind of concrete classes of Decl.
92 enum Kind {
93#define DECL(DERIVED, BASE) DERIVED,
94#define ABSTRACT_DECL(DECL)
95#define DECL_RANGE(BASE, START, END) \
96 first##BASE = START, last##BASE = END,
97#define LAST_DECL_RANGE(BASE, START, END) \
98 first##BASE = START, last##BASE = END
99#include "clang/AST/DeclNodes.inc"
100 };
101
102 /// A placeholder type used to construct an empty shell of a
103 /// decl-derived type that will be filled in later (e.g., by some
104 /// deserialization method).
105 struct EmptyShell {};
106
107 /// IdentifierNamespace - The different namespaces in which
108 /// declarations may appear. According to C99 6.2.3, there are
109 /// four namespaces, labels, tags, members and ordinary
110 /// identifiers. C++ describes lookup completely differently:
111 /// certain lookups merely "ignore" certain kinds of declarations,
112 /// usually based on whether the declaration is of a type, etc.
113 ///
114 /// These are meant as bitmasks, so that searches in
115 /// C++ can look into the "tag" namespace during ordinary lookup.
116 ///
117 /// Decl currently provides 15 bits of IDNS bits.
118 enum IdentifierNamespace {
119 /// Labels, declared with 'x:' and referenced with 'goto x'.
120 IDNS_Label = 0x0001,
121
122 /// Tags, declared with 'struct foo;' and referenced with
123 /// 'struct foo'. All tags are also types. This is what
124 /// elaborated-type-specifiers look for in C.
125 /// This also contains names that conflict with tags in the
126 /// same scope but that are otherwise ordinary names (non-type
127 /// template parameters and indirect field declarations).
128 IDNS_Tag = 0x0002,
129
130 /// Types, declared with 'struct foo', typedefs, etc.
131 /// This is what elaborated-type-specifiers look for in C++,
132 /// but note that it's ill-formed to find a non-tag.
133 IDNS_Type = 0x0004,
134
135 /// Members, declared with object declarations within tag
136 /// definitions. In C, these can only be found by "qualified"
137 /// lookup in member expressions. In C++, they're found by
138 /// normal lookup.
139 IDNS_Member = 0x0008,
140
141 /// Namespaces, declared with 'namespace foo {}'.
142 /// Lookup for nested-name-specifiers find these.
143 IDNS_Namespace = 0x0010,
144
145 /// Ordinary names. In C, everything that's not a label, tag,
146 /// member, or function-local extern ends up here.
147 IDNS_Ordinary = 0x0020,
148
149 /// Objective C \@protocol.
150 IDNS_ObjCProtocol = 0x0040,
151
152 /// This declaration is a friend function. A friend function
153 /// declaration is always in this namespace but may also be in
154 /// IDNS_Ordinary if it was previously declared.
155 IDNS_OrdinaryFriend = 0x0080,
156
157 /// This declaration is a friend class. A friend class
158 /// declaration is always in this namespace but may also be in
159 /// IDNS_Tag|IDNS_Type if it was previously declared.
160 IDNS_TagFriend = 0x0100,
161
162 /// This declaration is a using declaration. A using declaration
163 /// *introduces* a number of other declarations into the current
164 /// scope, and those declarations use the IDNS of their targets,
165 /// but the actual using declarations go in this namespace.
166 IDNS_Using = 0x0200,
167
168 /// This declaration is a C++ operator declared in a non-class
169 /// context. All such operators are also in IDNS_Ordinary.
170 /// C++ lexical operator lookup looks for these.
171 IDNS_NonMemberOperator = 0x0400,
172
173 /// This declaration is a function-local extern declaration of a
174 /// variable or function. This may also be IDNS_Ordinary if it
175 /// has been declared outside any function. These act mostly like
176 /// invisible friend declarations, but are also visible to unqualified
177 /// lookup within the scope of the declaring function.
178 IDNS_LocalExtern = 0x0800,
179
180 /// This declaration is an OpenMP user defined reduction construction.
181 IDNS_OMPReduction = 0x1000,
182
183 /// This declaration is an OpenMP user defined mapper.
184 IDNS_OMPMapper = 0x2000,
185 };
186
187 /// ObjCDeclQualifier - 'Qualifiers' written next to the return and
188 /// parameter types in method declarations. Other than remembering
189 /// them and mangling them into the method's signature string, these
190 /// are ignored by the compiler; they are consumed by certain
191 /// remote-messaging frameworks.
192 ///
193 /// in, inout, and out are mutually exclusive and apply only to
194 /// method parameters. bycopy and byref are mutually exclusive and
195 /// apply only to method parameters (?). oneway applies only to
196 /// results. All of these expect their corresponding parameter to
197 /// have a particular type. None of this is currently enforced by
198 /// clang.
199 ///
200 /// This should be kept in sync with ObjCDeclSpec::ObjCDeclQualifier.
201 enum ObjCDeclQualifier {
202 OBJC_TQ_None = 0x0,
203 OBJC_TQ_In = 0x1,
204 OBJC_TQ_Inout = 0x2,
205 OBJC_TQ_Out = 0x4,
206 OBJC_TQ_Bycopy = 0x8,
207 OBJC_TQ_Byref = 0x10,
208 OBJC_TQ_Oneway = 0x20,
209
210 /// The nullability qualifier is set when the nullability of the
211 /// result or parameter was expressed via a context-sensitive
212 /// keyword.
213 OBJC_TQ_CSNullability = 0x40
214 };
215
216 /// The kind of ownership a declaration has, for visibility purposes.
217 /// This enumeration is designed such that higher values represent higher
218 /// levels of name hiding.
219 enum class ModuleOwnershipKind : unsigned {
220 /// This declaration is not owned by a module.
221 Unowned,
222
223 /// This declaration has an owning module, but is globally visible
224 /// (typically because its owning module is visible and we know that
225 /// modules cannot later become hidden in this compilation).
226 /// After serialization and deserialization, this will be converted
227 /// to VisibleWhenImported.
228 Visible,
229
230 /// This declaration has an owning module, and is visible when that
231 /// module is imported.
232 VisibleWhenImported,
233
234 /// This declaration has an owning module, but is only visible to
235 /// lookups that occur within that module.
236 ModulePrivate
237 };
238
239protected:
240 /// The next declaration within the same lexical
241 /// DeclContext. These pointers form the linked list that is
242 /// traversed via DeclContext's decls_begin()/decls_end().
243 ///
244 /// The extra two bits are used for the ModuleOwnershipKind.
245 llvm::PointerIntPair<Decl *, 2, ModuleOwnershipKind> NextInContextAndBits;
246
247private:
248 friend class DeclContext;
249
250 struct MultipleDC {
251 DeclContext *SemanticDC;
252 DeclContext *LexicalDC;
253 };
254
255 /// DeclCtx - Holds either a DeclContext* or a MultipleDC*.
256 /// For declarations that don't contain C++ scope specifiers, it contains
257 /// the DeclContext where the Decl was declared.
258 /// For declarations with C++ scope specifiers, it contains a MultipleDC*
259 /// with the context where it semantically belongs (SemanticDC) and the
260 /// context where it was lexically declared (LexicalDC).
261 /// e.g.:
262 ///
263 /// namespace A {
264 /// void f(); // SemanticDC == LexicalDC == 'namespace A'
265 /// }
266 /// void A::f(); // SemanticDC == namespace 'A'
267 /// // LexicalDC == global namespace
268 llvm::PointerUnion<DeclContext*, MultipleDC*> DeclCtx;
269
270 bool isInSemaDC() const { return DeclCtx.is<DeclContext*>(); }
271 bool isOutOfSemaDC() const { return DeclCtx.is<MultipleDC*>(); }
272
273 MultipleDC *getMultipleDC() const {
274 return DeclCtx.get<MultipleDC*>();
275 }
276
277 DeclContext *getSemanticDC() const {
278 return DeclCtx.get<DeclContext*>();
279 }
280
281 /// Loc - The location of this decl.
282 SourceLocation Loc;
283
284 /// DeclKind - This indicates which class this is.
285 unsigned DeclKind : 7;
286
287 /// InvalidDecl - This indicates a semantic error occurred.
288 unsigned InvalidDecl : 1;
289
290 /// HasAttrs - This indicates whether the decl has attributes or not.
291 unsigned HasAttrs : 1;
292
293 /// Implicit - Whether this declaration was implicitly generated by
294 /// the implementation rather than explicitly written by the user.
295 unsigned Implicit : 1;
296
297 /// Whether this declaration was "used", meaning that a definition is
298 /// required.
299 unsigned Used : 1;
300
301 /// Whether this declaration was "referenced".
302 /// The difference with 'Used' is whether the reference appears in a
303 /// evaluated context or not, e.g. functions used in uninstantiated templates
304 /// are regarded as "referenced" but not "used".
305 unsigned Referenced : 1;
306
307 /// Whether this declaration is a top-level declaration (function,
308 /// global variable, etc.) that is lexically inside an objc container
309 /// definition.
310 unsigned TopLevelDeclInObjCContainer : 1;
311
312 /// Whether statistic collection is enabled.
313 static bool StatisticsEnabled;
314
315protected:
316 friend class ASTDeclReader;
317 friend class ASTDeclWriter;
318 friend class ASTNodeImporter;
319 friend class ASTReader;
320 friend class CXXClassMemberWrapper;
321 friend class LinkageComputer;
322 template<typename decl_type> friend class Redeclarable;
323
324 /// Access - Used by C++ decls for the access specifier.
325 // NOTE: VC++ treats enums as signed, avoid using the AccessSpecifier enum
326 unsigned Access : 2;
327
328 /// Whether this declaration was loaded from an AST file.
329 unsigned FromASTFile : 1;
330
331 /// IdentifierNamespace - This specifies what IDNS_* namespace this lives in.
332 unsigned IdentifierNamespace : 14;
333
334 /// If 0, we have not computed the linkage of this declaration.
335 /// Otherwise, it is the linkage + 1.
336 mutable unsigned CacheValidAndLinkage : 3;
337
338 /// Allocate memory for a deserialized declaration.
339 ///
340 /// This routine must be used to allocate memory for any declaration that is
341 /// deserialized from a module file.
342 ///
343 /// \param Size The size of the allocated object.
344 /// \param Ctx The context in which we will allocate memory.
345 /// \param ID The global ID of the deserialized declaration.
346 /// \param Extra The amount of extra space to allocate after the object.
347 void *operator new(std::size_t Size, const ASTContext &Ctx, unsigned ID,
348 std::size_t Extra = 0);
349
350 /// Allocate memory for a non-deserialized declaration.
351 void *operator new(std::size_t Size, const ASTContext &Ctx,
352 DeclContext *Parent, std::size_t Extra = 0);
353
354private:
355 bool AccessDeclContextSanity() const;
356
357 /// Get the module ownership kind to use for a local lexical child of \p DC,
358 /// which may be either a local or (rarely) an imported declaration.
359 static ModuleOwnershipKind getModuleOwnershipKindForChildOf(DeclContext *DC) {
360 if (DC) {
361 auto *D = cast<Decl>(DC);
362 auto MOK = D->getModuleOwnershipKind();
363 if (MOK != ModuleOwnershipKind::Unowned &&
364 (!D->isFromASTFile() || D->hasLocalOwningModuleStorage()))
365 return MOK;
366 // If D is not local and we have no local module storage, then we don't
367 // need to track module ownership at all.
368 }
369 return ModuleOwnershipKind::Unowned;
370 }
371
372public:
373 Decl() = delete;
374 Decl(const Decl&) = delete;
375 Decl(Decl &&) = delete;
376 Decl &operator=(const Decl&) = delete;
377 Decl &operator=(Decl&&) = delete;
378
379protected:
380 Decl(Kind DK, DeclContext *DC, SourceLocation L)
381 : NextInContextAndBits(nullptr, getModuleOwnershipKindForChildOf(DC)),
382 DeclCtx(DC), Loc(L), DeclKind(DK), InvalidDecl(false), HasAttrs(false),
383 Implicit(false), Used(false), Referenced(false),
384 TopLevelDeclInObjCContainer(false), Access(AS_none), FromASTFile(0),
385 IdentifierNamespace(getIdentifierNamespaceForKind(DK)),
386 CacheValidAndLinkage(0) {
387 if (StatisticsEnabled) add(DK);
388 }
389
390 Decl(Kind DK, EmptyShell Empty)
391 : DeclKind(DK), InvalidDecl(false), HasAttrs(false), Implicit(false),
392 Used(false), Referenced(false), TopLevelDeclInObjCContainer(false),
393 Access(AS_none), FromASTFile(0),
394 IdentifierNamespace(getIdentifierNamespaceForKind(DK)),
395 CacheValidAndLinkage(0) {
396 if (StatisticsEnabled) add(DK);
397 }
398
399 virtual ~Decl();
400
401 /// Update a potentially out-of-date declaration.
402 void updateOutOfDate(IdentifierInfo &II) const;
403
404 Linkage getCachedLinkage() const {
405 return Linkage(CacheValidAndLinkage - 1);
406 }
407
408 void setCachedLinkage(Linkage L) const {
409 CacheValidAndLinkage = L + 1;
410 }
411
412 bool hasCachedLinkage() const {
413 return CacheValidAndLinkage;
414 }
415
416public:
417 /// Source range that this declaration covers.
418 virtual SourceRange getSourceRange() const LLVM_READONLY__attribute__((__pure__)) {
419 return SourceRange(getLocation(), getLocation());
420 }
421
422 SourceLocation getBeginLoc() const LLVM_READONLY__attribute__((__pure__)) {
423 return getSourceRange().getBegin();
424 }
425
426 SourceLocation getEndLoc() const LLVM_READONLY__attribute__((__pure__)) {
427 return getSourceRange().getEnd();
428 }
429
430 SourceLocation getLocation() const { return Loc; }
431 void setLocation(SourceLocation L) { Loc = L; }
432
433 Kind getKind() const { return static_cast<Kind>(DeclKind); }
434 const char *getDeclKindName() const;
435
436 Decl *getNextDeclInContext() { return NextInContextAndBits.getPointer(); }
437 const Decl *getNextDeclInContext() const {return NextInContextAndBits.getPointer();}
438
439 DeclContext *getDeclContext() {
440 if (isInSemaDC())
441 return getSemanticDC();
442 return getMultipleDC()->SemanticDC;
443 }
444 const DeclContext *getDeclContext() const {
445 return const_cast<Decl*>(this)->getDeclContext();
446 }
447
448 /// Find the innermost non-closure ancestor of this declaration,
449 /// walking up through blocks, lambdas, etc. If that ancestor is
450 /// not a code context (!isFunctionOrMethod()), returns null.
451 ///
452 /// A declaration may be its own non-closure context.
453 Decl *getNonClosureContext();
454 const Decl *getNonClosureContext() const {
455 return const_cast<Decl*>(this)->getNonClosureContext();
456 }
457
458 TranslationUnitDecl *getTranslationUnitDecl();
459 const TranslationUnitDecl *getTranslationUnitDecl() const {
460 return const_cast<Decl*>(this)->getTranslationUnitDecl();
461 }
462
463 bool isInAnonymousNamespace() const;
464
465 bool isInStdNamespace() const;
466
467 ASTContext &getASTContext() const LLVM_READONLY__attribute__((__pure__));
468
469 /// Helper to get the language options from the ASTContext.
470 /// Defined out of line to avoid depending on ASTContext.h.
471 const LangOptions &getLangOpts() const LLVM_READONLY__attribute__((__pure__));
472
473 void setAccess(AccessSpecifier AS) {
474 Access = AS;
475 assert(AccessDeclContextSanity())(static_cast <bool> (AccessDeclContextSanity()) ? void (
0) : __assert_fail ("AccessDeclContextSanity()", "/build/llvm-toolchain-snapshot-14~++20211016100712+8e1d532707fd/clang/include/clang/AST/DeclBase.h"
, 475, __extension__ __PRETTY_FUNCTION__))
;
476 }
477
478 AccessSpecifier getAccess() const {
479 assert(AccessDeclContextSanity())(static_cast <bool> (AccessDeclContextSanity()) ? void (
0) : __assert_fail ("AccessDeclContextSanity()", "/build/llvm-toolchain-snapshot-14~++20211016100712+8e1d532707fd/clang/include/clang/AST/DeclBase.h"
, 479, __extension__ __PRETTY_FUNCTION__))
;
480 return AccessSpecifier(Access);
481 }
482
483 /// Retrieve the access specifier for this declaration, even though
484 /// it may not yet have been properly set.
485 AccessSpecifier getAccessUnsafe() const {
486 return AccessSpecifier(Access);
487 }
488
489 bool hasAttrs() const { return HasAttrs; }
490
491 void setAttrs(const AttrVec& Attrs) {
492 return setAttrsImpl(Attrs, getASTContext());
493 }
494
495 AttrVec &getAttrs() {
496 return const_cast<AttrVec&>(const_cast<const Decl*>(this)->getAttrs());
497 }
498
499 const AttrVec &getAttrs() const;
500 void dropAttrs();
501 void addAttr(Attr *A);
502
503 using attr_iterator = AttrVec::const_iterator;
504 using attr_range = llvm::iterator_range<attr_iterator>;
505
506 attr_range attrs() const {
507 return attr_range(attr_begin(), attr_end());
508 }
509
510 attr_iterator attr_begin() const {
511 return hasAttrs() ? getAttrs().begin() : nullptr;
512 }
513 attr_iterator attr_end() const {
514 return hasAttrs() ? getAttrs().end() : nullptr;
515 }
516
517 template <typename T>
518 void dropAttr() {
519 if (!HasAttrs) return;
520
521 AttrVec &Vec = getAttrs();
522 llvm::erase_if(Vec, [](Attr *A) { return isa<T>(A); });
523
524 if (Vec.empty())
525 HasAttrs = false;
526 }
527
528 template <typename T>
529 llvm::iterator_range<specific_attr_iterator<T>> specific_attrs() const {
530 return llvm::make_range(specific_attr_begin<T>(), specific_attr_end<T>());
531 }
532
533 template <typename T>
534 specific_attr_iterator<T> specific_attr_begin() const {
535 return specific_attr_iterator<T>(attr_begin());
536 }
537
538 template <typename T>
539 specific_attr_iterator<T> specific_attr_end() const {
540 return specific_attr_iterator<T>(attr_end());
541 }
542
543 template<typename T> T *getAttr() const {
544 return hasAttrs() ? getSpecificAttr<T>(getAttrs()) : nullptr;
545 }
546
547 template<typename T> bool hasAttr() const {
548 return hasAttrs() && hasSpecificAttr<T>(getAttrs());
549 }
550
551 /// getMaxAlignment - return the maximum alignment specified by attributes
552 /// on this decl, 0 if there are none.
553 unsigned getMaxAlignment() const;
554
555 /// setInvalidDecl - Indicates the Decl had a semantic error. This
556 /// allows for graceful error recovery.
557 void setInvalidDecl(bool Invalid = true);
558 bool isInvalidDecl() const { return (bool) InvalidDecl; }
559
560 /// isImplicit - Indicates whether the declaration was implicitly
561 /// generated by the implementation. If false, this declaration
562 /// was written explicitly in the source code.
563 bool isImplicit() const { return Implicit; }
564 void setImplicit(bool I = true) { Implicit = I; }
565
566 /// Whether *any* (re-)declaration of the entity was used, meaning that
567 /// a definition is required.
568 ///
569 /// \param CheckUsedAttr When true, also consider the "used" attribute
570 /// (in addition to the "used" bit set by \c setUsed()) when determining
571 /// whether the function is used.
572 bool isUsed(bool CheckUsedAttr = true) const;
573
574 /// Set whether the declaration is used, in the sense of odr-use.
575 ///
576 /// This should only be used immediately after creating a declaration.
577 /// It intentionally doesn't notify any listeners.
578 void setIsUsed() { getCanonicalDecl()->Used = true; }
579
580 /// Mark the declaration used, in the sense of odr-use.
581 ///
582 /// This notifies any mutation listeners in addition to setting a bit
583 /// indicating the declaration is used.
584 void markUsed(ASTContext &C);
585
586 /// Whether any declaration of this entity was referenced.
587 bool isReferenced() const;
588
589 /// Whether this declaration was referenced. This should not be relied
590 /// upon for anything other than debugging.
591 bool isThisDeclarationReferenced() const { return Referenced; }
592
593 void setReferenced(bool R = true) { Referenced = R; }
594
595 /// Whether this declaration is a top-level declaration (function,
596 /// global variable, etc.) that is lexically inside an objc container
597 /// definition.
598 bool isTopLevelDeclInObjCContainer() const {
599 return TopLevelDeclInObjCContainer;
600 }
601
602 void setTopLevelDeclInObjCContainer(bool V = true) {
603 TopLevelDeclInObjCContainer = V;
604 }
605
606 /// Looks on this and related declarations for an applicable
607 /// external source symbol attribute.
608 ExternalSourceSymbolAttr *getExternalSourceSymbolAttr() const;
609
610 /// Whether this declaration was marked as being private to the
611 /// module in which it was defined.
612 bool isModulePrivate() const {
613 return getModuleOwnershipKind() == ModuleOwnershipKind::ModulePrivate;
614 }
615
616 /// Return true if this declaration has an attribute which acts as
617 /// definition of the entity, such as 'alias' or 'ifunc'.
618 bool hasDefiningAttr() const;
619
620 /// Return this declaration's defining attribute if it has one.
621 const Attr *getDefiningAttr() const;
622
623protected:
624 /// Specify that this declaration was marked as being private
625 /// to the module in which it was defined.
626 void setModulePrivate() {
627 // The module-private specifier has no effect on unowned declarations.
628 // FIXME: We should track this in some way for source fidelity.
629 if (getModuleOwnershipKind() == ModuleOwnershipKind::Unowned)
630 return;
631 setModuleOwnershipKind(ModuleOwnershipKind::ModulePrivate);
632 }
633
634public:
635 /// Set the FromASTFile flag. This indicates that this declaration
636 /// was deserialized and not parsed from source code and enables
637 /// features such as module ownership information.
638 void setFromASTFile() {
639 FromASTFile = true;
640 }
641
642 /// Set the owning module ID. This may only be called for
643 /// deserialized Decls.
644 void setOwningModuleID(unsigned ID) {
645 assert(isFromASTFile() && "Only works on a deserialized declaration")(static_cast <bool> (isFromASTFile() && "Only works on a deserialized declaration"
) ? void (0) : __assert_fail ("isFromASTFile() && \"Only works on a deserialized declaration\""
, "/build/llvm-toolchain-snapshot-14~++20211016100712+8e1d532707fd/clang/include/clang/AST/DeclBase.h"
, 645, __extension__ __PRETTY_FUNCTION__))
;
646 *((unsigned*)this - 2) = ID;
647 }
648
649public:
650 /// Determine the availability of the given declaration.
651 ///
652 /// This routine will determine the most restrictive availability of
653 /// the given declaration (e.g., preferring 'unavailable' to
654 /// 'deprecated').
655 ///
656 /// \param Message If non-NULL and the result is not \c
657 /// AR_Available, will be set to a (possibly empty) message
658 /// describing why the declaration has not been introduced, is
659 /// deprecated, or is unavailable.
660 ///
661 /// \param EnclosingVersion The version to compare with. If empty, assume the
662 /// deployment target version.
663 ///
664 /// \param RealizedPlatform If non-NULL and the availability result is found
665 /// in an available attribute it will set to the platform which is written in
666 /// the available attribute.
667 AvailabilityResult
668 getAvailability(std::string *Message = nullptr,
669 VersionTuple EnclosingVersion = VersionTuple(),
670 StringRef *RealizedPlatform = nullptr) const;
671
672 /// Retrieve the version of the target platform in which this
673 /// declaration was introduced.
674 ///
675 /// \returns An empty version tuple if this declaration has no 'introduced'
676 /// availability attributes, or the version tuple that's specified in the
677 /// attribute otherwise.
678 VersionTuple getVersionIntroduced() const;
679
680 /// Determine whether this declaration is marked 'deprecated'.
681 ///
682 /// \param Message If non-NULL and the declaration is deprecated,
683 /// this will be set to the message describing why the declaration
684 /// was deprecated (which may be empty).
685 bool isDeprecated(std::string *Message = nullptr) const {
686 return getAvailability(Message) == AR_Deprecated;
687 }
688
689 /// Determine whether this declaration is marked 'unavailable'.
690 ///
691 /// \param Message If non-NULL and the declaration is unavailable,
692 /// this will be set to the message describing why the declaration
693 /// was made unavailable (which may be empty).
694 bool isUnavailable(std::string *Message = nullptr) const {
695 return getAvailability(Message) == AR_Unavailable;
696 }
697
698 /// Determine whether this is a weak-imported symbol.
699 ///
700 /// Weak-imported symbols are typically marked with the
701 /// 'weak_import' attribute, but may also be marked with an
702 /// 'availability' attribute where we're targing a platform prior to
703 /// the introduction of this feature.
704 bool isWeakImported() const;
705
706 /// Determines whether this symbol can be weak-imported,
707 /// e.g., whether it would be well-formed to add the weak_import
708 /// attribute.
709 ///
710 /// \param IsDefinition Set to \c true to indicate that this
711 /// declaration cannot be weak-imported because it has a definition.
712 bool canBeWeakImported(bool &IsDefinition) const;
713
714 /// Determine whether this declaration came from an AST file (such as
715 /// a precompiled header or module) rather than having been parsed.
716 bool isFromASTFile() const { return FromASTFile; }
717
718 /// Retrieve the global declaration ID associated with this
719 /// declaration, which specifies where this Decl was loaded from.
720 unsigned getGlobalID() const {
721