1 //===--- DeclObjC.h - Classes for representing declarations -----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the DeclObjC interface and subclasses.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CLANG_AST_DECLOBJC_H
15 #define LLVM_CLANG_AST_DECLOBJC_H
17 #include "clang/AST/Decl.h"
18 #include "clang/AST/SelectorLocationsKind.h"
19 #include "llvm/ADT/STLExtras.h"
20 #include "llvm/Support/Compiler.h"
29 class ObjCProtocolDecl;
30 class ObjCCategoryDecl;
31 class ObjCPropertyDecl;
32 class ObjCPropertyImplDecl;
33 class CXXCtorInitializer;
36 ObjCListBase(const ObjCListBase &) LLVM_DELETED_FUNCTION;
37 void operator=(const ObjCListBase &) LLVM_DELETED_FUNCTION;
39 /// List is an array of pointers to objects that are not owned by this object.
44 ObjCListBase() : List(0), NumElts(0) {}
45 unsigned size() const { return NumElts; }
46 bool empty() const { return NumElts == 0; }
49 void set(void *const* InList, unsigned Elts, ASTContext &Ctx);
53 /// ObjCList - This is a simple template class used to hold various lists of
54 /// decls etc, which is heavily used by the ObjC front-end. This only use case
55 /// this supports is setting the list all at once and then reading elements out
58 class ObjCList : public ObjCListBase {
60 void set(T* const* InList, unsigned Elts, ASTContext &Ctx) {
61 ObjCListBase::set(reinterpret_cast<void*const*>(InList), Elts, Ctx);
64 typedef T* const * iterator;
65 iterator begin() const { return (iterator)List; }
66 iterator end() const { return (iterator)List+NumElts; }
68 T* operator[](unsigned Idx) const {
69 assert(Idx < NumElts && "Invalid access");
74 /// \brief A list of Objective-C protocols, along with the source
75 /// locations at which they were referenced.
76 class ObjCProtocolList : public ObjCList<ObjCProtocolDecl> {
77 SourceLocation *Locations;
79 using ObjCList<ObjCProtocolDecl>::set;
82 ObjCProtocolList() : ObjCList<ObjCProtocolDecl>(), Locations(0) { }
84 typedef const SourceLocation *loc_iterator;
85 loc_iterator loc_begin() const { return Locations; }
86 loc_iterator loc_end() const { return Locations + size(); }
88 void set(ObjCProtocolDecl* const* InList, unsigned Elts,
89 const SourceLocation *Locs, ASTContext &Ctx);
93 /// ObjCMethodDecl - Represents an instance or class method declaration.
94 /// ObjC methods can be declared within 4 contexts: class interfaces,
95 /// categories, protocols, and class implementations. While C++ member
96 /// functions leverage C syntax, Objective-C method syntax is modeled after
97 /// Smalltalk (using colons to specify argument types/expressions).
98 /// Here are some brief examples:
100 /// Setter/getter instance methods:
101 /// - (void)setMenu:(NSMenu *)menu;
102 /// - (NSMenu *)menu;
104 /// Instance method that takes 2 NSView arguments:
105 /// - (void)replaceSubview:(NSView *)oldView with:(NSView *)newView;
107 /// Getter class method:
108 /// + (NSMenu *)defaultMenu;
110 /// A selector represents a unique name for a method. The selector names for
111 /// the above methods are setMenu:, menu, replaceSubview:with:, and defaultMenu.
113 class ObjCMethodDecl : public NamedDecl, public DeclContext {
115 enum ImplementationControl { None, Required, Optional };
117 // The conventional meaning of this method; an ObjCMethodFamily.
118 // This is not serialized; instead, it is computed on demand and
120 mutable unsigned Family : ObjCMethodFamilyBitWidth;
122 /// instance (true) or class (false) method.
123 unsigned IsInstance : 1;
124 unsigned IsVariadic : 1;
126 /// True if this method is the getter or setter for an explicit property.
127 unsigned IsPropertyAccessor : 1;
129 // Method has a definition.
130 unsigned IsDefined : 1;
132 /// \brief Method redeclaration in the same interface.
133 unsigned IsRedeclaration : 1;
135 /// \brief Is redeclared in the same interface.
136 mutable unsigned HasRedeclaration : 1;
138 // NOTE: VC++ treats enums as signed, avoid using ImplementationControl enum
139 /// \@required/\@optional
140 unsigned DeclImplementation : 2;
142 // NOTE: VC++ treats enums as signed, avoid using the ObjCDeclQualifier enum
144 unsigned objcDeclQualifier : 6;
146 /// \brief Indicates whether this method has a related result type.
147 unsigned RelatedResultType : 1;
149 /// \brief Whether the locations of the selector identifiers are in a
150 /// "standard" position, a enum SelectorLocationsKind.
151 unsigned SelLocsKind : 2;
153 /// \brief Whether this method overrides any other in the class hierarchy.
155 /// A method is said to override any method in the class's
156 /// base classes, its protocols, or its categories' protocols, that has
157 /// the same selector and is of the same kind (class or instance).
158 /// A method in an implementation is not considered as overriding the same
159 /// method in the interface or its categories.
160 unsigned IsOverriding : 1;
162 /// \brief Indicates if the method was a definition but its body was skipped.
163 unsigned HasSkippedBody : 1;
165 // Result type of this method.
166 QualType MethodDeclType;
168 // Type source information for the result type.
169 TypeSourceInfo *ResultTInfo;
171 /// \brief Array of ParmVarDecls for the formal parameters of this method
172 /// and optionally followed by selector locations.
173 void *ParamsAndSelLocs;
176 /// List of attributes for this method declaration.
177 SourceLocation DeclEndLoc; // the location of the ';' or '{'.
179 // The following are only used for method definitions, null otherwise.
180 LazyDeclStmtPtr Body;
182 /// SelfDecl - Decl for the implicit self parameter. This is lazily
183 /// constructed by createImplicitParams.
184 ImplicitParamDecl *SelfDecl;
185 /// CmdDecl - Decl for the implicit _cmd parameter. This is lazily
186 /// constructed by createImplicitParams.
187 ImplicitParamDecl *CmdDecl;
189 SelectorLocationsKind getSelLocsKind() const {
190 return (SelectorLocationsKind)SelLocsKind;
192 bool hasStandardSelLocs() const {
193 return getSelLocsKind() != SelLoc_NonStandard;
196 /// \brief Get a pointer to the stored selector identifiers locations array.
197 /// No locations will be stored if HasStandardSelLocs is true.
198 SourceLocation *getStoredSelLocs() {
199 return reinterpret_cast<SourceLocation*>(getParams() + NumParams);
201 const SourceLocation *getStoredSelLocs() const {
202 return reinterpret_cast<const SourceLocation*>(getParams() + NumParams);
205 /// \brief Get a pointer to the stored selector identifiers locations array.
206 /// No locations will be stored if HasStandardSelLocs is true.
207 ParmVarDecl **getParams() {
208 return reinterpret_cast<ParmVarDecl **>(ParamsAndSelLocs);
210 const ParmVarDecl *const *getParams() const {
211 return reinterpret_cast<const ParmVarDecl *const *>(ParamsAndSelLocs);
214 /// \brief Get the number of stored selector identifiers locations.
215 /// No locations will be stored if HasStandardSelLocs is true.
216 unsigned getNumStoredSelLocs() const {
217 if (hasStandardSelLocs())
219 return getNumSelectorLocs();
222 void setParamsAndSelLocs(ASTContext &C,
223 ArrayRef<ParmVarDecl*> Params,
224 ArrayRef<SourceLocation> SelLocs);
226 ObjCMethodDecl(SourceLocation beginLoc, SourceLocation endLoc,
227 Selector SelInfo, QualType T,
228 TypeSourceInfo *ResultTInfo,
229 DeclContext *contextDecl,
230 bool isInstance = true,
231 bool isVariadic = false,
232 bool isPropertyAccessor = false,
233 bool isImplicitlyDeclared = false,
234 bool isDefined = false,
235 ImplementationControl impControl = None,
236 bool HasRelatedResultType = false)
237 : NamedDecl(ObjCMethod, contextDecl, beginLoc, SelInfo),
238 DeclContext(ObjCMethod), Family(InvalidObjCMethodFamily),
239 IsInstance(isInstance), IsVariadic(isVariadic),
240 IsPropertyAccessor(isPropertyAccessor),
241 IsDefined(isDefined), IsRedeclaration(0), HasRedeclaration(0),
242 DeclImplementation(impControl), objcDeclQualifier(OBJC_TQ_None),
243 RelatedResultType(HasRelatedResultType),
244 SelLocsKind(SelLoc_StandardNoSpace), IsOverriding(0), HasSkippedBody(0),
245 MethodDeclType(T), ResultTInfo(ResultTInfo),
246 ParamsAndSelLocs(0), NumParams(0),
247 DeclEndLoc(endLoc), Body(), SelfDecl(0), CmdDecl(0) {
248 setImplicit(isImplicitlyDeclared);
251 /// \brief A definition will return its interface declaration.
252 /// An interface declaration will return its definition.
253 /// Otherwise it will return itself.
254 virtual ObjCMethodDecl *getNextRedeclaration();
257 static ObjCMethodDecl *Create(ASTContext &C,
258 SourceLocation beginLoc,
259 SourceLocation endLoc,
262 TypeSourceInfo *ResultTInfo,
263 DeclContext *contextDecl,
264 bool isInstance = true,
265 bool isVariadic = false,
266 bool isPropertyAccessor = false,
267 bool isImplicitlyDeclared = false,
268 bool isDefined = false,
269 ImplementationControl impControl = None,
270 bool HasRelatedResultType = false);
272 static ObjCMethodDecl *CreateDeserialized(ASTContext &C, unsigned ID);
274 virtual ObjCMethodDecl *getCanonicalDecl();
275 const ObjCMethodDecl *getCanonicalDecl() const {
276 return const_cast<ObjCMethodDecl*>(this)->getCanonicalDecl();
279 ObjCDeclQualifier getObjCDeclQualifier() const {
280 return ObjCDeclQualifier(objcDeclQualifier);
282 void setObjCDeclQualifier(ObjCDeclQualifier QV) { objcDeclQualifier = QV; }
284 /// \brief Determine whether this method has a result type that is related
285 /// to the message receiver's type.
286 bool hasRelatedResultType() const { return RelatedResultType; }
288 /// \brief Note whether this method has a related result type.
289 void SetRelatedResultType(bool RRT = true) { RelatedResultType = RRT; }
291 /// \brief True if this is a method redeclaration in the same interface.
292 bool isRedeclaration() const { return IsRedeclaration; }
293 void setAsRedeclaration(const ObjCMethodDecl *PrevMethod);
295 /// \brief Returns the location where the declarator ends. It will be
296 /// the location of ';' for a method declaration and the location of '{'
297 /// for a method definition.
298 SourceLocation getDeclaratorEndLoc() const { return DeclEndLoc; }
300 // Location information, modeled after the Stmt API.
301 SourceLocation getLocStart() const LLVM_READONLY { return getLocation(); }
302 SourceLocation getLocEnd() const LLVM_READONLY;
303 virtual SourceRange getSourceRange() const LLVM_READONLY {
304 return SourceRange(getLocation(), getLocEnd());
307 SourceLocation getSelectorStartLoc() const {
309 return getLocStart();
310 return getSelectorLoc(0);
312 SourceLocation getSelectorLoc(unsigned Index) const {
313 assert(Index < getNumSelectorLocs() && "Index out of range!");
314 if (hasStandardSelLocs())
315 return getStandardSelectorLoc(Index, getSelector(),
316 getSelLocsKind() == SelLoc_StandardWithSpace,
317 llvm::makeArrayRef(const_cast<ParmVarDecl**>(getParams()),
320 return getStoredSelLocs()[Index];
323 void getSelectorLocs(SmallVectorImpl<SourceLocation> &SelLocs) const;
325 unsigned getNumSelectorLocs() const {
328 Selector Sel = getSelector();
329 if (Sel.isUnarySelector())
331 return Sel.getNumArgs();
334 ObjCInterfaceDecl *getClassInterface();
335 const ObjCInterfaceDecl *getClassInterface() const {
336 return const_cast<ObjCMethodDecl*>(this)->getClassInterface();
339 Selector getSelector() const { return getDeclName().getObjCSelector(); }
341 QualType getResultType() const { return MethodDeclType; }
342 void setResultType(QualType T) { MethodDeclType = T; }
344 /// \brief Determine the type of an expression that sends a message to this
346 QualType getSendResultType() const {
347 return getResultType().getNonLValueExprType(getASTContext());
350 TypeSourceInfo *getResultTypeSourceInfo() const { return ResultTInfo; }
351 void setResultTypeSourceInfo(TypeSourceInfo *TInfo) { ResultTInfo = TInfo; }
353 // Iterator access to formal parameters.
354 unsigned param_size() const { return NumParams; }
355 typedef const ParmVarDecl *const *param_const_iterator;
356 typedef ParmVarDecl *const *param_iterator;
357 param_const_iterator param_begin() const { return getParams(); }
358 param_const_iterator param_end() const { return getParams() + NumParams; }
359 param_iterator param_begin() { return getParams(); }
360 param_iterator param_end() { return getParams() + NumParams; }
361 // This method returns and of the parameters which are part of the selector
362 // name mangling requirements.
363 param_const_iterator sel_param_end() const {
364 return param_begin() + getSelector().getNumArgs();
367 /// \brief Sets the method's parameters and selector source locations.
368 /// If the method is implicit (not coming from source) \p SelLocs is
370 void setMethodParams(ASTContext &C,
371 ArrayRef<ParmVarDecl*> Params,
372 ArrayRef<SourceLocation> SelLocs =
373 ArrayRef<SourceLocation>());
375 // Iterator access to parameter types.
376 typedef std::const_mem_fun_t<QualType, ParmVarDecl> deref_fun;
377 typedef llvm::mapped_iterator<param_const_iterator, deref_fun>
380 arg_type_iterator arg_type_begin() const {
381 return llvm::map_iterator(param_begin(), deref_fun(&ParmVarDecl::getType));
383 arg_type_iterator arg_type_end() const {
384 return llvm::map_iterator(param_end(), deref_fun(&ParmVarDecl::getType));
387 /// createImplicitParams - Used to lazily create the self and cmd
388 /// implict parameters. This must be called prior to using getSelfDecl()
389 /// or getCmdDecl(). The call is ignored if the implicit paramters
390 /// have already been created.
391 void createImplicitParams(ASTContext &Context, const ObjCInterfaceDecl *ID);
393 ImplicitParamDecl * getSelfDecl() const { return SelfDecl; }
394 void setSelfDecl(ImplicitParamDecl *SD) { SelfDecl = SD; }
395 ImplicitParamDecl * getCmdDecl() const { return CmdDecl; }
396 void setCmdDecl(ImplicitParamDecl *CD) { CmdDecl = CD; }
398 /// Determines the family of this method.
399 ObjCMethodFamily getMethodFamily() const;
401 bool isInstanceMethod() const { return IsInstance; }
402 void setInstanceMethod(bool isInst) { IsInstance = isInst; }
403 bool isVariadic() const { return IsVariadic; }
404 void setVariadic(bool isVar) { IsVariadic = isVar; }
406 bool isClassMethod() const { return !IsInstance; }
408 bool isPropertyAccessor() const { return IsPropertyAccessor; }
409 void setPropertyAccessor(bool isAccessor) { IsPropertyAccessor = isAccessor; }
411 bool isDefined() const { return IsDefined; }
412 void setDefined(bool isDefined) { IsDefined = isDefined; }
414 /// \brief Whether this method overrides any other in the class hierarchy.
416 /// A method is said to override any method in the class's
417 /// base classes, its protocols, or its categories' protocols, that has
418 /// the same selector and is of the same kind (class or instance).
419 /// A method in an implementation is not considered as overriding the same
420 /// method in the interface or its categories.
421 bool isOverriding() const { return IsOverriding; }
422 void setOverriding(bool isOverriding) { IsOverriding = isOverriding; }
424 /// \brief Return overridden methods for the given \p Method.
426 /// An ObjC method is considered to override any method in the class's
427 /// base classes (and base's categories), its protocols, or its categories'
428 /// protocols, that has
429 /// the same selector and is of the same kind (class or instance).
430 /// A method in an implementation is not considered as overriding the same
431 /// method in the interface or its categories.
432 void getOverriddenMethods(
433 SmallVectorImpl<const ObjCMethodDecl *> &Overridden) const;
435 /// \brief True if the method was a definition but its body was skipped.
436 bool hasSkippedBody() const { return HasSkippedBody; }
437 void setHasSkippedBody(bool Skipped = true) { HasSkippedBody = Skipped; }
439 /// \brief Returns the property associated with this method's selector.
441 /// Note that even if this particular method is not marked as a property
442 /// accessor, it is still possible for it to match a property declared in a
443 /// superclass. Pass \c false if you only want to check the current class.
444 const ObjCPropertyDecl *findPropertyDecl(bool CheckOverrides = true) const;
446 // Related to protocols declared in \@protocol
447 void setDeclImplementation(ImplementationControl ic) {
448 DeclImplementation = ic;
450 ImplementationControl getImplementationControl() const {
451 return ImplementationControl(DeclImplementation);
454 /// \brief Determine whether this method has a body.
455 virtual bool hasBody() const { return Body; }
457 /// \brief Retrieve the body of this method, if it has one.
458 virtual Stmt *getBody() const;
460 void setLazyBody(uint64_t Offset) { Body = Offset; }
462 CompoundStmt *getCompoundBody() { return (CompoundStmt*)getBody(); }
463 void setBody(Stmt *B) { Body = B; }
465 /// \brief Returns whether this specific method is a definition.
466 bool isThisDeclarationADefinition() const { return Body; }
468 // Implement isa/cast/dyncast/etc.
469 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
470 static bool classofKind(Kind K) { return K == ObjCMethod; }
471 static DeclContext *castToDeclContext(const ObjCMethodDecl *D) {
472 return static_cast<DeclContext *>(const_cast<ObjCMethodDecl*>(D));
474 static ObjCMethodDecl *castFromDeclContext(const DeclContext *DC) {
475 return static_cast<ObjCMethodDecl *>(const_cast<DeclContext*>(DC));
478 friend class ASTDeclReader;
479 friend class ASTDeclWriter;
482 /// ObjCContainerDecl - Represents a container for method declarations.
483 /// Current sub-classes are ObjCInterfaceDecl, ObjCCategoryDecl,
484 /// ObjCProtocolDecl, and ObjCImplDecl.
486 class ObjCContainerDecl : public NamedDecl, public DeclContext {
487 virtual void anchor();
489 SourceLocation AtStart;
491 // These two locations in the range mark the end of the method container.
492 // The first points to the '@' token, and the second to the 'end' token.
496 ObjCContainerDecl(Kind DK, DeclContext *DC,
497 IdentifierInfo *Id, SourceLocation nameLoc,
498 SourceLocation atStartLoc)
499 : NamedDecl(DK, DC, nameLoc, Id), DeclContext(DK), AtStart(atStartLoc) {}
501 // Iterator access to properties.
502 typedef specific_decl_iterator<ObjCPropertyDecl> prop_iterator;
503 prop_iterator prop_begin() const {
504 return prop_iterator(decls_begin());
506 prop_iterator prop_end() const {
507 return prop_iterator(decls_end());
510 // Iterator access to instance/class methods.
511 typedef specific_decl_iterator<ObjCMethodDecl> method_iterator;
512 method_iterator meth_begin() const {
513 return method_iterator(decls_begin());
515 method_iterator meth_end() const {
516 return method_iterator(decls_end());
519 typedef filtered_decl_iterator<ObjCMethodDecl,
520 &ObjCMethodDecl::isInstanceMethod>
522 instmeth_iterator instmeth_begin() const {
523 return instmeth_iterator(decls_begin());
525 instmeth_iterator instmeth_end() const {
526 return instmeth_iterator(decls_end());
529 typedef filtered_decl_iterator<ObjCMethodDecl,
530 &ObjCMethodDecl::isClassMethod>
532 classmeth_iterator classmeth_begin() const {
533 return classmeth_iterator(decls_begin());
535 classmeth_iterator classmeth_end() const {
536 return classmeth_iterator(decls_end());
539 // Get the local instance/class method declared in this interface.
540 ObjCMethodDecl *getMethod(Selector Sel, bool isInstance,
541 bool AllowHidden = false) const;
542 ObjCMethodDecl *getInstanceMethod(Selector Sel,
543 bool AllowHidden = false) const {
544 return getMethod(Sel, true/*isInstance*/, AllowHidden);
546 ObjCMethodDecl *getClassMethod(Selector Sel, bool AllowHidden = false) const {
547 return getMethod(Sel, false/*isInstance*/, AllowHidden);
549 bool HasUserDeclaredSetterMethod(const ObjCPropertyDecl *P) const;
550 ObjCIvarDecl *getIvarDecl(IdentifierInfo *Id) const;
552 ObjCPropertyDecl *FindPropertyDeclaration(IdentifierInfo *PropertyId) const;
554 typedef llvm::DenseMap<IdentifierInfo*, ObjCPropertyDecl*> PropertyMap;
556 typedef llvm::SmallVector<ObjCPropertyDecl*, 8> PropertyDeclOrder;
558 /// This routine collects list of properties to be implemented in the class.
559 /// This includes, class's and its conforming protocols' properties.
560 /// Note, the superclass's properties are not included in the list.
561 virtual void collectPropertiesToImplement(PropertyMap &PM,
562 PropertyDeclOrder &PO) const {}
564 SourceLocation getAtStartLoc() const { return AtStart; }
565 void setAtStartLoc(SourceLocation Loc) { AtStart = Loc; }
567 // Marks the end of the container.
568 SourceRange getAtEndRange() const {
571 void setAtEndRange(SourceRange atEnd) {
575 virtual SourceRange getSourceRange() const LLVM_READONLY {
576 return SourceRange(AtStart, getAtEndRange().getEnd());
579 // Implement isa/cast/dyncast/etc.
580 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
581 static bool classofKind(Kind K) {
582 return K >= firstObjCContainer &&
583 K <= lastObjCContainer;
586 static DeclContext *castToDeclContext(const ObjCContainerDecl *D) {
587 return static_cast<DeclContext *>(const_cast<ObjCContainerDecl*>(D));
589 static ObjCContainerDecl *castFromDeclContext(const DeclContext *DC) {
590 return static_cast<ObjCContainerDecl *>(const_cast<DeclContext*>(DC));
594 /// \brief Represents an ObjC class declaration.
599 /// // MostPrimitive declares no super class (not particularly useful).
600 /// \@interface MostPrimitive
601 /// // no instance variables or methods.
604 /// // NSResponder inherits from NSObject & implements NSCoding (a protocol).
605 /// \@interface NSResponder : NSObject \<NSCoding>
606 /// { // instance variables are represented by ObjCIvarDecl.
607 /// id nextResponder; // nextResponder instance variable.
609 /// - (NSResponder *)nextResponder; // return a pointer to NSResponder.
610 /// - (void)mouseMoved:(NSEvent *)theEvent; // return void, takes a pointer
611 /// \@end // to an NSEvent.
614 /// Unlike C/C++, forward class declarations are accomplished with \@class.
615 /// Unlike C/C++, \@class allows for a list of classes to be forward declared.
616 /// Unlike C++, ObjC is a single-rooted class model. In Cocoa, classes
617 /// typically inherit from NSObject (an exception is NSProxy).
619 class ObjCInterfaceDecl : public ObjCContainerDecl
620 , public Redeclarable<ObjCInterfaceDecl> {
621 virtual void anchor();
623 /// TypeForDecl - This indicates the Type object that represents this
624 /// TypeDecl. It is a cache maintained by ASTContext::getObjCInterfaceType
625 mutable const Type *TypeForDecl;
626 friend class ASTContext;
628 struct DefinitionData {
629 /// \brief The definition of this class, for quick access from any
631 ObjCInterfaceDecl *Definition;
633 /// Class's super class.
634 ObjCInterfaceDecl *SuperClass;
636 /// Protocols referenced in the \@interface declaration
637 ObjCProtocolList ReferencedProtocols;
639 /// Protocols reference in both the \@interface and class extensions.
640 ObjCList<ObjCProtocolDecl> AllReferencedProtocols;
642 /// \brief List of categories and class extensions defined for this class.
644 /// Categories are stored as a linked list in the AST, since the categories
645 /// and class extensions come long after the initial interface declaration,
646 /// and we avoid dynamically-resized arrays in the AST wherever possible.
647 ObjCCategoryDecl *CategoryList;
649 /// IvarList - List of all ivars defined by this class; including class
650 /// extensions and implementation. This list is built lazily.
651 ObjCIvarDecl *IvarList;
653 /// \brief Indicates that the contents of this Objective-C class will be
654 /// completed by the external AST source when required.
655 mutable bool ExternallyCompleted : 1;
657 /// \brief Indicates that the ivar cache does not yet include ivars
658 /// declared in the implementation.
659 mutable bool IvarListMissingImplementation : 1;
661 /// \brief The location of the superclass, if any.
662 SourceLocation SuperClassLoc;
664 /// \brief The location of the last location in this declaration, before
665 /// the properties/methods. For example, this will be the '>', '}', or
667 SourceLocation EndLoc;
669 DefinitionData() : Definition(), SuperClass(), CategoryList(), IvarList(),
670 ExternallyCompleted(),
671 IvarListMissingImplementation(true) { }
674 ObjCInterfaceDecl(DeclContext *DC, SourceLocation atLoc, IdentifierInfo *Id,
675 SourceLocation CLoc, ObjCInterfaceDecl *PrevDecl,
678 void LoadExternalDefinition() const;
680 /// \brief Contains a pointer to the data associated with this class,
681 /// which will be NULL if this class has not yet been defined.
683 /// The bit indicates when we don't need to check for out-of-date
684 /// declarations. It will be set unless modules are enabled.
685 llvm::PointerIntPair<DefinitionData *, 1, bool> Data;
687 DefinitionData &data() const {
688 assert(Data.getPointer() && "Declaration has no definition!");
689 return *Data.getPointer();
692 /// \brief Allocate the definition data for this class.
693 void allocateDefinitionData();
695 typedef Redeclarable<ObjCInterfaceDecl> redeclarable_base;
696 virtual ObjCInterfaceDecl *getNextRedeclaration() {
697 return RedeclLink.getNext();
699 virtual ObjCInterfaceDecl *getPreviousDeclImpl() {
700 return getPreviousDecl();
702 virtual ObjCInterfaceDecl *getMostRecentDeclImpl() {
703 return getMostRecentDecl();
707 static ObjCInterfaceDecl *Create(const ASTContext &C, DeclContext *DC,
708 SourceLocation atLoc,
710 ObjCInterfaceDecl *PrevDecl,
711 SourceLocation ClassLoc = SourceLocation(),
712 bool isInternal = false);
714 static ObjCInterfaceDecl *CreateDeserialized(ASTContext &C, unsigned ID);
716 virtual SourceRange getSourceRange() const LLVM_READONLY {
717 if (isThisDeclarationADefinition())
718 return ObjCContainerDecl::getSourceRange();
720 return SourceRange(getAtStartLoc(), getLocation());
723 /// \brief Indicate that this Objective-C class is complete, but that
724 /// the external AST source will be responsible for filling in its contents
725 /// when a complete class is required.
726 void setExternallyCompleted();
728 const ObjCProtocolList &getReferencedProtocols() const {
729 assert(hasDefinition() && "Caller did not check for forward reference!");
730 if (data().ExternallyCompleted)
731 LoadExternalDefinition();
733 return data().ReferencedProtocols;
736 ObjCImplementationDecl *getImplementation() const;
737 void setImplementation(ObjCImplementationDecl *ImplD);
739 ObjCCategoryDecl *FindCategoryDeclaration(IdentifierInfo *CategoryId) const;
741 // Get the local instance/class method declared in a category.
742 ObjCMethodDecl *getCategoryInstanceMethod(Selector Sel) const;
743 ObjCMethodDecl *getCategoryClassMethod(Selector Sel) const;
744 ObjCMethodDecl *getCategoryMethod(Selector Sel, bool isInstance) const {
745 return isInstance ? getInstanceMethod(Sel)
746 : getClassMethod(Sel);
749 typedef ObjCProtocolList::iterator protocol_iterator;
751 protocol_iterator protocol_begin() const {
752 // FIXME: Should make sure no callers ever do this.
753 if (!hasDefinition())
754 return protocol_iterator();
756 if (data().ExternallyCompleted)
757 LoadExternalDefinition();
759 return data().ReferencedProtocols.begin();
761 protocol_iterator protocol_end() const {
762 // FIXME: Should make sure no callers ever do this.
763 if (!hasDefinition())
764 return protocol_iterator();
766 if (data().ExternallyCompleted)
767 LoadExternalDefinition();
769 return data().ReferencedProtocols.end();
772 typedef ObjCProtocolList::loc_iterator protocol_loc_iterator;
774 protocol_loc_iterator protocol_loc_begin() const {
775 // FIXME: Should make sure no callers ever do this.
776 if (!hasDefinition())
777 return protocol_loc_iterator();
779 if (data().ExternallyCompleted)
780 LoadExternalDefinition();
782 return data().ReferencedProtocols.loc_begin();
785 protocol_loc_iterator protocol_loc_end() const {
786 // FIXME: Should make sure no callers ever do this.
787 if (!hasDefinition())
788 return protocol_loc_iterator();
790 if (data().ExternallyCompleted)
791 LoadExternalDefinition();
793 return data().ReferencedProtocols.loc_end();
796 typedef ObjCList<ObjCProtocolDecl>::iterator all_protocol_iterator;
798 all_protocol_iterator all_referenced_protocol_begin() const {
799 // FIXME: Should make sure no callers ever do this.
800 if (!hasDefinition())
801 return all_protocol_iterator();
803 if (data().ExternallyCompleted)
804 LoadExternalDefinition();
806 return data().AllReferencedProtocols.empty()
808 : data().AllReferencedProtocols.begin();
810 all_protocol_iterator all_referenced_protocol_end() const {
811 // FIXME: Should make sure no callers ever do this.
812 if (!hasDefinition())
813 return all_protocol_iterator();
815 if (data().ExternallyCompleted)
816 LoadExternalDefinition();
818 return data().AllReferencedProtocols.empty()
820 : data().AllReferencedProtocols.end();
823 typedef specific_decl_iterator<ObjCIvarDecl> ivar_iterator;
825 ivar_iterator ivar_begin() const {
826 if (const ObjCInterfaceDecl *Def = getDefinition())
827 return ivar_iterator(Def->decls_begin());
829 // FIXME: Should make sure no callers ever do this.
830 return ivar_iterator();
832 ivar_iterator ivar_end() const {
833 if (const ObjCInterfaceDecl *Def = getDefinition())
834 return ivar_iterator(Def->decls_end());
836 // FIXME: Should make sure no callers ever do this.
837 return ivar_iterator();
840 unsigned ivar_size() const {
841 return std::distance(ivar_begin(), ivar_end());
844 bool ivar_empty() const { return ivar_begin() == ivar_end(); }
846 ObjCIvarDecl *all_declared_ivar_begin();
847 const ObjCIvarDecl *all_declared_ivar_begin() const {
848 // Even though this modifies IvarList, it's conceptually const:
849 // the ivar chain is essentially a cached property of ObjCInterfaceDecl.
850 return const_cast<ObjCInterfaceDecl *>(this)->all_declared_ivar_begin();
852 void setIvarList(ObjCIvarDecl *ivar) { data().IvarList = ivar; }
854 /// setProtocolList - Set the list of protocols that this interface
856 void setProtocolList(ObjCProtocolDecl *const* List, unsigned Num,
857 const SourceLocation *Locs, ASTContext &C) {
858 data().ReferencedProtocols.set(List, Num, Locs, C);
861 /// mergeClassExtensionProtocolList - Merge class extension's protocol list
862 /// into the protocol list for this class.
863 void mergeClassExtensionProtocolList(ObjCProtocolDecl *const* List,
867 /// \brief Determine whether this particular declaration of this class is
868 /// actually also a definition.
869 bool isThisDeclarationADefinition() const {
870 return getDefinition() == this;
873 /// \brief Determine whether this class has been defined.
874 bool hasDefinition() const {
875 // If the name of this class is out-of-date, bring it up-to-date, which
876 // might bring in a definition.
877 // Note: a null value indicates that we don't have a definition and that
878 // modules are enabled.
879 if (!Data.getOpaqueValue()) {
880 if (IdentifierInfo *II = getIdentifier()) {
881 if (II->isOutOfDate()) {
882 updateOutOfDate(*II);
887 return Data.getPointer();
890 /// \brief Retrieve the definition of this class, or NULL if this class
891 /// has been forward-declared (with \@class) but not yet defined (with
893 ObjCInterfaceDecl *getDefinition() {
894 return hasDefinition()? Data.getPointer()->Definition : 0;
897 /// \brief Retrieve the definition of this class, or NULL if this class
898 /// has been forward-declared (with \@class) but not yet defined (with
900 const ObjCInterfaceDecl *getDefinition() const {
901 return hasDefinition()? Data.getPointer()->Definition : 0;
904 /// \brief Starts the definition of this Objective-C class, taking it from
905 /// a forward declaration (\@class) to a definition (\@interface).
906 void startDefinition();
908 ObjCInterfaceDecl *getSuperClass() const {
909 // FIXME: Should make sure no callers ever do this.
910 if (!hasDefinition())
913 if (data().ExternallyCompleted)
914 LoadExternalDefinition();
916 return data().SuperClass;
919 void setSuperClass(ObjCInterfaceDecl * superCls) {
921 (superCls && superCls->hasDefinition()) ? superCls->getDefinition()
925 /// \brief Iterator that walks over the list of categories, filtering out
926 /// those that do not meet specific criteria.
928 /// This class template is used for the various permutations of category
929 /// and extension iterators.
930 template<bool (*Filter)(ObjCCategoryDecl *)>
931 class filtered_category_iterator {
932 ObjCCategoryDecl *Current;
934 void findAcceptableCategory();
937 typedef ObjCCategoryDecl * value_type;
938 typedef value_type reference;
939 typedef value_type pointer;
940 typedef std::ptrdiff_t difference_type;
941 typedef std::input_iterator_tag iterator_category;
943 filtered_category_iterator() : Current(0) { }
944 explicit filtered_category_iterator(ObjCCategoryDecl *Current)
947 findAcceptableCategory();
950 reference operator*() const { return Current; }
951 pointer operator->() const { return Current; }
953 filtered_category_iterator &operator++();
955 filtered_category_iterator operator++(int) {
956 filtered_category_iterator Tmp = *this;
961 friend bool operator==(filtered_category_iterator X,
962 filtered_category_iterator Y) {
963 return X.Current == Y.Current;
966 friend bool operator!=(filtered_category_iterator X,
967 filtered_category_iterator Y) {
968 return X.Current != Y.Current;
973 /// \brief Test whether the given category is visible.
975 /// Used in the \c visible_categories_iterator.
976 static bool isVisibleCategory(ObjCCategoryDecl *Cat);
979 /// \brief Iterator that walks over the list of categories and extensions
980 /// that are visible, i.e., not hidden in a non-imported submodule.
981 typedef filtered_category_iterator<isVisibleCategory>
982 visible_categories_iterator;
984 /// \brief Retrieve an iterator to the beginning of the visible-categories
986 visible_categories_iterator visible_categories_begin() const {
987 return visible_categories_iterator(getCategoryListRaw());
990 /// \brief Retrieve an iterator to the end of the visible-categories list.
991 visible_categories_iterator visible_categories_end() const {
992 return visible_categories_iterator();
995 /// \brief Determine whether the visible-categories list is empty.
996 bool visible_categories_empty() const {
997 return visible_categories_begin() == visible_categories_end();
1001 /// \brief Test whether the given category... is a category.
1003 /// Used in the \c known_categories_iterator.
1004 static bool isKnownCategory(ObjCCategoryDecl *) { return true; }
1007 /// \brief Iterator that walks over all of the known categories and
1008 /// extensions, including those that are hidden.
1009 typedef filtered_category_iterator<isKnownCategory> known_categories_iterator;
1011 /// \brief Retrieve an iterator to the beginning of the known-categories
1013 known_categories_iterator known_categories_begin() const {
1014 return known_categories_iterator(getCategoryListRaw());
1017 /// \brief Retrieve an iterator to the end of the known-categories list.
1018 known_categories_iterator known_categories_end() const {
1019 return known_categories_iterator();
1022 /// \brief Determine whether the known-categories list is empty.
1023 bool known_categories_empty() const {
1024 return known_categories_begin() == known_categories_end();
1028 /// \brief Test whether the given category is a visible extension.
1030 /// Used in the \c visible_extensions_iterator.
1031 static bool isVisibleExtension(ObjCCategoryDecl *Cat);
1034 /// \brief Iterator that walks over all of the visible extensions, skipping
1035 /// any that are known but hidden.
1036 typedef filtered_category_iterator<isVisibleExtension>
1037 visible_extensions_iterator;
1039 /// \brief Retrieve an iterator to the beginning of the visible-extensions
1041 visible_extensions_iterator visible_extensions_begin() const {
1042 return visible_extensions_iterator(getCategoryListRaw());
1045 /// \brief Retrieve an iterator to the end of the visible-extensions list.
1046 visible_extensions_iterator visible_extensions_end() const {
1047 return visible_extensions_iterator();
1050 /// \brief Determine whether the visible-extensions list is empty.
1051 bool visible_extensions_empty() const {
1052 return visible_extensions_begin() == visible_extensions_end();
1056 /// \brief Test whether the given category is an extension.
1058 /// Used in the \c known_extensions_iterator.
1059 static bool isKnownExtension(ObjCCategoryDecl *Cat);
1062 /// \brief Iterator that walks over all of the known extensions.
1063 typedef filtered_category_iterator<isKnownExtension>
1064 known_extensions_iterator;
1066 /// \brief Retrieve an iterator to the beginning of the known-extensions
1068 known_extensions_iterator known_extensions_begin() const {
1069 return known_extensions_iterator(getCategoryListRaw());
1072 /// \brief Retrieve an iterator to the end of the known-extensions list.
1073 known_extensions_iterator known_extensions_end() const {
1074 return known_extensions_iterator();
1077 /// \brief Determine whether the known-extensions list is empty.
1078 bool known_extensions_empty() const {
1079 return known_extensions_begin() == known_extensions_end();
1082 /// \brief Retrieve the raw pointer to the start of the category/extension
1084 ObjCCategoryDecl* getCategoryListRaw() const {
1085 // FIXME: Should make sure no callers ever do this.
1086 if (!hasDefinition())
1089 if (data().ExternallyCompleted)
1090 LoadExternalDefinition();
1092 return data().CategoryList;
1095 /// \brief Set the raw pointer to the start of the category/extension
1097 void setCategoryListRaw(ObjCCategoryDecl *category) {
1098 data().CategoryList = category;
1102 *FindPropertyVisibleInPrimaryClass(IdentifierInfo *PropertyId) const;
1104 virtual void collectPropertiesToImplement(PropertyMap &PM,
1105 PropertyDeclOrder &PO) const;
1107 /// isSuperClassOf - Return true if this class is the specified class or is a
1108 /// super class of the specified interface class.
1109 bool isSuperClassOf(const ObjCInterfaceDecl *I) const {
1110 // If RHS is derived from LHS it is OK; else it is not OK.
1112 if (declaresSameEntity(this, I))
1115 I = I->getSuperClass();
1120 /// isArcWeakrefUnavailable - Checks for a class or one of its super classes
1121 /// to be incompatible with __weak references. Returns true if it is.
1122 bool isArcWeakrefUnavailable() const;
1124 /// isObjCRequiresPropertyDefs - Checks that a class or one of its super
1125 /// classes must not be auto-synthesized. Returns class decl. if it must not
1126 /// be; 0, otherwise.
1127 const ObjCInterfaceDecl *isObjCRequiresPropertyDefs() const;
1129 ObjCIvarDecl *lookupInstanceVariable(IdentifierInfo *IVarName,
1130 ObjCInterfaceDecl *&ClassDeclared);
1131 ObjCIvarDecl *lookupInstanceVariable(IdentifierInfo *IVarName) {
1132 ObjCInterfaceDecl *ClassDeclared;
1133 return lookupInstanceVariable(IVarName, ClassDeclared);
1136 // Lookup a method. First, we search locally. If a method isn't
1137 // found, we search referenced protocols and class categories.
1138 ObjCMethodDecl *lookupMethod(Selector Sel, bool isInstance,
1139 bool shallowCategoryLookup= false,
1140 const ObjCCategoryDecl *C= 0) const;
1141 ObjCMethodDecl *lookupInstanceMethod(Selector Sel,
1142 bool shallowCategoryLookup = false) const {
1143 return lookupMethod(Sel, true/*isInstance*/, shallowCategoryLookup);
1145 ObjCMethodDecl *lookupClassMethod(Selector Sel,
1146 bool shallowCategoryLookup = false) const {
1147 return lookupMethod(Sel, false/*isInstance*/, shallowCategoryLookup);
1149 ObjCInterfaceDecl *lookupInheritedClass(const IdentifierInfo *ICName);
1151 /// \brief Lookup a method in the classes implementation hierarchy.
1152 ObjCMethodDecl *lookupPrivateMethod(const Selector &Sel,
1153 bool Instance=true) const;
1155 ObjCMethodDecl *lookupPrivateClassMethod(const Selector &Sel) {
1156 return lookupPrivateMethod(Sel, false);
1159 /// \brief Lookup a setter or getter in the class hierarchy,
1160 /// including in all categories except for category passed
1162 ObjCMethodDecl *lookupPropertyAccessor(const Selector Sel,
1163 const ObjCCategoryDecl *Cat) const {
1164 return lookupMethod(Sel, true/*isInstance*/,
1165 false/*shallowCategoryLookup*/, Cat);
1168 SourceLocation getEndOfDefinitionLoc() const {
1169 if (!hasDefinition())
1170 return getLocation();
1172 return data().EndLoc;
1175 void setEndOfDefinitionLoc(SourceLocation LE) { data().EndLoc = LE; }
1177 void setSuperClassLoc(SourceLocation Loc) { data().SuperClassLoc = Loc; }
1178 SourceLocation getSuperClassLoc() const { return data().SuperClassLoc; }
1180 /// isImplicitInterfaceDecl - check that this is an implicitly declared
1181 /// ObjCInterfaceDecl node. This is for legacy objective-c \@implementation
1182 /// declaration without an \@interface declaration.
1183 bool isImplicitInterfaceDecl() const {
1184 return hasDefinition() ? data().Definition->isImplicit() : isImplicit();
1187 /// ClassImplementsProtocol - Checks that 'lProto' protocol
1188 /// has been implemented in IDecl class, its super class or categories (if
1189 /// lookupCategory is true).
1190 bool ClassImplementsProtocol(ObjCProtocolDecl *lProto,
1191 bool lookupCategory,
1192 bool RHSIsQualifiedID = false);
1194 typedef redeclarable_base::redecl_iterator redecl_iterator;
1195 using redeclarable_base::redecls_begin;
1196 using redeclarable_base::redecls_end;
1197 using redeclarable_base::getPreviousDecl;
1198 using redeclarable_base::getMostRecentDecl;
1200 /// Retrieves the canonical declaration of this Objective-C class.
1201 ObjCInterfaceDecl *getCanonicalDecl() {
1202 return getFirstDeclaration();
1204 const ObjCInterfaceDecl *getCanonicalDecl() const {
1205 return getFirstDeclaration();
1208 // Low-level accessor
1209 const Type *getTypeForDecl() const { return TypeForDecl; }
1210 void setTypeForDecl(const Type *TD) const { TypeForDecl = TD; }
1212 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1213 static bool classofKind(Kind K) { return K == ObjCInterface; }
1215 friend class ASTReader;
1216 friend class ASTDeclReader;
1217 friend class ASTDeclWriter;
1220 /// ObjCIvarDecl - Represents an ObjC instance variable. In general, ObjC
1221 /// instance variables are identical to C. The only exception is Objective-C
1222 /// supports C++ style access control. For example:
1224 /// \@interface IvarExample : NSObject
1226 /// id defaultToProtected;
1228 /// id canBePublic; // same as C++.
1230 /// id canBeProtected; // same as C++.
1232 /// id canBePackage; // framework visibility (not available in C++).
1235 class ObjCIvarDecl : public FieldDecl {
1236 virtual void anchor();
1239 enum AccessControl {
1240 None, Private, Protected, Public, Package
1244 ObjCIvarDecl(ObjCContainerDecl *DC, SourceLocation StartLoc,
1245 SourceLocation IdLoc, IdentifierInfo *Id,
1246 QualType T, TypeSourceInfo *TInfo, AccessControl ac, Expr *BW,
1248 : FieldDecl(ObjCIvar, DC, StartLoc, IdLoc, Id, T, TInfo, BW,
1249 /*Mutable=*/false, /*HasInit=*/ICIS_NoInit),
1250 NextIvar(0), DeclAccess(ac), Synthesized(synthesized) {}
1253 static ObjCIvarDecl *Create(ASTContext &C, ObjCContainerDecl *DC,
1254 SourceLocation StartLoc, SourceLocation IdLoc,
1255 IdentifierInfo *Id, QualType T,
1256 TypeSourceInfo *TInfo,
1257 AccessControl ac, Expr *BW = NULL,
1258 bool synthesized=false);
1260 static ObjCIvarDecl *CreateDeserialized(ASTContext &C, unsigned ID);
1262 /// \brief Return the class interface that this ivar is logically contained
1263 /// in; this is either the interface where the ivar was declared, or the
1264 /// interface the ivar is conceptually a part of in the case of synthesized
1266 const ObjCInterfaceDecl *getContainingInterface() const;
1268 ObjCIvarDecl *getNextIvar() { return NextIvar; }
1269 const ObjCIvarDecl *getNextIvar() const { return NextIvar; }
1270 void setNextIvar(ObjCIvarDecl *ivar) { NextIvar = ivar; }
1272 void setAccessControl(AccessControl ac) { DeclAccess = ac; }
1274 AccessControl getAccessControl() const { return AccessControl(DeclAccess); }
1276 AccessControl getCanonicalAccessControl() const {
1277 return DeclAccess == None ? Protected : AccessControl(DeclAccess);
1280 void setSynthesize(bool synth) { Synthesized = synth; }
1281 bool getSynthesize() const { return Synthesized; }
1283 // Implement isa/cast/dyncast/etc.
1284 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1285 static bool classofKind(Kind K) { return K == ObjCIvar; }
1287 /// NextIvar - Next Ivar in the list of ivars declared in class; class's
1288 /// extensions and class's implementation
1289 ObjCIvarDecl *NextIvar;
1291 // NOTE: VC++ treats enums as signed, avoid using the AccessControl enum
1292 unsigned DeclAccess : 3;
1293 unsigned Synthesized : 1;
1297 /// \brief Represents a field declaration created by an \@defs(...).
1298 class ObjCAtDefsFieldDecl : public FieldDecl {
1299 virtual void anchor();
1300 ObjCAtDefsFieldDecl(DeclContext *DC, SourceLocation StartLoc,
1301 SourceLocation IdLoc, IdentifierInfo *Id,
1302 QualType T, Expr *BW)
1303 : FieldDecl(ObjCAtDefsField, DC, StartLoc, IdLoc, Id, T,
1304 /*TInfo=*/0, // FIXME: Do ObjCAtDefs have declarators ?
1305 BW, /*Mutable=*/false, /*HasInit=*/ICIS_NoInit) {}
1308 static ObjCAtDefsFieldDecl *Create(ASTContext &C, DeclContext *DC,
1309 SourceLocation StartLoc,
1310 SourceLocation IdLoc, IdentifierInfo *Id,
1311 QualType T, Expr *BW);
1313 static ObjCAtDefsFieldDecl *CreateDeserialized(ASTContext &C, unsigned ID);
1315 // Implement isa/cast/dyncast/etc.
1316 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1317 static bool classofKind(Kind K) { return K == ObjCAtDefsField; }
1320 /// \brief Represents an Objective-C protocol declaration.
1322 /// Objective-C protocols declare a pure abstract type (i.e., no instance
1323 /// variables are permitted). Protocols originally drew inspiration from
1324 /// C++ pure virtual functions (a C++ feature with nice semantics and lousy
1325 /// syntax:-). Here is an example:
1328 /// \@protocol NSDraggingInfo <refproto1, refproto2>
1329 /// - (NSWindow *)draggingDestinationWindow;
1330 /// - (NSImage *)draggedImage;
1334 /// This says that NSDraggingInfo requires two methods and requires everything
1335 /// that the two "referenced protocols" 'refproto1' and 'refproto2' require as
1339 /// \@interface ImplementsNSDraggingInfo : NSObject \<NSDraggingInfo>
1343 /// ObjC protocols inspired Java interfaces. Unlike Java, ObjC classes and
1344 /// protocols are in distinct namespaces. For example, Cocoa defines both
1345 /// an NSObject protocol and class (which isn't allowed in Java). As a result,
1346 /// protocols are referenced using angle brackets as follows:
1348 /// id \<NSDraggingInfo> anyObjectThatImplementsNSDraggingInfo;
1350 class ObjCProtocolDecl : public ObjCContainerDecl,
1351 public Redeclarable<ObjCProtocolDecl> {
1352 virtual void anchor();
1354 struct DefinitionData {
1355 // \brief The declaration that defines this protocol.
1356 ObjCProtocolDecl *Definition;
1358 /// \brief Referenced protocols
1359 ObjCProtocolList ReferencedProtocols;
1362 /// \brief Contains a pointer to the data associated with this class,
1363 /// which will be NULL if this class has not yet been defined.
1365 /// The bit indicates when we don't need to check for out-of-date
1366 /// declarations. It will be set unless modules are enabled.
1367 llvm::PointerIntPair<DefinitionData *, 1, bool> Data;
1369 DefinitionData &data() const {
1370 assert(Data.getPointer() && "Objective-C protocol has no definition!");
1371 return *Data.getPointer();
1374 ObjCProtocolDecl(DeclContext *DC, IdentifierInfo *Id,
1375 SourceLocation nameLoc, SourceLocation atStartLoc,
1376 ObjCProtocolDecl *PrevDecl);
1378 void allocateDefinitionData();
1380 typedef Redeclarable<ObjCProtocolDecl> redeclarable_base;
1381 virtual ObjCProtocolDecl *getNextRedeclaration() {
1382 return RedeclLink.getNext();
1384 virtual ObjCProtocolDecl *getPreviousDeclImpl() {
1385 return getPreviousDecl();
1387 virtual ObjCProtocolDecl *getMostRecentDeclImpl() {
1388 return getMostRecentDecl();
1392 static ObjCProtocolDecl *Create(ASTContext &C, DeclContext *DC,
1394 SourceLocation nameLoc,
1395 SourceLocation atStartLoc,
1396 ObjCProtocolDecl *PrevDecl);
1398 static ObjCProtocolDecl *CreateDeserialized(ASTContext &C, unsigned ID);
1400 const ObjCProtocolList &getReferencedProtocols() const {
1401 assert(hasDefinition() && "No definition available!");
1402 return data().ReferencedProtocols;
1404 typedef ObjCProtocolList::iterator protocol_iterator;
1405 protocol_iterator protocol_begin() const {
1406 if (!hasDefinition())
1407 return protocol_iterator();
1409 return data().ReferencedProtocols.begin();
1411 protocol_iterator protocol_end() const {
1412 if (!hasDefinition())
1413 return protocol_iterator();
1415 return data().ReferencedProtocols.end();
1417 typedef ObjCProtocolList::loc_iterator protocol_loc_iterator;
1418 protocol_loc_iterator protocol_loc_begin() const {
1419 if (!hasDefinition())
1420 return protocol_loc_iterator();
1422 return data().ReferencedProtocols.loc_begin();
1424 protocol_loc_iterator protocol_loc_end() const {
1425 if (!hasDefinition())
1426 return protocol_loc_iterator();
1428 return data().ReferencedProtocols.loc_end();
1430 unsigned protocol_size() const {
1431 if (!hasDefinition())
1434 return data().ReferencedProtocols.size();
1437 /// setProtocolList - Set the list of protocols that this interface
1439 void setProtocolList(ObjCProtocolDecl *const*List, unsigned Num,
1440 const SourceLocation *Locs, ASTContext &C) {
1441 assert(hasDefinition() && "Protocol is not defined");
1442 data().ReferencedProtocols.set(List, Num, Locs, C);
1445 ObjCProtocolDecl *lookupProtocolNamed(IdentifierInfo *PName);
1447 // Lookup a method. First, we search locally. If a method isn't
1448 // found, we search referenced protocols and class categories.
1449 ObjCMethodDecl *lookupMethod(Selector Sel, bool isInstance) const;
1450 ObjCMethodDecl *lookupInstanceMethod(Selector Sel) const {
1451 return lookupMethod(Sel, true/*isInstance*/);
1453 ObjCMethodDecl *lookupClassMethod(Selector Sel) const {
1454 return lookupMethod(Sel, false/*isInstance*/);
1457 /// \brief Determine whether this protocol has a definition.
1458 bool hasDefinition() const {
1459 // If the name of this protocol is out-of-date, bring it up-to-date, which
1460 // might bring in a definition.
1461 // Note: a null value indicates that we don't have a definition and that
1462 // modules are enabled.
1463 if (!Data.getOpaqueValue()) {
1464 if (IdentifierInfo *II = getIdentifier()) {
1465 if (II->isOutOfDate()) {
1466 updateOutOfDate(*II);
1471 return Data.getPointer();
1474 /// \brief Retrieve the definition of this protocol, if any.
1475 ObjCProtocolDecl *getDefinition() {
1476 return hasDefinition()? Data.getPointer()->Definition : 0;
1479 /// \brief Retrieve the definition of this protocol, if any.
1480 const ObjCProtocolDecl *getDefinition() const {
1481 return hasDefinition()? Data.getPointer()->Definition : 0;
1484 /// \brief Determine whether this particular declaration is also the
1486 bool isThisDeclarationADefinition() const {
1487 return getDefinition() == this;
1490 /// \brief Starts the definition of this Objective-C protocol.
1491 void startDefinition();
1493 virtual SourceRange getSourceRange() const LLVM_READONLY {
1494 if (isThisDeclarationADefinition())
1495 return ObjCContainerDecl::getSourceRange();
1497 return SourceRange(getAtStartLoc(), getLocation());
1500 typedef redeclarable_base::redecl_iterator redecl_iterator;
1501 using redeclarable_base::redecls_begin;
1502 using redeclarable_base::redecls_end;
1503 using redeclarable_base::getPreviousDecl;
1504 using redeclarable_base::getMostRecentDecl;
1506 /// Retrieves the canonical declaration of this Objective-C protocol.
1507 ObjCProtocolDecl *getCanonicalDecl() {
1508 return getFirstDeclaration();
1510 const ObjCProtocolDecl *getCanonicalDecl() const {
1511 return getFirstDeclaration();
1514 virtual void collectPropertiesToImplement(PropertyMap &PM,
1515 PropertyDeclOrder &PO) const;
1517 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1518 static bool classofKind(Kind K) { return K == ObjCProtocol; }
1520 friend class ASTReader;
1521 friend class ASTDeclReader;
1522 friend class ASTDeclWriter;
1525 /// ObjCCategoryDecl - Represents a category declaration. A category allows
1526 /// you to add methods to an existing class (without subclassing or modifying
1527 /// the original class interface or implementation:-). Categories don't allow
1528 /// you to add instance data. The following example adds "myMethod" to all
1529 /// NSView's within a process:
1531 /// \@interface NSView (MyViewMethods)
1535 /// Categories also allow you to split the implementation of a class across
1536 /// several files (a feature more naturally supported in C++).
1538 /// Categories were originally inspired by dynamic languages such as Common
1539 /// Lisp and Smalltalk. More traditional class-based languages (C++, Java)
1540 /// don't support this level of dynamism, which is both powerful and dangerous.
1542 class ObjCCategoryDecl : public ObjCContainerDecl {
1543 virtual void anchor();
1545 /// Interface belonging to this category
1546 ObjCInterfaceDecl *ClassInterface;
1548 /// referenced protocols in this category.
1549 ObjCProtocolList ReferencedProtocols;
1551 /// Next category belonging to this class.
1552 /// FIXME: this should not be a singly-linked list. Move storage elsewhere.
1553 ObjCCategoryDecl *NextClassCategory;
1555 /// \brief The location of the category name in this declaration.
1556 SourceLocation CategoryNameLoc;
1558 /// class extension may have private ivars.
1559 SourceLocation IvarLBraceLoc;
1560 SourceLocation IvarRBraceLoc;
1562 ObjCCategoryDecl(DeclContext *DC, SourceLocation AtLoc,
1563 SourceLocation ClassNameLoc, SourceLocation CategoryNameLoc,
1564 IdentifierInfo *Id, ObjCInterfaceDecl *IDecl,
1565 SourceLocation IvarLBraceLoc=SourceLocation(),
1566 SourceLocation IvarRBraceLoc=SourceLocation())
1567 : ObjCContainerDecl(ObjCCategory, DC, Id, ClassNameLoc, AtLoc),
1568 ClassInterface(IDecl), NextClassCategory(0),
1569 CategoryNameLoc(CategoryNameLoc),
1570 IvarLBraceLoc(IvarLBraceLoc), IvarRBraceLoc(IvarRBraceLoc) {
1575 static ObjCCategoryDecl *Create(ASTContext &C, DeclContext *DC,
1576 SourceLocation AtLoc,
1577 SourceLocation ClassNameLoc,
1578 SourceLocation CategoryNameLoc,
1580 ObjCInterfaceDecl *IDecl,
1581 SourceLocation IvarLBraceLoc=SourceLocation(),
1582 SourceLocation IvarRBraceLoc=SourceLocation());
1583 static ObjCCategoryDecl *CreateDeserialized(ASTContext &C, unsigned ID);
1585 ObjCInterfaceDecl *getClassInterface() { return ClassInterface; }
1586 const ObjCInterfaceDecl *getClassInterface() const { return ClassInterface; }
1588 ObjCCategoryImplDecl *getImplementation() const;
1589 void setImplementation(ObjCCategoryImplDecl *ImplD);
1591 /// setProtocolList - Set the list of protocols that this interface
1593 void setProtocolList(ObjCProtocolDecl *const*List, unsigned Num,
1594 const SourceLocation *Locs, ASTContext &C) {
1595 ReferencedProtocols.set(List, Num, Locs, C);
1598 const ObjCProtocolList &getReferencedProtocols() const {
1599 return ReferencedProtocols;
1602 typedef ObjCProtocolList::iterator protocol_iterator;
1603 protocol_iterator protocol_begin() const {return ReferencedProtocols.begin();}
1604 protocol_iterator protocol_end() const { return ReferencedProtocols.end(); }
1605 unsigned protocol_size() const { return ReferencedProtocols.size(); }
1606 typedef ObjCProtocolList::loc_iterator protocol_loc_iterator;
1607 protocol_loc_iterator protocol_loc_begin() const {
1608 return ReferencedProtocols.loc_begin();
1610 protocol_loc_iterator protocol_loc_end() const {
1611 return ReferencedProtocols.loc_end();
1614 ObjCCategoryDecl *getNextClassCategory() const { return NextClassCategory; }
1616 /// \brief Retrieve the pointer to the next stored category (or extension),
1617 /// which may be hidden.
1618 ObjCCategoryDecl *getNextClassCategoryRaw() const {
1619 return NextClassCategory;
1622 bool IsClassExtension() const { return getIdentifier() == 0; }
1624 typedef specific_decl_iterator<ObjCIvarDecl> ivar_iterator;
1625 ivar_iterator ivar_begin() const {
1626 return ivar_iterator(decls_begin());
1628 ivar_iterator ivar_end() const {
1629 return ivar_iterator(decls_end());
1631 unsigned ivar_size() const {
1632 return std::distance(ivar_begin(), ivar_end());
1634 bool ivar_empty() const {
1635 return ivar_begin() == ivar_end();
1638 SourceLocation getCategoryNameLoc() const { return CategoryNameLoc; }
1639 void setCategoryNameLoc(SourceLocation Loc) { CategoryNameLoc = Loc; }
1641 void setIvarLBraceLoc(SourceLocation Loc) { IvarLBraceLoc = Loc; }
1642 SourceLocation getIvarLBraceLoc() const { return IvarLBraceLoc; }
1643 void setIvarRBraceLoc(SourceLocation Loc) { IvarRBraceLoc = Loc; }
1644 SourceLocation getIvarRBraceLoc() const { return IvarRBraceLoc; }
1646 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1647 static bool classofKind(Kind K) { return K == ObjCCategory; }
1649 friend class ASTDeclReader;
1650 friend class ASTDeclWriter;
1653 class ObjCImplDecl : public ObjCContainerDecl {
1654 virtual void anchor();
1656 /// Class interface for this class/category implementation
1657 ObjCInterfaceDecl *ClassInterface;
1660 ObjCImplDecl(Kind DK, DeclContext *DC,
1661 ObjCInterfaceDecl *classInterface,
1662 SourceLocation nameLoc, SourceLocation atStartLoc)
1663 : ObjCContainerDecl(DK, DC,
1664 classInterface? classInterface->getIdentifier() : 0,
1665 nameLoc, atStartLoc),
1666 ClassInterface(classInterface) {}
1669 const ObjCInterfaceDecl *getClassInterface() const { return ClassInterface; }
1670 ObjCInterfaceDecl *getClassInterface() { return ClassInterface; }
1671 void setClassInterface(ObjCInterfaceDecl *IFace);
1673 void addInstanceMethod(ObjCMethodDecl *method) {
1674 // FIXME: Context should be set correctly before we get here.
1675 method->setLexicalDeclContext(this);
1678 void addClassMethod(ObjCMethodDecl *method) {
1679 // FIXME: Context should be set correctly before we get here.
1680 method->setLexicalDeclContext(this);
1684 void addPropertyImplementation(ObjCPropertyImplDecl *property);
1686 ObjCPropertyImplDecl *FindPropertyImplDecl(IdentifierInfo *propertyId) const;
1687 ObjCPropertyImplDecl *FindPropertyImplIvarDecl(IdentifierInfo *ivarId) const;
1689 // Iterator access to properties.
1690 typedef specific_decl_iterator<ObjCPropertyImplDecl> propimpl_iterator;
1691 propimpl_iterator propimpl_begin() const {
1692 return propimpl_iterator(decls_begin());
1694 propimpl_iterator propimpl_end() const {
1695 return propimpl_iterator(decls_end());
1698 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1699 static bool classofKind(Kind K) {
1700 return K >= firstObjCImpl && K <= lastObjCImpl;
1704 /// ObjCCategoryImplDecl - An object of this class encapsulates a category
1705 /// \@implementation declaration. If a category class has declaration of a
1706 /// property, its implementation must be specified in the category's
1707 /// \@implementation declaration. Example:
1708 /// \@interface I \@end
1709 /// \@interface I(CATEGORY)
1710 /// \@property int p1, d1;
1712 /// \@implementation I(CATEGORY)
1713 /// \@dynamic p1,d1;
1716 /// ObjCCategoryImplDecl
1717 class ObjCCategoryImplDecl : public ObjCImplDecl {
1718 virtual void anchor();
1723 // Category name location
1724 SourceLocation CategoryNameLoc;
1726 ObjCCategoryImplDecl(DeclContext *DC, IdentifierInfo *Id,
1727 ObjCInterfaceDecl *classInterface,
1728 SourceLocation nameLoc, SourceLocation atStartLoc,
1729 SourceLocation CategoryNameLoc)
1730 : ObjCImplDecl(ObjCCategoryImpl, DC, classInterface, nameLoc, atStartLoc),
1731 Id(Id), CategoryNameLoc(CategoryNameLoc) {}
1733 static ObjCCategoryImplDecl *Create(ASTContext &C, DeclContext *DC,
1735 ObjCInterfaceDecl *classInterface,
1736 SourceLocation nameLoc,
1737 SourceLocation atStartLoc,
1738 SourceLocation CategoryNameLoc);
1739 static ObjCCategoryImplDecl *CreateDeserialized(ASTContext &C, unsigned ID);
1741 /// getIdentifier - Get the identifier that names the category
1742 /// interface associated with this implementation.
1743 /// FIXME: This is a bad API, we are overriding the NamedDecl::getIdentifier()
1744 /// to mean something different. For example:
1745 /// ((NamedDecl *)SomeCategoryImplDecl)->getIdentifier()
1746 /// returns the class interface name, whereas
1747 /// ((ObjCCategoryImplDecl *)SomeCategoryImplDecl)->getIdentifier()
1748 /// returns the category name.
1749 IdentifierInfo *getIdentifier() const {
1752 void setIdentifier(IdentifierInfo *II) { Id = II; }
1754 ObjCCategoryDecl *getCategoryDecl() const;
1756 SourceLocation getCategoryNameLoc() const { return CategoryNameLoc; }
1758 /// getName - Get the name of identifier for the class interface associated
1759 /// with this implementation as a StringRef.
1761 // FIXME: This is a bad API, we are overriding the NamedDecl::getName, to mean
1762 // something different.
1763 StringRef getName() const {
1764 return Id ? Id->getNameStart() : "";
1767 /// @brief Get the name of the class associated with this interface.
1769 // FIXME: Deprecated, move clients to getName().
1770 std::string getNameAsString() const {
1774 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1775 static bool classofKind(Kind K) { return K == ObjCCategoryImpl;}
1777 friend class ASTDeclReader;
1778 friend class ASTDeclWriter;
1781 raw_ostream &operator<<(raw_ostream &OS, const ObjCCategoryImplDecl &CID);
1783 /// ObjCImplementationDecl - Represents a class definition - this is where
1784 /// method definitions are specified. For example:
1787 /// \@implementation MyClass
1788 /// - (void)myMethod { /* do something */ }
1792 /// Typically, instance variables are specified in the class interface,
1793 /// *not* in the implementation. Nevertheless (for legacy reasons), we
1794 /// allow instance variables to be specified in the implementation. When
1795 /// specified, they need to be *identical* to the interface.
1797 class ObjCImplementationDecl : public ObjCImplDecl {
1798 virtual void anchor();
1799 /// Implementation Class's super class.
1800 ObjCInterfaceDecl *SuperClass;
1801 SourceLocation SuperLoc;
1803 /// \@implementation may have private ivars.
1804 SourceLocation IvarLBraceLoc;
1805 SourceLocation IvarRBraceLoc;
1807 /// Support for ivar initialization.
1808 /// IvarInitializers - The arguments used to initialize the ivars
1809 CXXCtorInitializer **IvarInitializers;
1810 unsigned NumIvarInitializers;
1812 /// Do the ivars of this class require initialization other than
1813 /// zero-initialization?
1814 bool HasNonZeroConstructors : 1;
1816 /// Do the ivars of this class require non-trivial destruction?
1817 bool HasDestructors : 1;
1819 ObjCImplementationDecl(DeclContext *DC,
1820 ObjCInterfaceDecl *classInterface,
1821 ObjCInterfaceDecl *superDecl,
1822 SourceLocation nameLoc, SourceLocation atStartLoc,
1823 SourceLocation superLoc = SourceLocation(),
1824 SourceLocation IvarLBraceLoc=SourceLocation(),
1825 SourceLocation IvarRBraceLoc=SourceLocation())
1826 : ObjCImplDecl(ObjCImplementation, DC, classInterface, nameLoc, atStartLoc),
1827 SuperClass(superDecl), SuperLoc(superLoc), IvarLBraceLoc(IvarLBraceLoc),
1828 IvarRBraceLoc(IvarRBraceLoc),
1829 IvarInitializers(0), NumIvarInitializers(0),
1830 HasNonZeroConstructors(false), HasDestructors(false) {}
1832 static ObjCImplementationDecl *Create(ASTContext &C, DeclContext *DC,
1833 ObjCInterfaceDecl *classInterface,
1834 ObjCInterfaceDecl *superDecl,
1835 SourceLocation nameLoc,
1836 SourceLocation atStartLoc,
1837 SourceLocation superLoc = SourceLocation(),
1838 SourceLocation IvarLBraceLoc=SourceLocation(),
1839 SourceLocation IvarRBraceLoc=SourceLocation());
1841 static ObjCImplementationDecl *CreateDeserialized(ASTContext &C, unsigned ID);
1843 /// init_iterator - Iterates through the ivar initializer list.
1844 typedef CXXCtorInitializer **init_iterator;
1846 /// init_const_iterator - Iterates through the ivar initializer list.
1847 typedef CXXCtorInitializer * const * init_const_iterator;
1849 /// init_begin() - Retrieve an iterator to the first initializer.
1850 init_iterator init_begin() { return IvarInitializers; }
1851 /// begin() - Retrieve an iterator to the first initializer.
1852 init_const_iterator init_begin() const { return IvarInitializers; }
1854 /// init_end() - Retrieve an iterator past the last initializer.
1855 init_iterator init_end() {
1856 return IvarInitializers + NumIvarInitializers;
1858 /// end() - Retrieve an iterator past the last initializer.
1859 init_const_iterator init_end() const {
1860 return IvarInitializers + NumIvarInitializers;
1862 /// getNumArgs - Number of ivars which must be initialized.
1863 unsigned getNumIvarInitializers() const {
1864 return NumIvarInitializers;
1867 void setNumIvarInitializers(unsigned numNumIvarInitializers) {
1868 NumIvarInitializers = numNumIvarInitializers;
1871 void setIvarInitializers(ASTContext &C,
1872 CXXCtorInitializer ** initializers,
1873 unsigned numInitializers);
1875 /// Do any of the ivars of this class (not counting its base classes)
1876 /// require construction other than zero-initialization?
1877 bool hasNonZeroConstructors() const { return HasNonZeroConstructors; }
1878 void setHasNonZeroConstructors(bool val) { HasNonZeroConstructors = val; }
1880 /// Do any of the ivars of this class (not counting its base classes)
1881 /// require non-trivial destruction?
1882 bool hasDestructors() const { return HasDestructors; }
1883 void setHasDestructors(bool val) { HasDestructors = val; }
1885 /// getIdentifier - Get the identifier that names the class
1886 /// interface associated with this implementation.
1887 IdentifierInfo *getIdentifier() const {
1888 return getClassInterface()->getIdentifier();
1891 /// getName - Get the name of identifier for the class interface associated
1892 /// with this implementation as a StringRef.
1894 // FIXME: This is a bad API, we are overriding the NamedDecl::getName, to mean
1895 // something different.
1896 StringRef getName() const {
1897 assert(getIdentifier() && "Name is not a simple identifier");
1898 return getIdentifier()->getName();
1901 /// @brief Get the name of the class associated with this interface.
1903 // FIXME: Move to StringRef API.
1904 std::string getNameAsString() const {
1908 const ObjCInterfaceDecl *getSuperClass() const { return SuperClass; }
1909 ObjCInterfaceDecl *getSuperClass() { return SuperClass; }
1910 SourceLocation getSuperClassLoc() const { return SuperLoc; }
1912 void setSuperClass(ObjCInterfaceDecl * superCls) { SuperClass = superCls; }
1914 void setIvarLBraceLoc(SourceLocation Loc) { IvarLBraceLoc = Loc; }
1915 SourceLocation getIvarLBraceLoc() const { return IvarLBraceLoc; }
1916 void setIvarRBraceLoc(SourceLocation Loc) { IvarRBraceLoc = Loc; }
1917 SourceLocation getIvarRBraceLoc() const { return IvarRBraceLoc; }
1919 typedef specific_decl_iterator<ObjCIvarDecl> ivar_iterator;
1920 ivar_iterator ivar_begin() const {
1921 return ivar_iterator(decls_begin());
1923 ivar_iterator ivar_end() const {
1924 return ivar_iterator(decls_end());
1926 unsigned ivar_size() const {
1927 return std::distance(ivar_begin(), ivar_end());
1929 bool ivar_empty() const {
1930 return ivar_begin() == ivar_end();
1933 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1934 static bool classofKind(Kind K) { return K == ObjCImplementation; }
1936 friend class ASTDeclReader;
1937 friend class ASTDeclWriter;
1940 raw_ostream &operator<<(raw_ostream &OS, const ObjCImplementationDecl &ID);
1942 /// ObjCCompatibleAliasDecl - Represents alias of a class. This alias is
1943 /// declared as \@compatibility_alias alias class.
1944 class ObjCCompatibleAliasDecl : public NamedDecl {
1945 virtual void anchor();
1946 /// Class that this is an alias of.
1947 ObjCInterfaceDecl *AliasedClass;
1949 ObjCCompatibleAliasDecl(DeclContext *DC, SourceLocation L, IdentifierInfo *Id,
1950 ObjCInterfaceDecl* aliasedClass)
1951 : NamedDecl(ObjCCompatibleAlias, DC, L, Id), AliasedClass(aliasedClass) {}
1953 static ObjCCompatibleAliasDecl *Create(ASTContext &C, DeclContext *DC,
1954 SourceLocation L, IdentifierInfo *Id,
1955 ObjCInterfaceDecl* aliasedClass);
1957 static ObjCCompatibleAliasDecl *CreateDeserialized(ASTContext &C,
1960 const ObjCInterfaceDecl *getClassInterface() const { return AliasedClass; }
1961 ObjCInterfaceDecl *getClassInterface() { return AliasedClass; }
1962 void setClassInterface(ObjCInterfaceDecl *D) { AliasedClass = D; }
1964 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1965 static bool classofKind(Kind K) { return K == ObjCCompatibleAlias; }
1969 /// \brief Represents one property declaration in an Objective-C interface.
1973 /// \@property (assign, readwrite) int MyProperty;
1975 class ObjCPropertyDecl : public NamedDecl {
1976 virtual void anchor();
1978 enum PropertyAttributeKind {
1979 OBJC_PR_noattr = 0x00,
1980 OBJC_PR_readonly = 0x01,
1981 OBJC_PR_getter = 0x02,
1982 OBJC_PR_assign = 0x04,
1983 OBJC_PR_readwrite = 0x08,
1984 OBJC_PR_retain = 0x10,
1985 OBJC_PR_copy = 0x20,
1986 OBJC_PR_nonatomic = 0x40,
1987 OBJC_PR_setter = 0x80,
1988 OBJC_PR_atomic = 0x100,
1989 OBJC_PR_weak = 0x200,
1990 OBJC_PR_strong = 0x400,
1991 OBJC_PR_unsafe_unretained = 0x800
1992 // Adding a property should change NumPropertyAttrsBits
1996 /// \brief Number of bits fitting all the property attributes.
1997 NumPropertyAttrsBits = 12
2000 enum SetterKind { Assign, Retain, Copy, Weak };
2001 enum PropertyControl { None, Required, Optional };
2003 SourceLocation AtLoc; // location of \@property
2004 SourceLocation LParenLoc; // location of '(' starting attribute list or null.
2005 TypeSourceInfo *DeclType;
2006 unsigned PropertyAttributes : NumPropertyAttrsBits;
2007 unsigned PropertyAttributesAsWritten : NumPropertyAttrsBits;
2008 // \@required/\@optional
2009 unsigned PropertyImplementation : 2;
2011 Selector GetterName; // getter name of NULL if no getter
2012 Selector SetterName; // setter name of NULL if no setter
2014 ObjCMethodDecl *GetterMethodDecl; // Declaration of getter instance method
2015 ObjCMethodDecl *SetterMethodDecl; // Declaration of setter instance method
2016 ObjCIvarDecl *PropertyIvarDecl; // Synthesize ivar for this property
2018 ObjCPropertyDecl(DeclContext *DC, SourceLocation L, IdentifierInfo *Id,
2019 SourceLocation AtLocation, SourceLocation LParenLocation,
2021 : NamedDecl(ObjCProperty, DC, L, Id), AtLoc(AtLocation),
2022 LParenLoc(LParenLocation), DeclType(T),
2023 PropertyAttributes(OBJC_PR_noattr),
2024 PropertyAttributesAsWritten(OBJC_PR_noattr),
2025 PropertyImplementation(None),
2026 GetterName(Selector()),
2027 SetterName(Selector()),
2028 GetterMethodDecl(0), SetterMethodDecl(0) , PropertyIvarDecl(0) {}
2030 static ObjCPropertyDecl *Create(ASTContext &C, DeclContext *DC,
2032 IdentifierInfo *Id, SourceLocation AtLocation,
2033 SourceLocation LParenLocation,
2035 PropertyControl propControl = None);
2037 static ObjCPropertyDecl *CreateDeserialized(ASTContext &C, unsigned ID);
2039 SourceLocation getAtLoc() const { return AtLoc; }
2040 void setAtLoc(SourceLocation L) { AtLoc = L; }
2042 SourceLocation getLParenLoc() const { return LParenLoc; }
2043 void setLParenLoc(SourceLocation L) { LParenLoc = L; }
2045 TypeSourceInfo *getTypeSourceInfo() const { return DeclType; }
2046 QualType getType() const { return DeclType->getType(); }
2047 void setType(TypeSourceInfo *T) { DeclType = T; }
2049 PropertyAttributeKind getPropertyAttributes() const {
2050 return PropertyAttributeKind(PropertyAttributes);
2052 void setPropertyAttributes(PropertyAttributeKind PRVal) {
2053 PropertyAttributes |= PRVal;
2056 PropertyAttributeKind getPropertyAttributesAsWritten() const {
2057 return PropertyAttributeKind(PropertyAttributesAsWritten);
2060 bool hasWrittenStorageAttribute() const {
2061 return PropertyAttributesAsWritten & (OBJC_PR_assign | OBJC_PR_copy |
2062 OBJC_PR_unsafe_unretained | OBJC_PR_retain | OBJC_PR_strong |
2066 void setPropertyAttributesAsWritten(PropertyAttributeKind PRVal) {
2067 PropertyAttributesAsWritten = PRVal;
2070 void makeitReadWriteAttribute() {
2071 PropertyAttributes &= ~OBJC_PR_readonly;
2072 PropertyAttributes |= OBJC_PR_readwrite;
2075 // Helper methods for accessing attributes.
2077 /// isReadOnly - Return true iff the property has a setter.
2078 bool isReadOnly() const {
2079 return (PropertyAttributes & OBJC_PR_readonly);
2082 /// isAtomic - Return true if the property is atomic.
2083 bool isAtomic() const {
2084 return (PropertyAttributes & OBJC_PR_atomic);
2087 /// isRetaining - Return true if the property retains its value.
2088 bool isRetaining() const {
2089 return (PropertyAttributes &
2090 (OBJC_PR_retain | OBJC_PR_strong | OBJC_PR_copy));
2093 /// getSetterKind - Return the method used for doing assignment in
2094 /// the property setter. This is only valid if the property has been
2095 /// defined to have a setter.
2096 SetterKind getSetterKind() const {
2097 if (PropertyAttributes & OBJC_PR_strong)
2098 return getType()->isBlockPointerType() ? Copy : Retain;
2099 if (PropertyAttributes & OBJC_PR_retain)
2101 if (PropertyAttributes & OBJC_PR_copy)
2103 if (PropertyAttributes & OBJC_PR_weak)
2108 Selector getGetterName() const { return GetterName; }
2109 void setGetterName(Selector Sel) { GetterName = Sel; }
2111 Selector getSetterName() const { return SetterName; }
2112 void setSetterName(Selector Sel) { SetterName = Sel; }
2114 ObjCMethodDecl *getGetterMethodDecl() const { return GetterMethodDecl; }
2115 void setGetterMethodDecl(ObjCMethodDecl *gDecl) { GetterMethodDecl = gDecl; }
2117 ObjCMethodDecl *getSetterMethodDecl() const { return SetterMethodDecl; }
2118 void setSetterMethodDecl(ObjCMethodDecl *gDecl) { SetterMethodDecl = gDecl; }
2120 // Related to \@optional/\@required declared in \@protocol
2121 void setPropertyImplementation(PropertyControl pc) {
2122 PropertyImplementation = pc;
2124 PropertyControl getPropertyImplementation() const {
2125 return PropertyControl(PropertyImplementation);
2128 void setPropertyIvarDecl(ObjCIvarDecl *Ivar) {
2129 PropertyIvarDecl = Ivar;
2131 ObjCIvarDecl *getPropertyIvarDecl() const {
2132 return PropertyIvarDecl;
2135 virtual SourceRange getSourceRange() const LLVM_READONLY {
2136 return SourceRange(AtLoc, getLocation());
2139 /// Get the default name of the synthesized ivar.
2140 IdentifierInfo *getDefaultSynthIvarName(ASTContext &Ctx) const;
2142 /// Lookup a property by name in the specified DeclContext.
2143 static ObjCPropertyDecl *findPropertyDecl(const DeclContext *DC,
2144 IdentifierInfo *propertyID);
2146 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
2147 static bool classofKind(Kind K) { return K == ObjCProperty; }
2150 /// ObjCPropertyImplDecl - Represents implementation declaration of a property
2151 /// in a class or category implementation block. For example:
2152 /// \@synthesize prop1 = ivar1;
2154 class ObjCPropertyImplDecl : public Decl {
2161 SourceLocation AtLoc; // location of \@synthesize or \@dynamic
2163 /// \brief For \@synthesize, the location of the ivar, if it was written in
2164 /// the source code.
2167 /// \@synthesize int a = b
2169 SourceLocation IvarLoc;
2171 /// Property declaration being implemented
2172 ObjCPropertyDecl *PropertyDecl;
2174 /// Null for \@dynamic. Required for \@synthesize.
2175 ObjCIvarDecl *PropertyIvarDecl;
2177 /// Null for \@dynamic. Non-null if property must be copy-constructed in
2179 Expr *GetterCXXConstructor;
2181 /// Null for \@dynamic. Non-null if property has assignment operator to call
2182 /// in Setter synthesis.
2183 Expr *SetterCXXAssignment;
2185 ObjCPropertyImplDecl(DeclContext *DC, SourceLocation atLoc, SourceLocation L,
2186 ObjCPropertyDecl *property,
2188 ObjCIvarDecl *ivarDecl,
2189 SourceLocation ivarLoc)
2190 : Decl(ObjCPropertyImpl, DC, L), AtLoc(atLoc),
2191 IvarLoc(ivarLoc), PropertyDecl(property), PropertyIvarDecl(ivarDecl),
2192 GetterCXXConstructor(0), SetterCXXAssignment(0) {
2193 assert (PK == Dynamic || PropertyIvarDecl);
2197 static ObjCPropertyImplDecl *Create(ASTContext &C, DeclContext *DC,
2198 SourceLocation atLoc, SourceLocation L,
2199 ObjCPropertyDecl *property,
2201 ObjCIvarDecl *ivarDecl,
2202 SourceLocation ivarLoc);
2204 static ObjCPropertyImplDecl *CreateDeserialized(ASTContext &C, unsigned ID);
2206 virtual SourceRange getSourceRange() const LLVM_READONLY;
2208 SourceLocation getLocStart() const LLVM_READONLY { return AtLoc; }
2209 void setAtLoc(SourceLocation Loc) { AtLoc = Loc; }
2211 ObjCPropertyDecl *getPropertyDecl() const {
2212 return PropertyDecl;
2214 void setPropertyDecl(ObjCPropertyDecl *Prop) { PropertyDecl = Prop; }
2216 Kind getPropertyImplementation() const {
2217 return PropertyIvarDecl ? Synthesize : Dynamic;
2220 ObjCIvarDecl *getPropertyIvarDecl() const {
2221 return PropertyIvarDecl;
2223 SourceLocation getPropertyIvarDeclLoc() const { return IvarLoc; }
2225 void setPropertyIvarDecl(ObjCIvarDecl *Ivar,
2226 SourceLocation IvarLoc) {
2227 PropertyIvarDecl = Ivar;
2228 this->IvarLoc = IvarLoc;
2231 /// \brief For \@synthesize, returns true if an ivar name was explicitly
2235 /// \@synthesize int a = b; // true
2236 /// \@synthesize int a; // false
2238 bool isIvarNameSpecified() const {
2239 return IvarLoc.isValid() && IvarLoc != getLocation();
2242 Expr *getGetterCXXConstructor() const {
2243 return GetterCXXConstructor;
2245 void setGetterCXXConstructor(Expr *getterCXXConstructor) {
2246 GetterCXXConstructor = getterCXXConstructor;
2249 Expr *getSetterCXXAssignment() const {
2250 return SetterCXXAssignment;
2252 void setSetterCXXAssignment(Expr *setterCXXAssignment) {
2253 SetterCXXAssignment = setterCXXAssignment;
2256 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
2257 static bool classofKind(Decl::Kind K) { return K == ObjCPropertyImpl; }
2259 friend class ASTDeclReader;
2262 template<bool (*Filter)(ObjCCategoryDecl *)>
2264 ObjCInterfaceDecl::filtered_category_iterator<Filter>::
2265 findAcceptableCategory() {
2266 while (Current && !Filter(Current))
2267 Current = Current->getNextClassCategoryRaw();
2270 template<bool (*Filter)(ObjCCategoryDecl *)>
2271 inline ObjCInterfaceDecl::filtered_category_iterator<Filter> &
2272 ObjCInterfaceDecl::filtered_category_iterator<Filter>::operator++() {
2273 Current = Current->getNextClassCategoryRaw();
2274 findAcceptableCategory();
2278 inline bool ObjCInterfaceDecl::isVisibleCategory(ObjCCategoryDecl *Cat) {
2279 return !Cat->isHidden();
2282 inline bool ObjCInterfaceDecl::isVisibleExtension(ObjCCategoryDecl *Cat) {
2283 return Cat->IsClassExtension() && !Cat->isHidden();
2286 inline bool ObjCInterfaceDecl::isKnownExtension(ObjCCategoryDecl *Cat) {
2287 return Cat->IsClassExtension();
2290 } // end namespace clang