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.isValid(); }
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 hasBody(); }
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::DenseMap<const ObjCProtocolDecl *, ObjCPropertyDecl*>
559 typedef llvm::SmallVector<ObjCPropertyDecl*, 8> PropertyDeclOrder;
561 /// This routine collects list of properties to be implemented in the class.
562 /// This includes, class's and its conforming protocols' properties.
563 /// Note, the superclass's properties are not included in the list.
564 virtual void collectPropertiesToImplement(PropertyMap &PM,
565 PropertyDeclOrder &PO) const {}
567 SourceLocation getAtStartLoc() const { return AtStart; }
568 void setAtStartLoc(SourceLocation Loc) { AtStart = Loc; }
570 // Marks the end of the container.
571 SourceRange getAtEndRange() const {
574 void setAtEndRange(SourceRange atEnd) {
578 virtual SourceRange getSourceRange() const LLVM_READONLY {
579 return SourceRange(AtStart, getAtEndRange().getEnd());
582 // Implement isa/cast/dyncast/etc.
583 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
584 static bool classofKind(Kind K) {
585 return K >= firstObjCContainer &&
586 K <= lastObjCContainer;
589 static DeclContext *castToDeclContext(const ObjCContainerDecl *D) {
590 return static_cast<DeclContext *>(const_cast<ObjCContainerDecl*>(D));
592 static ObjCContainerDecl *castFromDeclContext(const DeclContext *DC) {
593 return static_cast<ObjCContainerDecl *>(const_cast<DeclContext*>(DC));
597 /// \brief Represents an ObjC class declaration.
602 /// // MostPrimitive declares no super class (not particularly useful).
603 /// \@interface MostPrimitive
604 /// // no instance variables or methods.
607 /// // NSResponder inherits from NSObject & implements NSCoding (a protocol).
608 /// \@interface NSResponder : NSObject \<NSCoding>
609 /// { // instance variables are represented by ObjCIvarDecl.
610 /// id nextResponder; // nextResponder instance variable.
612 /// - (NSResponder *)nextResponder; // return a pointer to NSResponder.
613 /// - (void)mouseMoved:(NSEvent *)theEvent; // return void, takes a pointer
614 /// \@end // to an NSEvent.
617 /// Unlike C/C++, forward class declarations are accomplished with \@class.
618 /// Unlike C/C++, \@class allows for a list of classes to be forward declared.
619 /// Unlike C++, ObjC is a single-rooted class model. In Cocoa, classes
620 /// typically inherit from NSObject (an exception is NSProxy).
622 class ObjCInterfaceDecl : public ObjCContainerDecl
623 , public Redeclarable<ObjCInterfaceDecl> {
624 virtual void anchor();
626 /// TypeForDecl - This indicates the Type object that represents this
627 /// TypeDecl. It is a cache maintained by ASTContext::getObjCInterfaceType
628 mutable const Type *TypeForDecl;
629 friend class ASTContext;
631 struct DefinitionData {
632 /// \brief The definition of this class, for quick access from any
634 ObjCInterfaceDecl *Definition;
636 /// Class's super class.
637 ObjCInterfaceDecl *SuperClass;
639 /// Protocols referenced in the \@interface declaration
640 ObjCProtocolList ReferencedProtocols;
642 /// Protocols reference in both the \@interface and class extensions.
643 ObjCList<ObjCProtocolDecl> AllReferencedProtocols;
645 /// \brief List of categories and class extensions defined for this class.
647 /// Categories are stored as a linked list in the AST, since the categories
648 /// and class extensions come long after the initial interface declaration,
649 /// and we avoid dynamically-resized arrays in the AST wherever possible.
650 ObjCCategoryDecl *CategoryList;
652 /// IvarList - List of all ivars defined by this class; including class
653 /// extensions and implementation. This list is built lazily.
654 ObjCIvarDecl *IvarList;
656 /// \brief Indicates that the contents of this Objective-C class will be
657 /// completed by the external AST source when required.
658 mutable bool ExternallyCompleted : 1;
660 /// \brief Indicates that the ivar cache does not yet include ivars
661 /// declared in the implementation.
662 mutable bool IvarListMissingImplementation : 1;
664 /// \brief The location of the superclass, if any.
665 SourceLocation SuperClassLoc;
667 /// \brief The location of the last location in this declaration, before
668 /// the properties/methods. For example, this will be the '>', '}', or
670 SourceLocation EndLoc;
672 DefinitionData() : Definition(), SuperClass(), CategoryList(), IvarList(),
673 ExternallyCompleted(),
674 IvarListMissingImplementation(true) { }
677 ObjCInterfaceDecl(DeclContext *DC, SourceLocation atLoc, IdentifierInfo *Id,
678 SourceLocation CLoc, ObjCInterfaceDecl *PrevDecl,
681 void LoadExternalDefinition() const;
683 /// \brief Contains a pointer to the data associated with this class,
684 /// which will be NULL if this class has not yet been defined.
686 /// The bit indicates when we don't need to check for out-of-date
687 /// declarations. It will be set unless modules are enabled.
688 llvm::PointerIntPair<DefinitionData *, 1, bool> Data;
690 DefinitionData &data() const {
691 assert(Data.getPointer() && "Declaration has no definition!");
692 return *Data.getPointer();
695 /// \brief Allocate the definition data for this class.
696 void allocateDefinitionData();
698 typedef Redeclarable<ObjCInterfaceDecl> redeclarable_base;
699 virtual ObjCInterfaceDecl *getNextRedeclaration() {
700 return RedeclLink.getNext();
702 virtual ObjCInterfaceDecl *getPreviousDeclImpl() {
703 return getPreviousDecl();
705 virtual ObjCInterfaceDecl *getMostRecentDeclImpl() {
706 return getMostRecentDecl();
710 static ObjCInterfaceDecl *Create(const ASTContext &C, DeclContext *DC,
711 SourceLocation atLoc,
713 ObjCInterfaceDecl *PrevDecl,
714 SourceLocation ClassLoc = SourceLocation(),
715 bool isInternal = false);
717 static ObjCInterfaceDecl *CreateDeserialized(ASTContext &C, unsigned ID);
719 virtual SourceRange getSourceRange() const LLVM_READONLY {
720 if (isThisDeclarationADefinition())
721 return ObjCContainerDecl::getSourceRange();
723 return SourceRange(getAtStartLoc(), getLocation());
726 /// \brief Indicate that this Objective-C class is complete, but that
727 /// the external AST source will be responsible for filling in its contents
728 /// when a complete class is required.
729 void setExternallyCompleted();
731 const ObjCProtocolList &getReferencedProtocols() const {
732 assert(hasDefinition() && "Caller did not check for forward reference!");
733 if (data().ExternallyCompleted)
734 LoadExternalDefinition();
736 return data().ReferencedProtocols;
739 ObjCImplementationDecl *getImplementation() const;
740 void setImplementation(ObjCImplementationDecl *ImplD);
742 ObjCCategoryDecl *FindCategoryDeclaration(IdentifierInfo *CategoryId) const;
744 // Get the local instance/class method declared in a category.
745 ObjCMethodDecl *getCategoryInstanceMethod(Selector Sel) const;
746 ObjCMethodDecl *getCategoryClassMethod(Selector Sel) const;
747 ObjCMethodDecl *getCategoryMethod(Selector Sel, bool isInstance) const {
748 return isInstance ? getInstanceMethod(Sel)
749 : getClassMethod(Sel);
752 typedef ObjCProtocolList::iterator protocol_iterator;
754 protocol_iterator protocol_begin() const {
755 // FIXME: Should make sure no callers ever do this.
756 if (!hasDefinition())
757 return protocol_iterator();
759 if (data().ExternallyCompleted)
760 LoadExternalDefinition();
762 return data().ReferencedProtocols.begin();
764 protocol_iterator protocol_end() const {
765 // FIXME: Should make sure no callers ever do this.
766 if (!hasDefinition())
767 return protocol_iterator();
769 if (data().ExternallyCompleted)
770 LoadExternalDefinition();
772 return data().ReferencedProtocols.end();
775 typedef ObjCProtocolList::loc_iterator protocol_loc_iterator;
777 protocol_loc_iterator protocol_loc_begin() const {
778 // FIXME: Should make sure no callers ever do this.
779 if (!hasDefinition())
780 return protocol_loc_iterator();
782 if (data().ExternallyCompleted)
783 LoadExternalDefinition();
785 return data().ReferencedProtocols.loc_begin();
788 protocol_loc_iterator protocol_loc_end() const {
789 // FIXME: Should make sure no callers ever do this.
790 if (!hasDefinition())
791 return protocol_loc_iterator();
793 if (data().ExternallyCompleted)
794 LoadExternalDefinition();
796 return data().ReferencedProtocols.loc_end();
799 typedef ObjCList<ObjCProtocolDecl>::iterator all_protocol_iterator;
801 all_protocol_iterator all_referenced_protocol_begin() const {
802 // FIXME: Should make sure no callers ever do this.
803 if (!hasDefinition())
804 return all_protocol_iterator();
806 if (data().ExternallyCompleted)
807 LoadExternalDefinition();
809 return data().AllReferencedProtocols.empty()
811 : data().AllReferencedProtocols.begin();
813 all_protocol_iterator all_referenced_protocol_end() const {
814 // FIXME: Should make sure no callers ever do this.
815 if (!hasDefinition())
816 return all_protocol_iterator();
818 if (data().ExternallyCompleted)
819 LoadExternalDefinition();
821 return data().AllReferencedProtocols.empty()
823 : data().AllReferencedProtocols.end();
826 typedef specific_decl_iterator<ObjCIvarDecl> ivar_iterator;
828 ivar_iterator ivar_begin() const {
829 if (const ObjCInterfaceDecl *Def = getDefinition())
830 return ivar_iterator(Def->decls_begin());
832 // FIXME: Should make sure no callers ever do this.
833 return ivar_iterator();
835 ivar_iterator ivar_end() const {
836 if (const ObjCInterfaceDecl *Def = getDefinition())
837 return ivar_iterator(Def->decls_end());
839 // FIXME: Should make sure no callers ever do this.
840 return ivar_iterator();
843 unsigned ivar_size() const {
844 return std::distance(ivar_begin(), ivar_end());
847 bool ivar_empty() const { return ivar_begin() == ivar_end(); }
849 ObjCIvarDecl *all_declared_ivar_begin();
850 const ObjCIvarDecl *all_declared_ivar_begin() const {
851 // Even though this modifies IvarList, it's conceptually const:
852 // the ivar chain is essentially a cached property of ObjCInterfaceDecl.
853 return const_cast<ObjCInterfaceDecl *>(this)->all_declared_ivar_begin();
855 void setIvarList(ObjCIvarDecl *ivar) { data().IvarList = ivar; }
857 /// setProtocolList - Set the list of protocols that this interface
859 void setProtocolList(ObjCProtocolDecl *const* List, unsigned Num,
860 const SourceLocation *Locs, ASTContext &C) {
861 data().ReferencedProtocols.set(List, Num, Locs, C);
864 /// mergeClassExtensionProtocolList - Merge class extension's protocol list
865 /// into the protocol list for this class.
866 void mergeClassExtensionProtocolList(ObjCProtocolDecl *const* List,
870 /// \brief Determine whether this particular declaration of this class is
871 /// actually also a definition.
872 bool isThisDeclarationADefinition() const {
873 return getDefinition() == this;
876 /// \brief Determine whether this class has been defined.
877 bool hasDefinition() const {
878 // If the name of this class is out-of-date, bring it up-to-date, which
879 // might bring in a definition.
880 // Note: a null value indicates that we don't have a definition and that
881 // modules are enabled.
882 if (!Data.getOpaqueValue()) {
883 if (IdentifierInfo *II = getIdentifier()) {
884 if (II->isOutOfDate()) {
885 updateOutOfDate(*II);
890 return Data.getPointer();
893 /// \brief Retrieve the definition of this class, or NULL if this class
894 /// has been forward-declared (with \@class) but not yet defined (with
896 ObjCInterfaceDecl *getDefinition() {
897 return hasDefinition()? Data.getPointer()->Definition : 0;
900 /// \brief Retrieve the definition of this class, or NULL if this class
901 /// has been forward-declared (with \@class) but not yet defined (with
903 const ObjCInterfaceDecl *getDefinition() const {
904 return hasDefinition()? Data.getPointer()->Definition : 0;
907 /// \brief Starts the definition of this Objective-C class, taking it from
908 /// a forward declaration (\@class) to a definition (\@interface).
909 void startDefinition();
911 ObjCInterfaceDecl *getSuperClass() const {
912 // FIXME: Should make sure no callers ever do this.
913 if (!hasDefinition())
916 if (data().ExternallyCompleted)
917 LoadExternalDefinition();
919 return data().SuperClass;
922 void setSuperClass(ObjCInterfaceDecl * superCls) {
924 (superCls && superCls->hasDefinition()) ? superCls->getDefinition()
928 /// \brief Iterator that walks over the list of categories, filtering out
929 /// those that do not meet specific criteria.
931 /// This class template is used for the various permutations of category
932 /// and extension iterators.
933 template<bool (*Filter)(ObjCCategoryDecl *)>
934 class filtered_category_iterator {
935 ObjCCategoryDecl *Current;
937 void findAcceptableCategory();
940 typedef ObjCCategoryDecl * value_type;
941 typedef value_type reference;
942 typedef value_type pointer;
943 typedef std::ptrdiff_t difference_type;
944 typedef std::input_iterator_tag iterator_category;
946 filtered_category_iterator() : Current(0) { }
947 explicit filtered_category_iterator(ObjCCategoryDecl *Current)
950 findAcceptableCategory();
953 reference operator*() const { return Current; }
954 pointer operator->() const { return Current; }
956 filtered_category_iterator &operator++();
958 filtered_category_iterator operator++(int) {
959 filtered_category_iterator Tmp = *this;
964 friend bool operator==(filtered_category_iterator X,
965 filtered_category_iterator Y) {
966 return X.Current == Y.Current;
969 friend bool operator!=(filtered_category_iterator X,
970 filtered_category_iterator Y) {
971 return X.Current != Y.Current;
976 /// \brief Test whether the given category is visible.
978 /// Used in the \c visible_categories_iterator.
979 static bool isVisibleCategory(ObjCCategoryDecl *Cat);
982 /// \brief Iterator that walks over the list of categories and extensions
983 /// that are visible, i.e., not hidden in a non-imported submodule.
984 typedef filtered_category_iterator<isVisibleCategory>
985 visible_categories_iterator;
987 /// \brief Retrieve an iterator to the beginning of the visible-categories
989 visible_categories_iterator visible_categories_begin() const {
990 return visible_categories_iterator(getCategoryListRaw());
993 /// \brief Retrieve an iterator to the end of the visible-categories list.
994 visible_categories_iterator visible_categories_end() const {
995 return visible_categories_iterator();
998 /// \brief Determine whether the visible-categories list is empty.
999 bool visible_categories_empty() const {
1000 return visible_categories_begin() == visible_categories_end();
1004 /// \brief Test whether the given category... is a category.
1006 /// Used in the \c known_categories_iterator.
1007 static bool isKnownCategory(ObjCCategoryDecl *) { return true; }
1010 /// \brief Iterator that walks over all of the known categories and
1011 /// extensions, including those that are hidden.
1012 typedef filtered_category_iterator<isKnownCategory> known_categories_iterator;
1014 /// \brief Retrieve an iterator to the beginning of the known-categories
1016 known_categories_iterator known_categories_begin() const {
1017 return known_categories_iterator(getCategoryListRaw());
1020 /// \brief Retrieve an iterator to the end of the known-categories list.
1021 known_categories_iterator known_categories_end() const {
1022 return known_categories_iterator();
1025 /// \brief Determine whether the known-categories list is empty.
1026 bool known_categories_empty() const {
1027 return known_categories_begin() == known_categories_end();
1031 /// \brief Test whether the given category is a visible extension.
1033 /// Used in the \c visible_extensions_iterator.
1034 static bool isVisibleExtension(ObjCCategoryDecl *Cat);
1037 /// \brief Iterator that walks over all of the visible extensions, skipping
1038 /// any that are known but hidden.
1039 typedef filtered_category_iterator<isVisibleExtension>
1040 visible_extensions_iterator;
1042 /// \brief Retrieve an iterator to the beginning of the visible-extensions
1044 visible_extensions_iterator visible_extensions_begin() const {
1045 return visible_extensions_iterator(getCategoryListRaw());
1048 /// \brief Retrieve an iterator to the end of the visible-extensions list.
1049 visible_extensions_iterator visible_extensions_end() const {
1050 return visible_extensions_iterator();
1053 /// \brief Determine whether the visible-extensions list is empty.
1054 bool visible_extensions_empty() const {
1055 return visible_extensions_begin() == visible_extensions_end();
1059 /// \brief Test whether the given category is an extension.
1061 /// Used in the \c known_extensions_iterator.
1062 static bool isKnownExtension(ObjCCategoryDecl *Cat);
1065 /// \brief Iterator that walks over all of the known extensions.
1066 typedef filtered_category_iterator<isKnownExtension>
1067 known_extensions_iterator;
1069 /// \brief Retrieve an iterator to the beginning of the known-extensions
1071 known_extensions_iterator known_extensions_begin() const {
1072 return known_extensions_iterator(getCategoryListRaw());
1075 /// \brief Retrieve an iterator to the end of the known-extensions list.
1076 known_extensions_iterator known_extensions_end() const {
1077 return known_extensions_iterator();
1080 /// \brief Determine whether the known-extensions list is empty.
1081 bool known_extensions_empty() const {
1082 return known_extensions_begin() == known_extensions_end();
1085 /// \brief Retrieve the raw pointer to the start of the category/extension
1087 ObjCCategoryDecl* getCategoryListRaw() const {
1088 // FIXME: Should make sure no callers ever do this.
1089 if (!hasDefinition())
1092 if (data().ExternallyCompleted)
1093 LoadExternalDefinition();
1095 return data().CategoryList;
1098 /// \brief Set the raw pointer to the start of the category/extension
1100 void setCategoryListRaw(ObjCCategoryDecl *category) {
1101 data().CategoryList = category;
1105 *FindPropertyVisibleInPrimaryClass(IdentifierInfo *PropertyId) const;
1107 virtual void collectPropertiesToImplement(PropertyMap &PM,
1108 PropertyDeclOrder &PO) const;
1110 /// isSuperClassOf - Return true if this class is the specified class or is a
1111 /// super class of the specified interface class.
1112 bool isSuperClassOf(const ObjCInterfaceDecl *I) const {
1113 // If RHS is derived from LHS it is OK; else it is not OK.
1115 if (declaresSameEntity(this, I))
1118 I = I->getSuperClass();
1123 /// isArcWeakrefUnavailable - Checks for a class or one of its super classes
1124 /// to be incompatible with __weak references. Returns true if it is.
1125 bool isArcWeakrefUnavailable() const;
1127 /// isObjCRequiresPropertyDefs - Checks that a class or one of its super
1128 /// classes must not be auto-synthesized. Returns class decl. if it must not
1129 /// be; 0, otherwise.
1130 const ObjCInterfaceDecl *isObjCRequiresPropertyDefs() const;
1132 ObjCIvarDecl *lookupInstanceVariable(IdentifierInfo *IVarName,
1133 ObjCInterfaceDecl *&ClassDeclared);
1134 ObjCIvarDecl *lookupInstanceVariable(IdentifierInfo *IVarName) {
1135 ObjCInterfaceDecl *ClassDeclared;
1136 return lookupInstanceVariable(IVarName, ClassDeclared);
1139 ObjCProtocolDecl *lookupNestedProtocol(IdentifierInfo *Name);
1141 // Lookup a method. First, we search locally. If a method isn't
1142 // found, we search referenced protocols and class categories.
1143 ObjCMethodDecl *lookupMethod(Selector Sel, bool isInstance,
1144 bool shallowCategoryLookup= false,
1145 const ObjCCategoryDecl *C= 0) const;
1146 ObjCMethodDecl *lookupInstanceMethod(Selector Sel,
1147 bool shallowCategoryLookup = false) const {
1148 return lookupMethod(Sel, true/*isInstance*/, shallowCategoryLookup);
1150 ObjCMethodDecl *lookupClassMethod(Selector Sel,
1151 bool shallowCategoryLookup = false) const {
1152 return lookupMethod(Sel, false/*isInstance*/, shallowCategoryLookup);
1154 ObjCInterfaceDecl *lookupInheritedClass(const IdentifierInfo *ICName);
1156 /// \brief Lookup a method in the classes implementation hierarchy.
1157 ObjCMethodDecl *lookupPrivateMethod(const Selector &Sel,
1158 bool Instance=true) const;
1160 ObjCMethodDecl *lookupPrivateClassMethod(const Selector &Sel) {
1161 return lookupPrivateMethod(Sel, false);
1164 /// \brief Lookup a setter or getter in the class hierarchy,
1165 /// including in all categories except for category passed
1167 ObjCMethodDecl *lookupPropertyAccessor(const Selector Sel,
1168 const ObjCCategoryDecl *Cat) const {
1169 return lookupMethod(Sel, true/*isInstance*/,
1170 false/*shallowCategoryLookup*/, Cat);
1173 SourceLocation getEndOfDefinitionLoc() const {
1174 if (!hasDefinition())
1175 return getLocation();
1177 return data().EndLoc;
1180 void setEndOfDefinitionLoc(SourceLocation LE) { data().EndLoc = LE; }
1182 void setSuperClassLoc(SourceLocation Loc) { data().SuperClassLoc = Loc; }
1183 SourceLocation getSuperClassLoc() const { return data().SuperClassLoc; }
1185 /// isImplicitInterfaceDecl - check that this is an implicitly declared
1186 /// ObjCInterfaceDecl node. This is for legacy objective-c \@implementation
1187 /// declaration without an \@interface declaration.
1188 bool isImplicitInterfaceDecl() const {
1189 return hasDefinition() ? data().Definition->isImplicit() : isImplicit();
1192 /// ClassImplementsProtocol - Checks that 'lProto' protocol
1193 /// has been implemented in IDecl class, its super class or categories (if
1194 /// lookupCategory is true).
1195 bool ClassImplementsProtocol(ObjCProtocolDecl *lProto,
1196 bool lookupCategory,
1197 bool RHSIsQualifiedID = false);
1199 typedef redeclarable_base::redecl_iterator redecl_iterator;
1200 using redeclarable_base::redecls_begin;
1201 using redeclarable_base::redecls_end;
1202 using redeclarable_base::getPreviousDecl;
1203 using redeclarable_base::getMostRecentDecl;
1204 using redeclarable_base::isFirstDecl;
1206 /// Retrieves the canonical declaration of this Objective-C class.
1207 ObjCInterfaceDecl *getCanonicalDecl() { return getFirstDecl(); }
1208 const ObjCInterfaceDecl *getCanonicalDecl() const { return getFirstDecl(); }
1210 // Low-level accessor
1211 const Type *getTypeForDecl() const { return TypeForDecl; }
1212 void setTypeForDecl(const Type *TD) const { TypeForDecl = TD; }
1214 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1215 static bool classofKind(Kind K) { return K == ObjCInterface; }
1217 friend class ASTReader;
1218 friend class ASTDeclReader;
1219 friend class ASTDeclWriter;
1222 /// ObjCIvarDecl - Represents an ObjC instance variable. In general, ObjC
1223 /// instance variables are identical to C. The only exception is Objective-C
1224 /// supports C++ style access control. For example:
1226 /// \@interface IvarExample : NSObject
1228 /// id defaultToProtected;
1230 /// id canBePublic; // same as C++.
1232 /// id canBeProtected; // same as C++.
1234 /// id canBePackage; // framework visibility (not available in C++).
1237 class ObjCIvarDecl : public FieldDecl {
1238 virtual void anchor();
1241 enum AccessControl {
1242 None, Private, Protected, Public, Package
1246 ObjCIvarDecl(ObjCContainerDecl *DC, SourceLocation StartLoc,
1247 SourceLocation IdLoc, IdentifierInfo *Id,
1248 QualType T, TypeSourceInfo *TInfo, AccessControl ac, Expr *BW,
1250 bool backingIvarReferencedInAccessor)
1251 : FieldDecl(ObjCIvar, DC, StartLoc, IdLoc, Id, T, TInfo, BW,
1252 /*Mutable=*/false, /*HasInit=*/ICIS_NoInit),
1253 NextIvar(0), DeclAccess(ac), Synthesized(synthesized),
1254 BackingIvarReferencedInAccessor(backingIvarReferencedInAccessor) {}
1257 static ObjCIvarDecl *Create(ASTContext &C, ObjCContainerDecl *DC,
1258 SourceLocation StartLoc, SourceLocation IdLoc,
1259 IdentifierInfo *Id, QualType T,
1260 TypeSourceInfo *TInfo,
1261 AccessControl ac, Expr *BW = NULL,
1262 bool synthesized=false,
1263 bool backingIvarReferencedInAccessor=false);
1265 static ObjCIvarDecl *CreateDeserialized(ASTContext &C, unsigned ID);
1267 /// \brief Return the class interface that this ivar is logically contained
1268 /// in; this is either the interface where the ivar was declared, or the
1269 /// interface the ivar is conceptually a part of in the case of synthesized
1271 const ObjCInterfaceDecl *getContainingInterface() const;
1273 ObjCIvarDecl *getNextIvar() { return NextIvar; }
1274 const ObjCIvarDecl *getNextIvar() const { return NextIvar; }
1275 void setNextIvar(ObjCIvarDecl *ivar) { NextIvar = ivar; }
1277 void setAccessControl(AccessControl ac) { DeclAccess = ac; }
1279 AccessControl getAccessControl() const { return AccessControl(DeclAccess); }
1281 AccessControl getCanonicalAccessControl() const {
1282 return DeclAccess == None ? Protected : AccessControl(DeclAccess);
1285 void setBackingIvarReferencedInAccessor(bool val) {
1286 BackingIvarReferencedInAccessor = val;
1288 bool getBackingIvarReferencedInAccessor() const {
1289 return BackingIvarReferencedInAccessor;
1292 void setSynthesize(bool synth) { Synthesized = synth; }
1293 bool getSynthesize() const { return Synthesized; }
1295 // Implement isa/cast/dyncast/etc.
1296 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1297 static bool classofKind(Kind K) { return K == ObjCIvar; }
1299 /// NextIvar - Next Ivar in the list of ivars declared in class; class's
1300 /// extensions and class's implementation
1301 ObjCIvarDecl *NextIvar;
1303 // NOTE: VC++ treats enums as signed, avoid using the AccessControl enum
1304 unsigned DeclAccess : 3;
1305 unsigned Synthesized : 1;
1306 unsigned BackingIvarReferencedInAccessor : 1;
1310 /// \brief Represents a field declaration created by an \@defs(...).
1311 class ObjCAtDefsFieldDecl : public FieldDecl {
1312 virtual void anchor();
1313 ObjCAtDefsFieldDecl(DeclContext *DC, SourceLocation StartLoc,
1314 SourceLocation IdLoc, IdentifierInfo *Id,
1315 QualType T, Expr *BW)
1316 : FieldDecl(ObjCAtDefsField, DC, StartLoc, IdLoc, Id, T,
1317 /*TInfo=*/0, // FIXME: Do ObjCAtDefs have declarators ?
1318 BW, /*Mutable=*/false, /*HasInit=*/ICIS_NoInit) {}
1321 static ObjCAtDefsFieldDecl *Create(ASTContext &C, DeclContext *DC,
1322 SourceLocation StartLoc,
1323 SourceLocation IdLoc, IdentifierInfo *Id,
1324 QualType T, Expr *BW);
1326 static ObjCAtDefsFieldDecl *CreateDeserialized(ASTContext &C, unsigned ID);
1328 // Implement isa/cast/dyncast/etc.
1329 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1330 static bool classofKind(Kind K) { return K == ObjCAtDefsField; }
1333 /// \brief Represents an Objective-C protocol declaration.
1335 /// Objective-C protocols declare a pure abstract type (i.e., no instance
1336 /// variables are permitted). Protocols originally drew inspiration from
1337 /// C++ pure virtual functions (a C++ feature with nice semantics and lousy
1338 /// syntax:-). Here is an example:
1341 /// \@protocol NSDraggingInfo <refproto1, refproto2>
1342 /// - (NSWindow *)draggingDestinationWindow;
1343 /// - (NSImage *)draggedImage;
1347 /// This says that NSDraggingInfo requires two methods and requires everything
1348 /// that the two "referenced protocols" 'refproto1' and 'refproto2' require as
1352 /// \@interface ImplementsNSDraggingInfo : NSObject \<NSDraggingInfo>
1356 /// ObjC protocols inspired Java interfaces. Unlike Java, ObjC classes and
1357 /// protocols are in distinct namespaces. For example, Cocoa defines both
1358 /// an NSObject protocol and class (which isn't allowed in Java). As a result,
1359 /// protocols are referenced using angle brackets as follows:
1361 /// id \<NSDraggingInfo> anyObjectThatImplementsNSDraggingInfo;
1363 class ObjCProtocolDecl : public ObjCContainerDecl,
1364 public Redeclarable<ObjCProtocolDecl> {
1365 virtual void anchor();
1367 struct DefinitionData {
1368 // \brief The declaration that defines this protocol.
1369 ObjCProtocolDecl *Definition;
1371 /// \brief Referenced protocols
1372 ObjCProtocolList ReferencedProtocols;
1375 /// \brief Contains a pointer to the data associated with this class,
1376 /// which will be NULL if this class has not yet been defined.
1378 /// The bit indicates when we don't need to check for out-of-date
1379 /// declarations. It will be set unless modules are enabled.
1380 llvm::PointerIntPair<DefinitionData *, 1, bool> Data;
1382 DefinitionData &data() const {
1383 assert(Data.getPointer() && "Objective-C protocol has no definition!");
1384 return *Data.getPointer();
1387 ObjCProtocolDecl(DeclContext *DC, IdentifierInfo *Id,
1388 SourceLocation nameLoc, SourceLocation atStartLoc,
1389 ObjCProtocolDecl *PrevDecl);
1391 void allocateDefinitionData();
1393 typedef Redeclarable<ObjCProtocolDecl> redeclarable_base;
1394 virtual ObjCProtocolDecl *getNextRedeclaration() {
1395 return RedeclLink.getNext();
1397 virtual ObjCProtocolDecl *getPreviousDeclImpl() {
1398 return getPreviousDecl();
1400 virtual ObjCProtocolDecl *getMostRecentDeclImpl() {
1401 return getMostRecentDecl();
1405 static ObjCProtocolDecl *Create(ASTContext &C, DeclContext *DC,
1407 SourceLocation nameLoc,
1408 SourceLocation atStartLoc,
1409 ObjCProtocolDecl *PrevDecl);
1411 static ObjCProtocolDecl *CreateDeserialized(ASTContext &C, unsigned ID);
1413 const ObjCProtocolList &getReferencedProtocols() const {
1414 assert(hasDefinition() && "No definition available!");
1415 return data().ReferencedProtocols;
1417 typedef ObjCProtocolList::iterator protocol_iterator;
1418 protocol_iterator protocol_begin() const {
1419 if (!hasDefinition())
1420 return protocol_iterator();
1422 return data().ReferencedProtocols.begin();
1424 protocol_iterator protocol_end() const {
1425 if (!hasDefinition())
1426 return protocol_iterator();
1428 return data().ReferencedProtocols.end();
1430 typedef ObjCProtocolList::loc_iterator protocol_loc_iterator;
1431 protocol_loc_iterator protocol_loc_begin() const {
1432 if (!hasDefinition())
1433 return protocol_loc_iterator();
1435 return data().ReferencedProtocols.loc_begin();
1437 protocol_loc_iterator protocol_loc_end() const {
1438 if (!hasDefinition())
1439 return protocol_loc_iterator();
1441 return data().ReferencedProtocols.loc_end();
1443 unsigned protocol_size() const {
1444 if (!hasDefinition())
1447 return data().ReferencedProtocols.size();
1450 /// setProtocolList - Set the list of protocols that this interface
1452 void setProtocolList(ObjCProtocolDecl *const*List, unsigned Num,
1453 const SourceLocation *Locs, ASTContext &C) {
1454 assert(hasDefinition() && "Protocol is not defined");
1455 data().ReferencedProtocols.set(List, Num, Locs, C);
1458 ObjCProtocolDecl *lookupProtocolNamed(IdentifierInfo *PName);
1460 // Lookup a method. First, we search locally. If a method isn't
1461 // found, we search referenced protocols and class categories.
1462 ObjCMethodDecl *lookupMethod(Selector Sel, bool isInstance) const;
1463 ObjCMethodDecl *lookupInstanceMethod(Selector Sel) const {
1464 return lookupMethod(Sel, true/*isInstance*/);
1466 ObjCMethodDecl *lookupClassMethod(Selector Sel) const {
1467 return lookupMethod(Sel, false/*isInstance*/);
1470 /// \brief Determine whether this protocol has a definition.
1471 bool hasDefinition() const {
1472 // If the name of this protocol is out-of-date, bring it up-to-date, which
1473 // might bring in a definition.
1474 // Note: a null value indicates that we don't have a definition and that
1475 // modules are enabled.
1476 if (!Data.getOpaqueValue()) {
1477 if (IdentifierInfo *II = getIdentifier()) {
1478 if (II->isOutOfDate()) {
1479 updateOutOfDate(*II);
1484 return Data.getPointer();
1487 /// \brief Retrieve the definition of this protocol, if any.
1488 ObjCProtocolDecl *getDefinition() {
1489 return hasDefinition()? Data.getPointer()->Definition : 0;
1492 /// \brief Retrieve the definition of this protocol, if any.
1493 const ObjCProtocolDecl *getDefinition() const {
1494 return hasDefinition()? Data.getPointer()->Definition : 0;
1497 /// \brief Determine whether this particular declaration is also the
1499 bool isThisDeclarationADefinition() const {
1500 return getDefinition() == this;
1503 /// \brief Starts the definition of this Objective-C protocol.
1504 void startDefinition();
1506 virtual SourceRange getSourceRange() const LLVM_READONLY {
1507 if (isThisDeclarationADefinition())
1508 return ObjCContainerDecl::getSourceRange();
1510 return SourceRange(getAtStartLoc(), getLocation());
1513 typedef redeclarable_base::redecl_iterator redecl_iterator;
1514 using redeclarable_base::redecls_begin;
1515 using redeclarable_base::redecls_end;
1516 using redeclarable_base::getPreviousDecl;
1517 using redeclarable_base::getMostRecentDecl;
1518 using redeclarable_base::isFirstDecl;
1520 /// Retrieves the canonical declaration of this Objective-C protocol.
1521 ObjCProtocolDecl *getCanonicalDecl() { return getFirstDecl(); }
1522 const ObjCProtocolDecl *getCanonicalDecl() const { return getFirstDecl(); }
1524 virtual void collectPropertiesToImplement(PropertyMap &PM,
1525 PropertyDeclOrder &PO) const;
1527 void collectInheritedProtocolProperties(const ObjCPropertyDecl *Property,
1528 ProtocolPropertyMap &PM) const;
1530 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1531 static bool classofKind(Kind K) { return K == ObjCProtocol; }
1533 friend class ASTReader;
1534 friend class ASTDeclReader;
1535 friend class ASTDeclWriter;
1538 /// ObjCCategoryDecl - Represents a category declaration. A category allows
1539 /// you to add methods to an existing class (without subclassing or modifying
1540 /// the original class interface or implementation:-). Categories don't allow
1541 /// you to add instance data. The following example adds "myMethod" to all
1542 /// NSView's within a process:
1544 /// \@interface NSView (MyViewMethods)
1548 /// Categories also allow you to split the implementation of a class across
1549 /// several files (a feature more naturally supported in C++).
1551 /// Categories were originally inspired by dynamic languages such as Common
1552 /// Lisp and Smalltalk. More traditional class-based languages (C++, Java)
1553 /// don't support this level of dynamism, which is both powerful and dangerous.
1555 class ObjCCategoryDecl : public ObjCContainerDecl {
1556 virtual void anchor();
1558 /// Interface belonging to this category
1559 ObjCInterfaceDecl *ClassInterface;
1561 /// referenced protocols in this category.
1562 ObjCProtocolList ReferencedProtocols;
1564 /// Next category belonging to this class.
1565 /// FIXME: this should not be a singly-linked list. Move storage elsewhere.
1566 ObjCCategoryDecl *NextClassCategory;
1568 /// \brief The location of the category name in this declaration.
1569 SourceLocation CategoryNameLoc;
1571 /// class extension may have private ivars.
1572 SourceLocation IvarLBraceLoc;
1573 SourceLocation IvarRBraceLoc;
1575 ObjCCategoryDecl(DeclContext *DC, SourceLocation AtLoc,
1576 SourceLocation ClassNameLoc, SourceLocation CategoryNameLoc,
1577 IdentifierInfo *Id, ObjCInterfaceDecl *IDecl,
1578 SourceLocation IvarLBraceLoc=SourceLocation(),
1579 SourceLocation IvarRBraceLoc=SourceLocation())
1580 : ObjCContainerDecl(ObjCCategory, DC, Id, ClassNameLoc, AtLoc),
1581 ClassInterface(IDecl), NextClassCategory(0),
1582 CategoryNameLoc(CategoryNameLoc),
1583 IvarLBraceLoc(IvarLBraceLoc), IvarRBraceLoc(IvarRBraceLoc) {
1588 static ObjCCategoryDecl *Create(ASTContext &C, DeclContext *DC,
1589 SourceLocation AtLoc,
1590 SourceLocation ClassNameLoc,
1591 SourceLocation CategoryNameLoc,
1593 ObjCInterfaceDecl *IDecl,
1594 SourceLocation IvarLBraceLoc=SourceLocation(),
1595 SourceLocation IvarRBraceLoc=SourceLocation());
1596 static ObjCCategoryDecl *CreateDeserialized(ASTContext &C, unsigned ID);
1598 ObjCInterfaceDecl *getClassInterface() { return ClassInterface; }
1599 const ObjCInterfaceDecl *getClassInterface() const { return ClassInterface; }
1601 ObjCCategoryImplDecl *getImplementation() const;
1602 void setImplementation(ObjCCategoryImplDecl *ImplD);
1604 /// setProtocolList - Set the list of protocols that this interface
1606 void setProtocolList(ObjCProtocolDecl *const*List, unsigned Num,
1607 const SourceLocation *Locs, ASTContext &C) {
1608 ReferencedProtocols.set(List, Num, Locs, C);
1611 const ObjCProtocolList &getReferencedProtocols() const {
1612 return ReferencedProtocols;
1615 typedef ObjCProtocolList::iterator protocol_iterator;
1616 protocol_iterator protocol_begin() const {return ReferencedProtocols.begin();}
1617 protocol_iterator protocol_end() const { return ReferencedProtocols.end(); }
1618 unsigned protocol_size() const { return ReferencedProtocols.size(); }
1619 typedef ObjCProtocolList::loc_iterator protocol_loc_iterator;
1620 protocol_loc_iterator protocol_loc_begin() const {
1621 return ReferencedProtocols.loc_begin();
1623 protocol_loc_iterator protocol_loc_end() const {
1624 return ReferencedProtocols.loc_end();
1627 ObjCCategoryDecl *getNextClassCategory() const { return NextClassCategory; }
1629 /// \brief Retrieve the pointer to the next stored category (or extension),
1630 /// which may be hidden.
1631 ObjCCategoryDecl *getNextClassCategoryRaw() const {
1632 return NextClassCategory;
1635 bool IsClassExtension() const { return getIdentifier() == 0; }
1637 typedef specific_decl_iterator<ObjCIvarDecl> ivar_iterator;
1638 ivar_iterator ivar_begin() const {
1639 return ivar_iterator(decls_begin());
1641 ivar_iterator ivar_end() const {
1642 return ivar_iterator(decls_end());
1644 unsigned ivar_size() const {
1645 return std::distance(ivar_begin(), ivar_end());
1647 bool ivar_empty() const {
1648 return ivar_begin() == ivar_end();
1651 SourceLocation getCategoryNameLoc() const { return CategoryNameLoc; }
1652 void setCategoryNameLoc(SourceLocation Loc) { CategoryNameLoc = Loc; }
1654 void setIvarLBraceLoc(SourceLocation Loc) { IvarLBraceLoc = Loc; }
1655 SourceLocation getIvarLBraceLoc() const { return IvarLBraceLoc; }
1656 void setIvarRBraceLoc(SourceLocation Loc) { IvarRBraceLoc = Loc; }
1657 SourceLocation getIvarRBraceLoc() const { return IvarRBraceLoc; }
1659 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1660 static bool classofKind(Kind K) { return K == ObjCCategory; }
1662 friend class ASTDeclReader;
1663 friend class ASTDeclWriter;
1666 class ObjCImplDecl : public ObjCContainerDecl {
1667 virtual void anchor();
1669 /// Class interface for this class/category implementation
1670 ObjCInterfaceDecl *ClassInterface;
1673 ObjCImplDecl(Kind DK, DeclContext *DC,
1674 ObjCInterfaceDecl *classInterface,
1675 SourceLocation nameLoc, SourceLocation atStartLoc)
1676 : ObjCContainerDecl(DK, DC,
1677 classInterface? classInterface->getIdentifier() : 0,
1678 nameLoc, atStartLoc),
1679 ClassInterface(classInterface) {}
1682 const ObjCInterfaceDecl *getClassInterface() const { return ClassInterface; }
1683 ObjCInterfaceDecl *getClassInterface() { return ClassInterface; }
1684 void setClassInterface(ObjCInterfaceDecl *IFace);
1686 void addInstanceMethod(ObjCMethodDecl *method) {
1687 // FIXME: Context should be set correctly before we get here.
1688 method->setLexicalDeclContext(this);
1691 void addClassMethod(ObjCMethodDecl *method) {
1692 // FIXME: Context should be set correctly before we get here.
1693 method->setLexicalDeclContext(this);
1697 void addPropertyImplementation(ObjCPropertyImplDecl *property);
1699 ObjCPropertyImplDecl *FindPropertyImplDecl(IdentifierInfo *propertyId) const;
1700 ObjCPropertyImplDecl *FindPropertyImplIvarDecl(IdentifierInfo *ivarId) const;
1702 // Iterator access to properties.
1703 typedef specific_decl_iterator<ObjCPropertyImplDecl> propimpl_iterator;
1704 propimpl_iterator propimpl_begin() const {
1705 return propimpl_iterator(decls_begin());
1707 propimpl_iterator propimpl_end() const {
1708 return propimpl_iterator(decls_end());
1711 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1712 static bool classofKind(Kind K) {
1713 return K >= firstObjCImpl && K <= lastObjCImpl;
1717 /// ObjCCategoryImplDecl - An object of this class encapsulates a category
1718 /// \@implementation declaration. If a category class has declaration of a
1719 /// property, its implementation must be specified in the category's
1720 /// \@implementation declaration. Example:
1721 /// \@interface I \@end
1722 /// \@interface I(CATEGORY)
1723 /// \@property int p1, d1;
1725 /// \@implementation I(CATEGORY)
1726 /// \@dynamic p1,d1;
1729 /// ObjCCategoryImplDecl
1730 class ObjCCategoryImplDecl : public ObjCImplDecl {
1731 virtual void anchor();
1736 // Category name location
1737 SourceLocation CategoryNameLoc;
1739 ObjCCategoryImplDecl(DeclContext *DC, IdentifierInfo *Id,
1740 ObjCInterfaceDecl *classInterface,
1741 SourceLocation nameLoc, SourceLocation atStartLoc,
1742 SourceLocation CategoryNameLoc)
1743 : ObjCImplDecl(ObjCCategoryImpl, DC, classInterface, nameLoc, atStartLoc),
1744 Id(Id), CategoryNameLoc(CategoryNameLoc) {}
1746 static ObjCCategoryImplDecl *Create(ASTContext &C, DeclContext *DC,
1748 ObjCInterfaceDecl *classInterface,
1749 SourceLocation nameLoc,
1750 SourceLocation atStartLoc,
1751 SourceLocation CategoryNameLoc);
1752 static ObjCCategoryImplDecl *CreateDeserialized(ASTContext &C, unsigned ID);
1754 /// getIdentifier - Get the identifier that names the category
1755 /// interface associated with this implementation.
1756 /// FIXME: This is a bad API, we are overriding the NamedDecl::getIdentifier()
1757 /// to mean something different. For example:
1758 /// ((NamedDecl *)SomeCategoryImplDecl)->getIdentifier()
1759 /// returns the class interface name, whereas
1760 /// ((ObjCCategoryImplDecl *)SomeCategoryImplDecl)->getIdentifier()
1761 /// returns the category name.
1762 IdentifierInfo *getIdentifier() const {
1765 void setIdentifier(IdentifierInfo *II) { Id = II; }
1767 ObjCCategoryDecl *getCategoryDecl() const;
1769 SourceLocation getCategoryNameLoc() const { return CategoryNameLoc; }
1771 /// getName - Get the name of identifier for the class interface associated
1772 /// with this implementation as a StringRef.
1774 // FIXME: This is a bad API, we are overriding the NamedDecl::getName, to mean
1775 // something different.
1776 StringRef getName() const {
1777 return Id ? Id->getNameStart() : "";
1780 /// @brief Get the name of the class associated with this interface.
1782 // FIXME: Deprecated, move clients to getName().
1783 std::string getNameAsString() const {
1787 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1788 static bool classofKind(Kind K) { return K == ObjCCategoryImpl;}
1790 friend class ASTDeclReader;
1791 friend class ASTDeclWriter;
1794 raw_ostream &operator<<(raw_ostream &OS, const ObjCCategoryImplDecl &CID);
1796 /// ObjCImplementationDecl - Represents a class definition - this is where
1797 /// method definitions are specified. For example:
1800 /// \@implementation MyClass
1801 /// - (void)myMethod { /* do something */ }
1805 /// Typically, instance variables are specified in the class interface,
1806 /// *not* in the implementation. Nevertheless (for legacy reasons), we
1807 /// allow instance variables to be specified in the implementation. When
1808 /// specified, they need to be *identical* to the interface.
1810 class ObjCImplementationDecl : public ObjCImplDecl {
1811 virtual void anchor();
1812 /// Implementation Class's super class.
1813 ObjCInterfaceDecl *SuperClass;
1814 SourceLocation SuperLoc;
1816 /// \@implementation may have private ivars.
1817 SourceLocation IvarLBraceLoc;
1818 SourceLocation IvarRBraceLoc;
1820 /// Support for ivar initialization.
1821 /// IvarInitializers - The arguments used to initialize the ivars
1822 CXXCtorInitializer **IvarInitializers;
1823 unsigned NumIvarInitializers;
1825 /// Do the ivars of this class require initialization other than
1826 /// zero-initialization?
1827 bool HasNonZeroConstructors : 1;
1829 /// Do the ivars of this class require non-trivial destruction?
1830 bool HasDestructors : 1;
1832 ObjCImplementationDecl(DeclContext *DC,
1833 ObjCInterfaceDecl *classInterface,
1834 ObjCInterfaceDecl *superDecl,
1835 SourceLocation nameLoc, SourceLocation atStartLoc,
1836 SourceLocation superLoc = SourceLocation(),
1837 SourceLocation IvarLBraceLoc=SourceLocation(),
1838 SourceLocation IvarRBraceLoc=SourceLocation())
1839 : ObjCImplDecl(ObjCImplementation, DC, classInterface, nameLoc, atStartLoc),
1840 SuperClass(superDecl), SuperLoc(superLoc), IvarLBraceLoc(IvarLBraceLoc),
1841 IvarRBraceLoc(IvarRBraceLoc),
1842 IvarInitializers(0), NumIvarInitializers(0),
1843 HasNonZeroConstructors(false), HasDestructors(false) {}
1845 static ObjCImplementationDecl *Create(ASTContext &C, DeclContext *DC,
1846 ObjCInterfaceDecl *classInterface,
1847 ObjCInterfaceDecl *superDecl,
1848 SourceLocation nameLoc,
1849 SourceLocation atStartLoc,
1850 SourceLocation superLoc = SourceLocation(),
1851 SourceLocation IvarLBraceLoc=SourceLocation(),
1852 SourceLocation IvarRBraceLoc=SourceLocation());
1854 static ObjCImplementationDecl *CreateDeserialized(ASTContext &C, unsigned ID);
1856 /// init_iterator - Iterates through the ivar initializer list.
1857 typedef CXXCtorInitializer **init_iterator;
1859 /// init_const_iterator - Iterates through the ivar initializer list.
1860 typedef CXXCtorInitializer * const * init_const_iterator;
1862 /// init_begin() - Retrieve an iterator to the first initializer.
1863 init_iterator init_begin() { return IvarInitializers; }
1864 /// begin() - Retrieve an iterator to the first initializer.
1865 init_const_iterator init_begin() const { return IvarInitializers; }
1867 /// init_end() - Retrieve an iterator past the last initializer.
1868 init_iterator init_end() {
1869 return IvarInitializers + NumIvarInitializers;
1871 /// end() - Retrieve an iterator past the last initializer.
1872 init_const_iterator init_end() const {
1873 return IvarInitializers + NumIvarInitializers;
1875 /// getNumArgs - Number of ivars which must be initialized.
1876 unsigned getNumIvarInitializers() const {
1877 return NumIvarInitializers;
1880 void setNumIvarInitializers(unsigned numNumIvarInitializers) {
1881 NumIvarInitializers = numNumIvarInitializers;
1884 void setIvarInitializers(ASTContext &C,
1885 CXXCtorInitializer ** initializers,
1886 unsigned numInitializers);
1888 /// Do any of the ivars of this class (not counting its base classes)
1889 /// require construction other than zero-initialization?
1890 bool hasNonZeroConstructors() const { return HasNonZeroConstructors; }
1891 void setHasNonZeroConstructors(bool val) { HasNonZeroConstructors = val; }
1893 /// Do any of the ivars of this class (not counting its base classes)
1894 /// require non-trivial destruction?
1895 bool hasDestructors() const { return HasDestructors; }
1896 void setHasDestructors(bool val) { HasDestructors = val; }
1898 /// getIdentifier - Get the identifier that names the class
1899 /// interface associated with this implementation.
1900 IdentifierInfo *getIdentifier() const {
1901 return getClassInterface()->getIdentifier();
1904 /// getName - Get the name of identifier for the class interface associated
1905 /// with this implementation as a StringRef.
1907 // FIXME: This is a bad API, we are overriding the NamedDecl::getName, to mean
1908 // something different.
1909 StringRef getName() const {
1910 assert(getIdentifier() && "Name is not a simple identifier");
1911 return getIdentifier()->getName();
1914 /// @brief Get the name of the class associated with this interface.
1916 // FIXME: Move to StringRef API.
1917 std::string getNameAsString() const {
1921 const ObjCInterfaceDecl *getSuperClass() const { return SuperClass; }
1922 ObjCInterfaceDecl *getSuperClass() { return SuperClass; }
1923 SourceLocation getSuperClassLoc() const { return SuperLoc; }
1925 void setSuperClass(ObjCInterfaceDecl * superCls) { SuperClass = superCls; }
1927 void setIvarLBraceLoc(SourceLocation Loc) { IvarLBraceLoc = Loc; }
1928 SourceLocation getIvarLBraceLoc() const { return IvarLBraceLoc; }
1929 void setIvarRBraceLoc(SourceLocation Loc) { IvarRBraceLoc = Loc; }
1930 SourceLocation getIvarRBraceLoc() const { return IvarRBraceLoc; }
1932 typedef specific_decl_iterator<ObjCIvarDecl> ivar_iterator;
1933 ivar_iterator ivar_begin() const {
1934 return ivar_iterator(decls_begin());
1936 ivar_iterator ivar_end() const {
1937 return ivar_iterator(decls_end());
1939 unsigned ivar_size() const {
1940 return std::distance(ivar_begin(), ivar_end());
1942 bool ivar_empty() const {
1943 return ivar_begin() == ivar_end();
1946 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1947 static bool classofKind(Kind K) { return K == ObjCImplementation; }
1949 friend class ASTDeclReader;
1950 friend class ASTDeclWriter;
1953 raw_ostream &operator<<(raw_ostream &OS, const ObjCImplementationDecl &ID);
1955 /// ObjCCompatibleAliasDecl - Represents alias of a class. This alias is
1956 /// declared as \@compatibility_alias alias class.
1957 class ObjCCompatibleAliasDecl : public NamedDecl {
1958 virtual void anchor();
1959 /// Class that this is an alias of.
1960 ObjCInterfaceDecl *AliasedClass;
1962 ObjCCompatibleAliasDecl(DeclContext *DC, SourceLocation L, IdentifierInfo *Id,
1963 ObjCInterfaceDecl* aliasedClass)
1964 : NamedDecl(ObjCCompatibleAlias, DC, L, Id), AliasedClass(aliasedClass) {}
1966 static ObjCCompatibleAliasDecl *Create(ASTContext &C, DeclContext *DC,
1967 SourceLocation L, IdentifierInfo *Id,
1968 ObjCInterfaceDecl* aliasedClass);
1970 static ObjCCompatibleAliasDecl *CreateDeserialized(ASTContext &C,
1973 const ObjCInterfaceDecl *getClassInterface() const { return AliasedClass; }
1974 ObjCInterfaceDecl *getClassInterface() { return AliasedClass; }
1975 void setClassInterface(ObjCInterfaceDecl *D) { AliasedClass = D; }
1977 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1978 static bool classofKind(Kind K) { return K == ObjCCompatibleAlias; }
1982 /// \brief Represents one property declaration in an Objective-C interface.
1986 /// \@property (assign, readwrite) int MyProperty;
1988 class ObjCPropertyDecl : public NamedDecl {
1989 virtual void anchor();
1991 enum PropertyAttributeKind {
1992 OBJC_PR_noattr = 0x00,
1993 OBJC_PR_readonly = 0x01,
1994 OBJC_PR_getter = 0x02,
1995 OBJC_PR_assign = 0x04,
1996 OBJC_PR_readwrite = 0x08,
1997 OBJC_PR_retain = 0x10,
1998 OBJC_PR_copy = 0x20,
1999 OBJC_PR_nonatomic = 0x40,
2000 OBJC_PR_setter = 0x80,
2001 OBJC_PR_atomic = 0x100,
2002 OBJC_PR_weak = 0x200,
2003 OBJC_PR_strong = 0x400,
2004 OBJC_PR_unsafe_unretained = 0x800
2005 // Adding a property should change NumPropertyAttrsBits
2009 /// \brief Number of bits fitting all the property attributes.
2010 NumPropertyAttrsBits = 12
2013 enum SetterKind { Assign, Retain, Copy, Weak };
2014 enum PropertyControl { None, Required, Optional };
2016 SourceLocation AtLoc; // location of \@property
2017 SourceLocation LParenLoc; // location of '(' starting attribute list or null.
2018 TypeSourceInfo *DeclType;
2019 unsigned PropertyAttributes : NumPropertyAttrsBits;
2020 unsigned PropertyAttributesAsWritten : NumPropertyAttrsBits;
2021 // \@required/\@optional
2022 unsigned PropertyImplementation : 2;
2024 Selector GetterName; // getter name of NULL if no getter
2025 Selector SetterName; // setter name of NULL if no setter
2027 ObjCMethodDecl *GetterMethodDecl; // Declaration of getter instance method
2028 ObjCMethodDecl *SetterMethodDecl; // Declaration of setter instance method
2029 ObjCIvarDecl *PropertyIvarDecl; // Synthesize ivar for this property
2031 ObjCPropertyDecl(DeclContext *DC, SourceLocation L, IdentifierInfo *Id,
2032 SourceLocation AtLocation, SourceLocation LParenLocation,
2034 : NamedDecl(ObjCProperty, DC, L, Id), AtLoc(AtLocation),
2035 LParenLoc(LParenLocation), DeclType(T),
2036 PropertyAttributes(OBJC_PR_noattr),
2037 PropertyAttributesAsWritten(OBJC_PR_noattr),
2038 PropertyImplementation(None),
2039 GetterName(Selector()),
2040 SetterName(Selector()),
2041 GetterMethodDecl(0), SetterMethodDecl(0) , PropertyIvarDecl(0) {}
2043 static ObjCPropertyDecl *Create(ASTContext &C, DeclContext *DC,
2045 IdentifierInfo *Id, SourceLocation AtLocation,
2046 SourceLocation LParenLocation,
2048 PropertyControl propControl = None);
2050 static ObjCPropertyDecl *CreateDeserialized(ASTContext &C, unsigned ID);
2052 SourceLocation getAtLoc() const { return AtLoc; }
2053 void setAtLoc(SourceLocation L) { AtLoc = L; }
2055 SourceLocation getLParenLoc() const { return LParenLoc; }
2056 void setLParenLoc(SourceLocation L) { LParenLoc = L; }
2058 TypeSourceInfo *getTypeSourceInfo() const { return DeclType; }
2059 QualType getType() const { return DeclType->getType(); }
2060 void setType(TypeSourceInfo *T) { DeclType = T; }
2062 PropertyAttributeKind getPropertyAttributes() const {
2063 return PropertyAttributeKind(PropertyAttributes);
2065 void setPropertyAttributes(PropertyAttributeKind PRVal) {
2066 PropertyAttributes |= PRVal;
2069 PropertyAttributeKind getPropertyAttributesAsWritten() const {
2070 return PropertyAttributeKind(PropertyAttributesAsWritten);
2073 bool hasWrittenStorageAttribute() const {
2074 return PropertyAttributesAsWritten & (OBJC_PR_assign | OBJC_PR_copy |
2075 OBJC_PR_unsafe_unretained | OBJC_PR_retain | OBJC_PR_strong |
2079 void setPropertyAttributesAsWritten(PropertyAttributeKind PRVal) {
2080 PropertyAttributesAsWritten = PRVal;
2083 void makeitReadWriteAttribute() {
2084 PropertyAttributes &= ~OBJC_PR_readonly;
2085 PropertyAttributes |= OBJC_PR_readwrite;
2088 // Helper methods for accessing attributes.
2090 /// isReadOnly - Return true iff the property has a setter.
2091 bool isReadOnly() const {
2092 return (PropertyAttributes & OBJC_PR_readonly);
2095 /// isAtomic - Return true if the property is atomic.
2096 bool isAtomic() const {
2097 return (PropertyAttributes & OBJC_PR_atomic);
2100 /// isRetaining - Return true if the property retains its value.
2101 bool isRetaining() const {
2102 return (PropertyAttributes &
2103 (OBJC_PR_retain | OBJC_PR_strong | OBJC_PR_copy));
2106 /// getSetterKind - Return the method used for doing assignment in
2107 /// the property setter. This is only valid if the property has been
2108 /// defined to have a setter.
2109 SetterKind getSetterKind() const {
2110 if (PropertyAttributes & OBJC_PR_strong)
2111 return getType()->isBlockPointerType() ? Copy : Retain;
2112 if (PropertyAttributes & OBJC_PR_retain)
2114 if (PropertyAttributes & OBJC_PR_copy)
2116 if (PropertyAttributes & OBJC_PR_weak)
2121 Selector getGetterName() const { return GetterName; }
2122 void setGetterName(Selector Sel) { GetterName = Sel; }
2124 Selector getSetterName() const { return SetterName; }
2125 void setSetterName(Selector Sel) { SetterName = Sel; }
2127 ObjCMethodDecl *getGetterMethodDecl() const { return GetterMethodDecl; }
2128 void setGetterMethodDecl(ObjCMethodDecl *gDecl) { GetterMethodDecl = gDecl; }
2130 ObjCMethodDecl *getSetterMethodDecl() const { return SetterMethodDecl; }
2131 void setSetterMethodDecl(ObjCMethodDecl *gDecl) { SetterMethodDecl = gDecl; }
2133 // Related to \@optional/\@required declared in \@protocol
2134 void setPropertyImplementation(PropertyControl pc) {
2135 PropertyImplementation = pc;
2137 PropertyControl getPropertyImplementation() const {
2138 return PropertyControl(PropertyImplementation);
2141 void setPropertyIvarDecl(ObjCIvarDecl *Ivar) {
2142 PropertyIvarDecl = Ivar;
2144 ObjCIvarDecl *getPropertyIvarDecl() const {
2145 return PropertyIvarDecl;
2148 virtual SourceRange getSourceRange() const LLVM_READONLY {
2149 return SourceRange(AtLoc, getLocation());
2152 /// Get the default name of the synthesized ivar.
2153 IdentifierInfo *getDefaultSynthIvarName(ASTContext &Ctx) const;
2155 /// Lookup a property by name in the specified DeclContext.
2156 static ObjCPropertyDecl *findPropertyDecl(const DeclContext *DC,
2157 IdentifierInfo *propertyID);
2159 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
2160 static bool classofKind(Kind K) { return K == ObjCProperty; }
2163 /// ObjCPropertyImplDecl - Represents implementation declaration of a property
2164 /// in a class or category implementation block. For example:
2165 /// \@synthesize prop1 = ivar1;
2167 class ObjCPropertyImplDecl : public Decl {
2174 SourceLocation AtLoc; // location of \@synthesize or \@dynamic
2176 /// \brief For \@synthesize, the location of the ivar, if it was written in
2177 /// the source code.
2180 /// \@synthesize int a = b
2182 SourceLocation IvarLoc;
2184 /// Property declaration being implemented
2185 ObjCPropertyDecl *PropertyDecl;
2187 /// Null for \@dynamic. Required for \@synthesize.
2188 ObjCIvarDecl *PropertyIvarDecl;
2190 /// Null for \@dynamic. Non-null if property must be copy-constructed in
2192 Expr *GetterCXXConstructor;
2194 /// Null for \@dynamic. Non-null if property has assignment operator to call
2195 /// in Setter synthesis.
2196 Expr *SetterCXXAssignment;
2198 ObjCPropertyImplDecl(DeclContext *DC, SourceLocation atLoc, SourceLocation L,
2199 ObjCPropertyDecl *property,
2201 ObjCIvarDecl *ivarDecl,
2202 SourceLocation ivarLoc)
2203 : Decl(ObjCPropertyImpl, DC, L), AtLoc(atLoc),
2204 IvarLoc(ivarLoc), PropertyDecl(property), PropertyIvarDecl(ivarDecl),
2205 GetterCXXConstructor(0), SetterCXXAssignment(0) {
2206 assert (PK == Dynamic || PropertyIvarDecl);
2210 static ObjCPropertyImplDecl *Create(ASTContext &C, DeclContext *DC,
2211 SourceLocation atLoc, SourceLocation L,
2212 ObjCPropertyDecl *property,
2214 ObjCIvarDecl *ivarDecl,
2215 SourceLocation ivarLoc);
2217 static ObjCPropertyImplDecl *CreateDeserialized(ASTContext &C, unsigned ID);
2219 virtual SourceRange getSourceRange() const LLVM_READONLY;
2221 SourceLocation getLocStart() const LLVM_READONLY { return AtLoc; }
2222 void setAtLoc(SourceLocation Loc) { AtLoc = Loc; }
2224 ObjCPropertyDecl *getPropertyDecl() const {
2225 return PropertyDecl;
2227 void setPropertyDecl(ObjCPropertyDecl *Prop) { PropertyDecl = Prop; }
2229 Kind getPropertyImplementation() const {
2230 return PropertyIvarDecl ? Synthesize : Dynamic;
2233 ObjCIvarDecl *getPropertyIvarDecl() const {
2234 return PropertyIvarDecl;
2236 SourceLocation getPropertyIvarDeclLoc() const { return IvarLoc; }
2238 void setPropertyIvarDecl(ObjCIvarDecl *Ivar,
2239 SourceLocation IvarLoc) {
2240 PropertyIvarDecl = Ivar;
2241 this->IvarLoc = IvarLoc;
2244 /// \brief For \@synthesize, returns true if an ivar name was explicitly
2248 /// \@synthesize int a = b; // true
2249 /// \@synthesize int a; // false
2251 bool isIvarNameSpecified() const {
2252 return IvarLoc.isValid() && IvarLoc != getLocation();
2255 Expr *getGetterCXXConstructor() const {
2256 return GetterCXXConstructor;
2258 void setGetterCXXConstructor(Expr *getterCXXConstructor) {
2259 GetterCXXConstructor = getterCXXConstructor;
2262 Expr *getSetterCXXAssignment() const {
2263 return SetterCXXAssignment;
2265 void setSetterCXXAssignment(Expr *setterCXXAssignment) {
2266 SetterCXXAssignment = setterCXXAssignment;
2269 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
2270 static bool classofKind(Decl::Kind K) { return K == ObjCPropertyImpl; }
2272 friend class ASTDeclReader;
2275 template<bool (*Filter)(ObjCCategoryDecl *)>
2277 ObjCInterfaceDecl::filtered_category_iterator<Filter>::
2278 findAcceptableCategory() {
2279 while (Current && !Filter(Current))
2280 Current = Current->getNextClassCategoryRaw();
2283 template<bool (*Filter)(ObjCCategoryDecl *)>
2284 inline ObjCInterfaceDecl::filtered_category_iterator<Filter> &
2285 ObjCInterfaceDecl::filtered_category_iterator<Filter>::operator++() {
2286 Current = Current->getNextClassCategoryRaw();
2287 findAcceptableCategory();
2291 inline bool ObjCInterfaceDecl::isVisibleCategory(ObjCCategoryDecl *Cat) {
2292 return !Cat->isHidden();
2295 inline bool ObjCInterfaceDecl::isVisibleExtension(ObjCCategoryDecl *Cat) {
2296 return Cat->IsClassExtension() && !Cat->isHidden();
2299 inline bool ObjCInterfaceDecl::isKnownExtension(ObjCCategoryDecl *Cat) {
2300 return Cat->IsClassExtension();
2303 } // end namespace clang