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 "llvm/ADT/STLExtras.h"
27 class ObjCProtocolDecl;
28 class ObjCCategoryDecl;
29 class ObjCPropertyDecl;
30 class ObjCPropertyImplDecl;
31 class CXXCtorInitializer;
34 void operator=(const ObjCListBase &); // DO NOT IMPLEMENT
35 ObjCListBase(const ObjCListBase&); // DO NOT IMPLEMENT
37 /// List is an array of pointers to objects that are not owned by this object.
42 ObjCListBase() : List(0), NumElts(0) {}
43 unsigned size() const { return NumElts; }
44 bool empty() const { return NumElts == 0; }
47 void set(void *const* InList, unsigned Elts, ASTContext &Ctx);
51 /// ObjCList - This is a simple template class used to hold various lists of
52 /// decls etc, which is heavily used by the ObjC front-end. This only use case
53 /// this supports is setting the list all at once and then reading elements out
56 class ObjCList : public ObjCListBase {
58 void set(T* const* InList, unsigned Elts, ASTContext &Ctx) {
59 ObjCListBase::set(reinterpret_cast<void*const*>(InList), Elts, Ctx);
62 typedef T* const * iterator;
63 iterator begin() const { return (iterator)List; }
64 iterator end() const { return (iterator)List+NumElts; }
66 T* operator[](unsigned Idx) const {
67 assert(Idx < NumElts && "Invalid access");
72 /// \brief A list of Objective-C protocols, along with the source
73 /// locations at which they were referenced.
74 class ObjCProtocolList : public ObjCList<ObjCProtocolDecl> {
75 SourceLocation *Locations;
77 using ObjCList<ObjCProtocolDecl>::set;
80 ObjCProtocolList() : ObjCList<ObjCProtocolDecl>(), Locations(0) { }
82 typedef const SourceLocation *loc_iterator;
83 loc_iterator loc_begin() const { return Locations; }
84 loc_iterator loc_end() const { return Locations + size(); }
86 void set(ObjCProtocolDecl* const* InList, unsigned Elts,
87 const SourceLocation *Locs, ASTContext &Ctx);
91 /// ObjCMethodDecl - Represents an instance or class method declaration.
92 /// ObjC methods can be declared within 4 contexts: class interfaces,
93 /// categories, protocols, and class implementations. While C++ member
94 /// functions leverage C syntax, Objective-C method syntax is modeled after
95 /// Smalltalk (using colons to specify argument types/expressions).
96 /// Here are some brief examples:
98 /// Setter/getter instance methods:
99 /// - (void)setMenu:(NSMenu *)menu;
100 /// - (NSMenu *)menu;
102 /// Instance method that takes 2 NSView arguments:
103 /// - (void)replaceSubview:(NSView *)oldView with:(NSView *)newView;
105 /// Getter class method:
106 /// + (NSMenu *)defaultMenu;
108 /// A selector represents a unique name for a method. The selector names for
109 /// the above methods are setMenu:, menu, replaceSubview:with:, and defaultMenu.
111 class ObjCMethodDecl : public NamedDecl, public DeclContext {
113 enum ImplementationControl { None, Required, Optional };
115 // The conventional meaning of this method; an ObjCMethodFamily.
116 // This is not serialized; instead, it is computed on demand and
118 mutable unsigned Family : ObjCMethodFamilyBitWidth;
120 /// instance (true) or class (false) method.
121 unsigned IsInstance : 1;
122 unsigned IsVariadic : 1;
124 // Synthesized declaration method for a property setter/getter
125 unsigned IsSynthesized : 1;
127 // Method has a definition.
128 unsigned IsDefined : 1;
130 // NOTE: VC++ treats enums as signed, avoid using ImplementationControl enum
131 /// @required/@optional
132 unsigned DeclImplementation : 2;
134 // NOTE: VC++ treats enums as signed, avoid using the ObjCDeclQualifier enum
136 unsigned objcDeclQualifier : 6;
138 /// \brief Indicates whether this method has a related result type.
139 unsigned RelatedResultType : 1;
141 // Number of args separated by ':' in a method declaration.
142 unsigned NumSelectorArgs;
144 // Result type of this method.
145 QualType MethodDeclType;
147 // Type source information for the result type.
148 TypeSourceInfo *ResultTInfo;
150 /// ParamInfo - List of pointers to VarDecls for the formal parameters of this
152 ObjCList<ParmVarDecl> ParamInfo;
154 /// List of attributes for this method declaration.
155 SourceLocation EndLoc; // the location of the ';' or '}'.
157 // The following are only used for method definitions, null otherwise.
158 // FIXME: space savings opportunity, consider a sub-class.
161 /// SelfDecl - Decl for the implicit self parameter. This is lazily
162 /// constructed by createImplicitParams.
163 ImplicitParamDecl *SelfDecl;
164 /// CmdDecl - Decl for the implicit _cmd parameter. This is lazily
165 /// constructed by createImplicitParams.
166 ImplicitParamDecl *CmdDecl;
168 ObjCMethodDecl(SourceLocation beginLoc, SourceLocation endLoc,
169 Selector SelInfo, QualType T,
170 TypeSourceInfo *ResultTInfo,
171 DeclContext *contextDecl,
172 bool isInstance = true,
173 bool isVariadic = false,
174 bool isSynthesized = false,
175 bool isDefined = false,
176 ImplementationControl impControl = None,
177 bool HasRelatedResultType = false,
178 unsigned numSelectorArgs = 0)
179 : NamedDecl(ObjCMethod, contextDecl, beginLoc, SelInfo),
180 DeclContext(ObjCMethod), Family(InvalidObjCMethodFamily),
181 IsInstance(isInstance), IsVariadic(isVariadic),
182 IsSynthesized(isSynthesized),
183 IsDefined(isDefined),
184 DeclImplementation(impControl), objcDeclQualifier(OBJC_TQ_None),
185 RelatedResultType(HasRelatedResultType), NumSelectorArgs(numSelectorArgs),
186 MethodDeclType(T), ResultTInfo(ResultTInfo),
187 EndLoc(endLoc), Body(0), SelfDecl(0), CmdDecl(0) {}
189 /// \brief A definition will return its interface declaration.
190 /// An interface declaration will return its definition.
191 /// Otherwise it will return itself.
192 virtual ObjCMethodDecl *getNextRedeclaration();
195 static ObjCMethodDecl *Create(ASTContext &C,
196 SourceLocation beginLoc,
197 SourceLocation endLoc, Selector SelInfo,
199 TypeSourceInfo *ResultTInfo,
200 DeclContext *contextDecl,
201 bool isInstance = true,
202 bool isVariadic = false,
203 bool isSynthesized = false,
204 bool isDefined = false,
205 ImplementationControl impControl = None,
206 bool HasRelatedResultType = false,
207 unsigned numSelectorArgs = 0);
209 virtual ObjCMethodDecl *getCanonicalDecl();
210 const ObjCMethodDecl *getCanonicalDecl() const {
211 return const_cast<ObjCMethodDecl*>(this)->getCanonicalDecl();
214 ObjCDeclQualifier getObjCDeclQualifier() const {
215 return ObjCDeclQualifier(objcDeclQualifier);
217 void setObjCDeclQualifier(ObjCDeclQualifier QV) { objcDeclQualifier = QV; }
219 /// \brief Determine whether this method has a result type that is related
220 /// to the message receiver's type.
221 bool hasRelatedResultType() const { return RelatedResultType; }
223 /// \brief Note whether this method has a related result type.
224 void SetRelatedResultType(bool RRT = true) { RelatedResultType = RRT; }
226 unsigned getNumSelectorArgs() const { return NumSelectorArgs; }
227 void setNumSelectorArgs(unsigned numSelectorArgs) {
228 NumSelectorArgs = numSelectorArgs;
231 // Location information, modeled after the Stmt API.
232 SourceLocation getLocStart() const { return getLocation(); }
233 SourceLocation getLocEnd() const { return EndLoc; }
234 void setEndLoc(SourceLocation Loc) { EndLoc = Loc; }
235 virtual SourceRange getSourceRange() const {
236 return SourceRange(getLocation(), EndLoc);
239 ObjCInterfaceDecl *getClassInterface();
240 const ObjCInterfaceDecl *getClassInterface() const {
241 return const_cast<ObjCMethodDecl*>(this)->getClassInterface();
244 Selector getSelector() const { return getDeclName().getObjCSelector(); }
246 QualType getResultType() const { return MethodDeclType; }
247 void setResultType(QualType T) { MethodDeclType = T; }
249 /// \brief Determine the type of an expression that sends a message to this
251 QualType getSendResultType() const {
252 return getResultType().getNonLValueExprType(getASTContext());
255 TypeSourceInfo *getResultTypeSourceInfo() const { return ResultTInfo; }
256 void setResultTypeSourceInfo(TypeSourceInfo *TInfo) { ResultTInfo = TInfo; }
258 // Iterator access to formal parameters.
259 unsigned param_size() const { return ParamInfo.size(); }
260 typedef ObjCList<ParmVarDecl>::iterator param_iterator;
261 param_iterator param_begin() const { return ParamInfo.begin(); }
262 param_iterator param_end() const { return ParamInfo.end(); }
263 // This method returns and of the parameters which are part of the selector
264 // name mangling requirements.
265 param_iterator sel_param_end() const {
266 return ParamInfo.begin() + NumSelectorArgs;
269 void setMethodParams(ASTContext &C, ParmVarDecl *const *List, unsigned Num,
270 unsigned numSelectorArgs) {
271 ParamInfo.set(List, Num, C);
272 NumSelectorArgs = numSelectorArgs;
275 // Iterator access to parameter types.
276 typedef std::const_mem_fun_t<QualType, ParmVarDecl> deref_fun;
277 typedef llvm::mapped_iterator<param_iterator, deref_fun> arg_type_iterator;
279 arg_type_iterator arg_type_begin() const {
280 return llvm::map_iterator(param_begin(), deref_fun(&ParmVarDecl::getType));
282 arg_type_iterator arg_type_end() const {
283 return llvm::map_iterator(param_end(), deref_fun(&ParmVarDecl::getType));
286 /// createImplicitParams - Used to lazily create the self and cmd
287 /// implict parameters. This must be called prior to using getSelfDecl()
288 /// or getCmdDecl(). The call is ignored if the implicit paramters
289 /// have already been created.
290 void createImplicitParams(ASTContext &Context, const ObjCInterfaceDecl *ID);
292 ImplicitParamDecl * getSelfDecl() const { return SelfDecl; }
293 void setSelfDecl(ImplicitParamDecl *SD) { SelfDecl = SD; }
294 ImplicitParamDecl * getCmdDecl() const { return CmdDecl; }
295 void setCmdDecl(ImplicitParamDecl *CD) { CmdDecl = CD; }
297 /// Determines the family of this method.
298 ObjCMethodFamily getMethodFamily() const;
300 bool isInstanceMethod() const { return IsInstance; }
301 void setInstanceMethod(bool isInst) { IsInstance = isInst; }
302 bool isVariadic() const { return IsVariadic; }
303 void setVariadic(bool isVar) { IsVariadic = isVar; }
305 bool isClassMethod() const { return !IsInstance; }
307 bool isSynthesized() const { return IsSynthesized; }
308 void setSynthesized(bool isSynth) { IsSynthesized = isSynth; }
310 bool isDefined() const { return IsDefined; }
311 void setDefined(bool isDefined) { IsDefined = isDefined; }
313 // Related to protocols declared in @protocol
314 void setDeclImplementation(ImplementationControl ic) {
315 DeclImplementation = ic;
317 ImplementationControl getImplementationControl() const {
318 return ImplementationControl(DeclImplementation);
321 virtual Stmt *getBody() const {
324 CompoundStmt *getCompoundBody() { return (CompoundStmt*)Body; }
325 void setBody(Stmt *B) { Body = B; }
327 /// \brief Returns whether this specific method is a definition.
328 bool isThisDeclarationADefinition() const { return Body; }
330 // Implement isa/cast/dyncast/etc.
331 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
332 static bool classof(const ObjCMethodDecl *D) { return true; }
333 static bool classofKind(Kind K) { return K == ObjCMethod; }
334 static DeclContext *castToDeclContext(const ObjCMethodDecl *D) {
335 return static_cast<DeclContext *>(const_cast<ObjCMethodDecl*>(D));
337 static ObjCMethodDecl *castFromDeclContext(const DeclContext *DC) {
338 return static_cast<ObjCMethodDecl *>(const_cast<DeclContext*>(DC));
342 /// ObjCContainerDecl - Represents a container for method declarations.
343 /// Current sub-classes are ObjCInterfaceDecl, ObjCCategoryDecl,
344 /// ObjCProtocolDecl, and ObjCImplDecl.
346 class ObjCContainerDecl : public NamedDecl, public DeclContext {
347 // These two locations in the range mark the end of the method container.
348 // The first points to the '@' token, and the second to the 'end' token.
352 ObjCContainerDecl(Kind DK, DeclContext *DC, SourceLocation L,
354 : NamedDecl(DK, DC, L, Id), DeclContext(DK) {}
356 // Iterator access to properties.
357 typedef specific_decl_iterator<ObjCPropertyDecl> prop_iterator;
358 prop_iterator prop_begin() const {
359 return prop_iterator(decls_begin());
361 prop_iterator prop_end() const {
362 return prop_iterator(decls_end());
365 // Iterator access to instance/class methods.
366 typedef specific_decl_iterator<ObjCMethodDecl> method_iterator;
367 method_iterator meth_begin() const {
368 return method_iterator(decls_begin());
370 method_iterator meth_end() const {
371 return method_iterator(decls_end());
374 typedef filtered_decl_iterator<ObjCMethodDecl,
375 &ObjCMethodDecl::isInstanceMethod>
377 instmeth_iterator instmeth_begin() const {
378 return instmeth_iterator(decls_begin());
380 instmeth_iterator instmeth_end() const {
381 return instmeth_iterator(decls_end());
384 typedef filtered_decl_iterator<ObjCMethodDecl,
385 &ObjCMethodDecl::isClassMethod>
387 classmeth_iterator classmeth_begin() const {
388 return classmeth_iterator(decls_begin());
390 classmeth_iterator classmeth_end() const {
391 return classmeth_iterator(decls_end());
394 // Get the local instance/class method declared in this interface.
395 ObjCMethodDecl *getMethod(Selector Sel, bool isInstance) const;
396 ObjCMethodDecl *getInstanceMethod(Selector Sel) const {
397 return getMethod(Sel, true/*isInstance*/);
399 ObjCMethodDecl *getClassMethod(Selector Sel) const {
400 return getMethod(Sel, false/*isInstance*/);
402 ObjCIvarDecl *getIvarDecl(IdentifierInfo *Id) const;
404 ObjCPropertyDecl *FindPropertyDeclaration(IdentifierInfo *PropertyId) const;
406 // Marks the end of the container.
407 SourceRange getAtEndRange() const {
410 void setAtEndRange(SourceRange atEnd) {
414 virtual SourceRange getSourceRange() const {
415 return SourceRange(getLocation(), getAtEndRange().getEnd());
418 // Implement isa/cast/dyncast/etc.
419 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
420 static bool classof(const ObjCContainerDecl *D) { return true; }
421 static bool classofKind(Kind K) {
422 return K >= firstObjCContainer &&
423 K <= lastObjCContainer;
426 static DeclContext *castToDeclContext(const ObjCContainerDecl *D) {
427 return static_cast<DeclContext *>(const_cast<ObjCContainerDecl*>(D));
429 static ObjCContainerDecl *castFromDeclContext(const DeclContext *DC) {
430 return static_cast<ObjCContainerDecl *>(const_cast<DeclContext*>(DC));
434 /// ObjCInterfaceDecl - Represents an ObjC class declaration. For example:
436 /// // MostPrimitive declares no super class (not particularly useful).
437 /// @interface MostPrimitive
438 /// // no instance variables or methods.
441 /// // NSResponder inherits from NSObject & implements NSCoding (a protocol).
442 /// @interface NSResponder : NSObject <NSCoding>
443 /// { // instance variables are represented by ObjCIvarDecl.
444 /// id nextResponder; // nextResponder instance variable.
446 /// - (NSResponder *)nextResponder; // return a pointer to NSResponder.
447 /// - (void)mouseMoved:(NSEvent *)theEvent; // return void, takes a pointer
448 /// @end // to an NSEvent.
450 /// Unlike C/C++, forward class declarations are accomplished with @class.
451 /// Unlike C/C++, @class allows for a list of classes to be forward declared.
452 /// Unlike C++, ObjC is a single-rooted class model. In Cocoa, classes
453 /// typically inherit from NSObject (an exception is NSProxy).
455 class ObjCInterfaceDecl : public ObjCContainerDecl {
456 /// TypeForDecl - This indicates the Type object that represents this
457 /// TypeDecl. It is a cache maintained by ASTContext::getObjCInterfaceType
458 mutable const Type *TypeForDecl;
459 friend class ASTContext;
461 /// Class's super class.
462 ObjCInterfaceDecl *SuperClass;
464 /// Protocols referenced in the @interface declaration
465 ObjCProtocolList ReferencedProtocols;
467 /// Protocols reference in both the @interface and class extensions.
468 ObjCList<ObjCProtocolDecl> AllReferencedProtocols;
470 /// \brief List of categories and class extensions defined for this class.
472 /// Categories are stored as a linked list in the AST, since the categories
473 /// and class extensions come long after the initial interface declaration,
474 /// and we avoid dynamically-resized arrays in the AST wherever possible.
475 ObjCCategoryDecl *CategoryList;
477 /// IvarList - List of all ivars defined by this class; including class
478 /// extensions and implementation. This list is built lazily.
479 ObjCIvarDecl *IvarList;
481 bool ForwardDecl:1; // declared with @class.
482 bool InternalInterface:1; // true - no @interface for @implementation
484 /// \brief Indicates that the contents of this Objective-C class will be
485 /// completed by the external AST source when required.
486 mutable bool ExternallyCompleted : 1;
488 SourceLocation ClassLoc; // location of the class identifier.
489 SourceLocation SuperClassLoc; // location of the super class identifier.
490 SourceLocation EndLoc; // marks the '>', '}', or identifier.
492 ObjCInterfaceDecl(DeclContext *DC, SourceLocation atLoc, IdentifierInfo *Id,
493 SourceLocation CLoc, bool FD, bool isInternal);
495 void LoadExternalDefinition() const;
497 static ObjCInterfaceDecl *Create(ASTContext &C, DeclContext *DC,
498 SourceLocation atLoc,
500 SourceLocation ClassLoc = SourceLocation(),
501 bool ForwardDecl = false,
502 bool isInternal = false);
504 /// \brief Indicate that this Objective-C class is complete, but that
505 /// the external AST source will be responsible for filling in its contents
506 /// when a complete class is required.
507 void setExternallyCompleted();
509 const ObjCProtocolList &getReferencedProtocols() const {
510 if (ExternallyCompleted)
511 LoadExternalDefinition();
513 return ReferencedProtocols;
516 ObjCImplementationDecl *getImplementation() const;
517 void setImplementation(ObjCImplementationDecl *ImplD);
519 ObjCCategoryDecl *FindCategoryDeclaration(IdentifierInfo *CategoryId) const;
521 // Get the local instance/class method declared in a category.
522 ObjCMethodDecl *getCategoryInstanceMethod(Selector Sel) const;
523 ObjCMethodDecl *getCategoryClassMethod(Selector Sel) const;
524 ObjCMethodDecl *getCategoryMethod(Selector Sel, bool isInstance) const {
525 return isInstance ? getInstanceMethod(Sel)
526 : getClassMethod(Sel);
529 typedef ObjCProtocolList::iterator protocol_iterator;
531 protocol_iterator protocol_begin() const {
532 if (ExternallyCompleted)
533 LoadExternalDefinition();
535 return ReferencedProtocols.begin();
537 protocol_iterator protocol_end() const {
538 if (ExternallyCompleted)
539 LoadExternalDefinition();
541 return ReferencedProtocols.end();
544 typedef ObjCProtocolList::loc_iterator protocol_loc_iterator;
546 protocol_loc_iterator protocol_loc_begin() const {
547 if (ExternallyCompleted)
548 LoadExternalDefinition();
550 return ReferencedProtocols.loc_begin();
553 protocol_loc_iterator protocol_loc_end() const {
554 if (ExternallyCompleted)
555 LoadExternalDefinition();
557 return ReferencedProtocols.loc_end();
560 typedef ObjCList<ObjCProtocolDecl>::iterator all_protocol_iterator;
562 all_protocol_iterator all_referenced_protocol_begin() const {
563 if (ExternallyCompleted)
564 LoadExternalDefinition();
566 return AllReferencedProtocols.empty() ? protocol_begin()
567 : AllReferencedProtocols.begin();
569 all_protocol_iterator all_referenced_protocol_end() const {
570 if (ExternallyCompleted)
571 LoadExternalDefinition();
573 return AllReferencedProtocols.empty() ? protocol_end()
574 : AllReferencedProtocols.end();
577 typedef specific_decl_iterator<ObjCIvarDecl> ivar_iterator;
579 ivar_iterator ivar_begin() const { return ivar_iterator(decls_begin()); }
580 ivar_iterator ivar_end() const { return ivar_iterator(decls_end()); }
582 unsigned ivar_size() const {
583 return std::distance(ivar_begin(), ivar_end());
586 bool ivar_empty() const { return ivar_begin() == ivar_end(); }
588 ObjCIvarDecl *all_declared_ivar_begin();
589 void setIvarList(ObjCIvarDecl *ivar) { IvarList = ivar; }
591 /// setProtocolList - Set the list of protocols that this interface
593 void setProtocolList(ObjCProtocolDecl *const* List, unsigned Num,
594 const SourceLocation *Locs, ASTContext &C) {
595 ReferencedProtocols.set(List, Num, Locs, C);
598 /// mergeClassExtensionProtocolList - Merge class extension's protocol list
599 /// into the protocol list for this class.
600 void mergeClassExtensionProtocolList(ObjCProtocolDecl *const* List,
604 bool isForwardDecl() const { return ForwardDecl; }
605 void setForwardDecl(bool val) { ForwardDecl = val; }
607 ObjCInterfaceDecl *getSuperClass() const {
608 if (ExternallyCompleted)
609 LoadExternalDefinition();
614 void setSuperClass(ObjCInterfaceDecl * superCls) { SuperClass = superCls; }
616 ObjCCategoryDecl* getCategoryList() const {
617 if (ExternallyCompleted)
618 LoadExternalDefinition();
623 void setCategoryList(ObjCCategoryDecl *category) {
624 CategoryList = category;
627 ObjCCategoryDecl* getFirstClassExtension() const;
630 *FindPropertyVisibleInPrimaryClass(IdentifierInfo *PropertyId) const;
632 /// isSuperClassOf - Return true if this class is the specified class or is a
633 /// super class of the specified interface class.
634 bool isSuperClassOf(const ObjCInterfaceDecl *I) const {
635 // If RHS is derived from LHS it is OK; else it is not OK.
639 I = I->getSuperClass();
644 /// isArcWeakrefUnavailable - Checks for a class or one of its super classes
645 /// to be incompatible with __weak references. Returns true if it is.
646 bool isArcWeakrefUnavailable() const {
647 const ObjCInterfaceDecl *Class = this;
649 if (Class->hasAttr<ArcWeakrefUnavailableAttr>())
651 Class = Class->getSuperClass();
656 ObjCIvarDecl *lookupInstanceVariable(IdentifierInfo *IVarName,
657 ObjCInterfaceDecl *&ClassDeclared);
658 ObjCIvarDecl *lookupInstanceVariable(IdentifierInfo *IVarName) {
659 ObjCInterfaceDecl *ClassDeclared;
660 return lookupInstanceVariable(IVarName, ClassDeclared);
663 // Lookup a method. First, we search locally. If a method isn't
664 // found, we search referenced protocols and class categories.
665 ObjCMethodDecl *lookupMethod(Selector Sel, bool isInstance) const;
666 ObjCMethodDecl *lookupInstanceMethod(Selector Sel) const {
667 return lookupMethod(Sel, true/*isInstance*/);
669 ObjCMethodDecl *lookupClassMethod(Selector Sel) const {
670 return lookupMethod(Sel, false/*isInstance*/);
672 ObjCInterfaceDecl *lookupInheritedClass(const IdentifierInfo *ICName);
674 // Lookup a method in the classes implementation hierarchy.
675 ObjCMethodDecl *lookupPrivateMethod(const Selector &Sel, bool Instance=true);
677 // Location information, modeled after the Stmt API.
678 SourceLocation getLocStart() const { return getLocation(); } // '@'interface
679 SourceLocation getLocEnd() const { return EndLoc; }
680 void setLocEnd(SourceLocation LE) { EndLoc = LE; }
682 void setClassLoc(SourceLocation Loc) { ClassLoc = Loc; }
683 SourceLocation getClassLoc() const { return ClassLoc; }
684 void setSuperClassLoc(SourceLocation Loc) { SuperClassLoc = Loc; }
685 SourceLocation getSuperClassLoc() const { return SuperClassLoc; }
687 /// isImplicitInterfaceDecl - check that this is an implicitly declared
688 /// ObjCInterfaceDecl node. This is for legacy objective-c @implementation
689 /// declaration without an @interface declaration.
690 bool isImplicitInterfaceDecl() const { return InternalInterface; }
691 void setImplicitInterfaceDecl(bool val) { InternalInterface = val; }
693 /// ClassImplementsProtocol - Checks that 'lProto' protocol
694 /// has been implemented in IDecl class, its super class or categories (if
695 /// lookupCategory is true).
696 bool ClassImplementsProtocol(ObjCProtocolDecl *lProto,
698 bool RHSIsQualifiedID = false);
700 // Low-level accessor
701 const Type *getTypeForDecl() const { return TypeForDecl; }
702 void setTypeForDecl(const Type *TD) const { TypeForDecl = TD; }
704 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
705 static bool classof(const ObjCInterfaceDecl *D) { return true; }
706 static bool classofKind(Kind K) { return K == ObjCInterface; }
708 friend class ASTDeclReader;
709 friend class ASTDeclWriter;
712 /// ObjCIvarDecl - Represents an ObjC instance variable. In general, ObjC
713 /// instance variables are identical to C. The only exception is Objective-C
714 /// supports C++ style access control. For example:
716 /// @interface IvarExample : NSObject
718 /// id defaultToProtected;
720 /// id canBePublic; // same as C++.
722 /// id canBeProtected; // same as C++.
724 /// id canBePackage; // framework visibility (not available in C++).
727 class ObjCIvarDecl : public FieldDecl {
730 None, Private, Protected, Public, Package
734 ObjCIvarDecl(ObjCContainerDecl *DC, SourceLocation StartLoc,
735 SourceLocation IdLoc, IdentifierInfo *Id,
736 QualType T, TypeSourceInfo *TInfo, AccessControl ac, Expr *BW,
738 : FieldDecl(ObjCIvar, DC, StartLoc, IdLoc, Id, T, TInfo, BW,
739 /*Mutable=*/false, /*HasInit=*/false),
740 NextIvar(0), DeclAccess(ac), Synthesized(synthesized) {}
743 static ObjCIvarDecl *Create(ASTContext &C, ObjCContainerDecl *DC,
744 SourceLocation StartLoc, SourceLocation IdLoc,
745 IdentifierInfo *Id, QualType T,
746 TypeSourceInfo *TInfo,
747 AccessControl ac, Expr *BW = NULL,
748 bool synthesized=false);
750 /// \brief Return the class interface that this ivar is logically contained
751 /// in; this is either the interface where the ivar was declared, or the
752 /// interface the ivar is conceptually a part of in the case of synthesized
754 const ObjCInterfaceDecl *getContainingInterface() const;
756 ObjCIvarDecl *getNextIvar() { return NextIvar; }
757 void setNextIvar(ObjCIvarDecl *ivar) { NextIvar = ivar; }
759 void setAccessControl(AccessControl ac) { DeclAccess = ac; }
761 AccessControl getAccessControl() const { return AccessControl(DeclAccess); }
763 AccessControl getCanonicalAccessControl() const {
764 return DeclAccess == None ? Protected : AccessControl(DeclAccess);
767 void setSynthesize(bool synth) { Synthesized = synth; }
768 bool getSynthesize() const { return Synthesized; }
770 // Implement isa/cast/dyncast/etc.
771 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
772 static bool classof(const ObjCIvarDecl *D) { return true; }
773 static bool classofKind(Kind K) { return K == ObjCIvar; }
775 /// NextIvar - Next Ivar in the list of ivars declared in class; class's
776 /// extensions and class's implementation
777 ObjCIvarDecl *NextIvar;
779 // NOTE: VC++ treats enums as signed, avoid using the AccessControl enum
780 unsigned DeclAccess : 3;
781 unsigned Synthesized : 1;
785 /// ObjCAtDefsFieldDecl - Represents a field declaration created by an
787 class ObjCAtDefsFieldDecl : public FieldDecl {
789 ObjCAtDefsFieldDecl(DeclContext *DC, SourceLocation StartLoc,
790 SourceLocation IdLoc, IdentifierInfo *Id,
791 QualType T, Expr *BW)
792 : FieldDecl(ObjCAtDefsField, DC, StartLoc, IdLoc, Id, T,
793 /*TInfo=*/0, // FIXME: Do ObjCAtDefs have declarators ?
794 BW, /*Mutable=*/false, /*HasInit=*/false) {}
797 static ObjCAtDefsFieldDecl *Create(ASTContext &C, DeclContext *DC,
798 SourceLocation StartLoc,
799 SourceLocation IdLoc, IdentifierInfo *Id,
800 QualType T, Expr *BW);
802 // Implement isa/cast/dyncast/etc.
803 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
804 static bool classof(const ObjCAtDefsFieldDecl *D) { return true; }
805 static bool classofKind(Kind K) { return K == ObjCAtDefsField; }
808 /// ObjCProtocolDecl - Represents a protocol declaration. ObjC protocols
809 /// declare a pure abstract type (i.e no instance variables are permitted).
810 /// Protocols originally drew inspiration from C++ pure virtual functions (a C++
811 /// feature with nice semantics and lousy syntax:-). Here is an example:
813 /// @protocol NSDraggingInfo <refproto1, refproto2>
814 /// - (NSWindow *)draggingDestinationWindow;
815 /// - (NSImage *)draggedImage;
818 /// This says that NSDraggingInfo requires two methods and requires everything
819 /// that the two "referenced protocols" 'refproto1' and 'refproto2' require as
822 /// @interface ImplementsNSDraggingInfo : NSObject <NSDraggingInfo>
825 /// ObjC protocols inspired Java interfaces. Unlike Java, ObjC classes and
826 /// protocols are in distinct namespaces. For example, Cocoa defines both
827 /// an NSObject protocol and class (which isn't allowed in Java). As a result,
828 /// protocols are referenced using angle brackets as follows:
830 /// id <NSDraggingInfo> anyObjectThatImplementsNSDraggingInfo;
832 class ObjCProtocolDecl : public ObjCContainerDecl {
833 /// Referenced protocols
834 ObjCProtocolList ReferencedProtocols;
836 bool isForwardProtoDecl; // declared with @protocol.
838 SourceLocation EndLoc; // marks the '>' or identifier.
840 ObjCProtocolDecl(DeclContext *DC, SourceLocation L, IdentifierInfo *Id)
841 : ObjCContainerDecl(ObjCProtocol, DC, L, Id),
842 isForwardProtoDecl(true) {
846 static ObjCProtocolDecl *Create(ASTContext &C, DeclContext *DC,
847 SourceLocation L, IdentifierInfo *Id);
849 const ObjCProtocolList &getReferencedProtocols() const {
850 return ReferencedProtocols;
852 typedef ObjCProtocolList::iterator protocol_iterator;
853 protocol_iterator protocol_begin() const {return ReferencedProtocols.begin();}
854 protocol_iterator protocol_end() const { return ReferencedProtocols.end(); }
855 typedef ObjCProtocolList::loc_iterator protocol_loc_iterator;
856 protocol_loc_iterator protocol_loc_begin() const {
857 return ReferencedProtocols.loc_begin();
859 protocol_loc_iterator protocol_loc_end() const {
860 return ReferencedProtocols.loc_end();
862 unsigned protocol_size() const { return ReferencedProtocols.size(); }
864 /// setProtocolList - Set the list of protocols that this interface
866 void setProtocolList(ObjCProtocolDecl *const*List, unsigned Num,
867 const SourceLocation *Locs, ASTContext &C) {
868 ReferencedProtocols.set(List, Num, Locs, C);
871 ObjCProtocolDecl *lookupProtocolNamed(IdentifierInfo *PName);
873 // Lookup a method. First, we search locally. If a method isn't
874 // found, we search referenced protocols and class categories.
875 ObjCMethodDecl *lookupMethod(Selector Sel, bool isInstance) const;
876 ObjCMethodDecl *lookupInstanceMethod(Selector Sel) const {
877 return lookupMethod(Sel, true/*isInstance*/);
879 ObjCMethodDecl *lookupClassMethod(Selector Sel) const {
880 return lookupMethod(Sel, false/*isInstance*/);
883 bool isForwardDecl() const { return isForwardProtoDecl; }
884 void setForwardDecl(bool val) { isForwardProtoDecl = val; }
886 // Location information, modeled after the Stmt API.
887 SourceLocation getLocStart() const { return getLocation(); } // '@'protocol
888 SourceLocation getLocEnd() const { return EndLoc; }
889 void setLocEnd(SourceLocation LE) { EndLoc = LE; }
891 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
892 static bool classof(const ObjCProtocolDecl *D) { return true; }
893 static bool classofKind(Kind K) { return K == ObjCProtocol; }
896 /// ObjCClassDecl - Specifies a list of forward class declarations. For example:
898 /// @class NSCursor, NSImage, NSPasteboard, NSWindow;
900 class ObjCClassDecl : public Decl {
903 ObjCInterfaceDecl *ID;
906 ObjCClassRef(ObjCInterfaceDecl *d, SourceLocation l) : ID(d), L(l) {}
907 SourceLocation getLocation() const { return L; }
908 ObjCInterfaceDecl *getInterface() const { return ID; }
911 ObjCClassRef *ForwardDecls;
914 ObjCClassDecl(DeclContext *DC, SourceLocation L,
915 ObjCInterfaceDecl *const *Elts, const SourceLocation *Locs,
916 unsigned nElts, ASTContext &C);
918 static ObjCClassDecl *Create(ASTContext &C, DeclContext *DC, SourceLocation L,
919 ObjCInterfaceDecl *const *Elts = 0,
920 const SourceLocation *Locs = 0,
923 virtual SourceRange getSourceRange() const;
925 typedef const ObjCClassRef* iterator;
926 iterator begin() const { return ForwardDecls; }
927 iterator end() const { return ForwardDecls + NumDecls; }
928 unsigned size() const { return NumDecls; }
930 /// setClassList - Set the list of forward classes.
931 void setClassList(ASTContext &C, ObjCInterfaceDecl*const*List,
932 const SourceLocation *Locs, unsigned Num);
934 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
935 static bool classof(const ObjCClassDecl *D) { return true; }
936 static bool classofKind(Kind K) { return K == ObjCClass; }
939 /// ObjCForwardProtocolDecl - Specifies a list of forward protocol declarations.
942 /// @protocol NSTextInput, NSChangeSpelling, NSDraggingInfo;
944 class ObjCForwardProtocolDecl : public Decl {
945 ObjCProtocolList ReferencedProtocols;
947 ObjCForwardProtocolDecl(DeclContext *DC, SourceLocation L,
948 ObjCProtocolDecl *const *Elts, unsigned nElts,
949 const SourceLocation *Locs, ASTContext &C);
952 static ObjCForwardProtocolDecl *Create(ASTContext &C, DeclContext *DC,
954 ObjCProtocolDecl *const *Elts,
956 const SourceLocation *Locs);
958 static ObjCForwardProtocolDecl *Create(ASTContext &C, DeclContext *DC,
960 return Create(C, DC, L, 0, 0, 0);
963 typedef ObjCProtocolList::iterator protocol_iterator;
964 protocol_iterator protocol_begin() const {return ReferencedProtocols.begin();}
965 protocol_iterator protocol_end() const { return ReferencedProtocols.end(); }
966 typedef ObjCProtocolList::loc_iterator protocol_loc_iterator;
967 protocol_loc_iterator protocol_loc_begin() const {
968 return ReferencedProtocols.loc_begin();
970 protocol_loc_iterator protocol_loc_end() const {
971 return ReferencedProtocols.loc_end();
974 unsigned protocol_size() const { return ReferencedProtocols.size(); }
976 /// setProtocolList - Set the list of forward protocols.
977 void setProtocolList(ObjCProtocolDecl *const*List, unsigned Num,
978 const SourceLocation *Locs, ASTContext &C) {
979 ReferencedProtocols.set(List, Num, Locs, C);
981 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
982 static bool classof(const ObjCForwardProtocolDecl *D) { return true; }
983 static bool classofKind(Kind K) { return K == ObjCForwardProtocol; }
986 /// ObjCCategoryDecl - Represents a category declaration. A category allows
987 /// you to add methods to an existing class (without subclassing or modifying
988 /// the original class interface or implementation:-). Categories don't allow
989 /// you to add instance data. The following example adds "myMethod" to all
990 /// NSView's within a process:
992 /// @interface NSView (MyViewMethods)
996 /// Categories also allow you to split the implementation of a class across
997 /// several files (a feature more naturally supported in C++).
999 /// Categories were originally inspired by dynamic languages such as Common
1000 /// Lisp and Smalltalk. More traditional class-based languages (C++, Java)
1001 /// don't support this level of dynamism, which is both powerful and dangerous.
1003 class ObjCCategoryDecl : public ObjCContainerDecl {
1004 /// Interface belonging to this category
1005 ObjCInterfaceDecl *ClassInterface;
1007 /// referenced protocols in this category.
1008 ObjCProtocolList ReferencedProtocols;
1010 /// Next category belonging to this class.
1011 /// FIXME: this should not be a singly-linked list. Move storage elsewhere.
1012 ObjCCategoryDecl *NextClassCategory;
1014 /// true of class extension has at least one bitfield ivar.
1015 bool HasSynthBitfield : 1;
1017 /// \brief The location of the '@' in '@interface'
1018 SourceLocation AtLoc;
1020 /// \brief The location of the category name in this declaration.
1021 SourceLocation CategoryNameLoc;
1023 ObjCCategoryDecl(DeclContext *DC, SourceLocation AtLoc,
1024 SourceLocation ClassNameLoc, SourceLocation CategoryNameLoc,
1026 : ObjCContainerDecl(ObjCCategory, DC, ClassNameLoc, Id),
1027 ClassInterface(0), NextClassCategory(0), HasSynthBitfield(false),
1028 AtLoc(AtLoc), CategoryNameLoc(CategoryNameLoc) {
1032 static ObjCCategoryDecl *Create(ASTContext &C, DeclContext *DC,
1033 SourceLocation AtLoc,
1034 SourceLocation ClassNameLoc,
1035 SourceLocation CategoryNameLoc,
1036 IdentifierInfo *Id);
1038 ObjCInterfaceDecl *getClassInterface() { return ClassInterface; }
1039 const ObjCInterfaceDecl *getClassInterface() const { return ClassInterface; }
1040 void setClassInterface(ObjCInterfaceDecl *IDecl) { ClassInterface = IDecl; }
1042 ObjCCategoryImplDecl *getImplementation() const;
1043 void setImplementation(ObjCCategoryImplDecl *ImplD);
1045 /// setProtocolList - Set the list of protocols that this interface
1047 void setProtocolList(ObjCProtocolDecl *const*List, unsigned Num,
1048 const SourceLocation *Locs, ASTContext &C) {
1049 ReferencedProtocols.set(List, Num, Locs, C);
1052 const ObjCProtocolList &getReferencedProtocols() const {
1053 return ReferencedProtocols;
1056 typedef ObjCProtocolList::iterator protocol_iterator;
1057 protocol_iterator protocol_begin() const {return ReferencedProtocols.begin();}
1058 protocol_iterator protocol_end() const { return ReferencedProtocols.end(); }
1059 unsigned protocol_size() const { return ReferencedProtocols.size(); }
1060 typedef ObjCProtocolList::loc_iterator protocol_loc_iterator;
1061 protocol_loc_iterator protocol_loc_begin() const {
1062 return ReferencedProtocols.loc_begin();
1064 protocol_loc_iterator protocol_loc_end() const {
1065 return ReferencedProtocols.loc_end();
1068 ObjCCategoryDecl *getNextClassCategory() const { return NextClassCategory; }
1069 void setNextClassCategory(ObjCCategoryDecl *Cat) {
1070 NextClassCategory = Cat;
1072 void insertNextClassCategory() {
1073 NextClassCategory = ClassInterface->getCategoryList();
1074 ClassInterface->setCategoryList(this);
1075 ClassInterface->setChangedSinceDeserialization(true);
1078 bool IsClassExtension() const { return getIdentifier() == 0; }
1079 const ObjCCategoryDecl *getNextClassExtension() const;
1081 bool hasSynthBitfield() const { return HasSynthBitfield; }
1082 void setHasSynthBitfield (bool val) { HasSynthBitfield = val; }
1084 typedef specific_decl_iterator<ObjCIvarDecl> ivar_iterator;
1085 ivar_iterator ivar_begin() const {
1086 return ivar_iterator(decls_begin());
1088 ivar_iterator ivar_end() const {
1089 return ivar_iterator(decls_end());
1091 unsigned ivar_size() const {
1092 return std::distance(ivar_begin(), ivar_end());
1094 bool ivar_empty() const {
1095 return ivar_begin() == ivar_end();
1098 SourceLocation getAtLoc() const { return AtLoc; }
1099 void setAtLoc(SourceLocation At) { AtLoc = At; }
1101 SourceLocation getCategoryNameLoc() const { return CategoryNameLoc; }
1102 void setCategoryNameLoc(SourceLocation Loc) { CategoryNameLoc = Loc; }
1104 virtual SourceRange getSourceRange() const {
1105 return SourceRange(AtLoc, getAtEndRange().getEnd());
1108 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1109 static bool classof(const ObjCCategoryDecl *D) { return true; }
1110 static bool classofKind(Kind K) { return K == ObjCCategory; }
1113 class ObjCImplDecl : public ObjCContainerDecl {
1114 /// Class interface for this class/category implementation
1115 ObjCInterfaceDecl *ClassInterface;
1118 ObjCImplDecl(Kind DK, DeclContext *DC, SourceLocation L,
1119 ObjCInterfaceDecl *classInterface)
1120 : ObjCContainerDecl(DK, DC, L,
1121 classInterface? classInterface->getIdentifier() : 0),
1122 ClassInterface(classInterface) {}
1125 const ObjCInterfaceDecl *getClassInterface() const { return ClassInterface; }
1126 ObjCInterfaceDecl *getClassInterface() { return ClassInterface; }
1127 void setClassInterface(ObjCInterfaceDecl *IFace);
1129 void addInstanceMethod(ObjCMethodDecl *method) {
1130 // FIXME: Context should be set correctly before we get here.
1131 method->setLexicalDeclContext(this);
1134 void addClassMethod(ObjCMethodDecl *method) {
1135 // FIXME: Context should be set correctly before we get here.
1136 method->setLexicalDeclContext(this);
1140 void addPropertyImplementation(ObjCPropertyImplDecl *property);
1142 ObjCPropertyImplDecl *FindPropertyImplDecl(IdentifierInfo *propertyId) const;
1143 ObjCPropertyImplDecl *FindPropertyImplIvarDecl(IdentifierInfo *ivarId) const;
1145 // Iterator access to properties.
1146 typedef specific_decl_iterator<ObjCPropertyImplDecl> propimpl_iterator;
1147 propimpl_iterator propimpl_begin() const {
1148 return propimpl_iterator(decls_begin());
1150 propimpl_iterator propimpl_end() const {
1151 return propimpl_iterator(decls_end());
1154 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1155 static bool classof(const ObjCImplDecl *D) { return true; }
1156 static bool classofKind(Kind K) {
1157 return K >= firstObjCImpl && K <= lastObjCImpl;
1161 /// ObjCCategoryImplDecl - An object of this class encapsulates a category
1162 /// @implementation declaration. If a category class has declaration of a
1163 /// property, its implementation must be specified in the category's
1164 /// @implementation declaration. Example:
1165 /// @interface I @end
1166 /// @interface I(CATEGORY)
1167 /// @property int p1, d1;
1169 /// @implementation I(CATEGORY)
1173 /// ObjCCategoryImplDecl
1174 class ObjCCategoryImplDecl : public ObjCImplDecl {
1178 ObjCCategoryImplDecl(DeclContext *DC, SourceLocation L, IdentifierInfo *Id,
1179 ObjCInterfaceDecl *classInterface)
1180 : ObjCImplDecl(ObjCCategoryImpl, DC, L, classInterface), Id(Id) {}
1182 static ObjCCategoryImplDecl *Create(ASTContext &C, DeclContext *DC,
1183 SourceLocation L, IdentifierInfo *Id,
1184 ObjCInterfaceDecl *classInterface);
1186 /// getIdentifier - Get the identifier that names the category
1187 /// interface associated with this implementation.
1188 /// FIXME: This is a bad API, we are overriding the NamedDecl::getIdentifier()
1189 /// to mean something different. For example:
1190 /// ((NamedDecl *)SomeCategoryImplDecl)->getIdentifier()
1191 /// returns the class interface name, whereas
1192 /// ((ObjCCategoryImplDecl *)SomeCategoryImplDecl)->getIdentifier()
1193 /// returns the category name.
1194 IdentifierInfo *getIdentifier() const {
1197 void setIdentifier(IdentifierInfo *II) { Id = II; }
1199 ObjCCategoryDecl *getCategoryDecl() const;
1201 /// getName - Get the name of identifier for the class interface associated
1202 /// with this implementation as a StringRef.
1204 // FIXME: This is a bad API, we are overriding the NamedDecl::getName, to mean
1205 // something different.
1206 llvm::StringRef getName() const {
1207 return Id ? Id->getNameStart() : "";
1210 /// getNameAsCString - Get the name of identifier for the class
1211 /// interface associated with this implementation as a C string
1214 // FIXME: Deprecated, move clients to getName().
1215 const char *getNameAsCString() const {
1216 return Id ? Id->getNameStart() : "";
1219 /// @brief Get the name of the class associated with this interface.
1221 // FIXME: Deprecated, move clients to getName().
1222 std::string getNameAsString() const {
1226 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1227 static bool classof(const ObjCCategoryImplDecl *D) { return true; }
1228 static bool classofKind(Kind K) { return K == ObjCCategoryImpl;}
1231 llvm::raw_ostream &operator<<(llvm::raw_ostream &OS,
1232 const ObjCCategoryImplDecl *CID);
1234 /// ObjCImplementationDecl - Represents a class definition - this is where
1235 /// method definitions are specified. For example:
1238 /// @implementation MyClass
1239 /// - (void)myMethod { /* do something */ }
1243 /// Typically, instance variables are specified in the class interface,
1244 /// *not* in the implementation. Nevertheless (for legacy reasons), we
1245 /// allow instance variables to be specified in the implementation. When
1246 /// specified, they need to be *identical* to the interface.
1248 class ObjCImplementationDecl : public ObjCImplDecl {
1249 /// Implementation Class's super class.
1250 ObjCInterfaceDecl *SuperClass;
1251 /// Support for ivar initialization.
1252 /// IvarInitializers - The arguments used to initialize the ivars
1253 CXXCtorInitializer **IvarInitializers;
1254 unsigned NumIvarInitializers;
1256 /// true if class has a .cxx_[construct,destruct] method.
1257 bool HasCXXStructors : 1;
1259 /// true of class extension has at least one bitfield ivar.
1260 bool HasSynthBitfield : 1;
1262 ObjCImplementationDecl(DeclContext *DC, SourceLocation L,
1263 ObjCInterfaceDecl *classInterface,
1264 ObjCInterfaceDecl *superDecl)
1265 : ObjCImplDecl(ObjCImplementation, DC, L, classInterface),
1266 SuperClass(superDecl), IvarInitializers(0), NumIvarInitializers(0),
1267 HasCXXStructors(false), HasSynthBitfield(false) {}
1269 static ObjCImplementationDecl *Create(ASTContext &C, DeclContext *DC,
1271 ObjCInterfaceDecl *classInterface,
1272 ObjCInterfaceDecl *superDecl);
1274 /// init_iterator - Iterates through the ivar initializer list.
1275 typedef CXXCtorInitializer **init_iterator;
1277 /// init_const_iterator - Iterates through the ivar initializer list.
1278 typedef CXXCtorInitializer * const * init_const_iterator;
1280 /// init_begin() - Retrieve an iterator to the first initializer.
1281 init_iterator init_begin() { return IvarInitializers; }
1282 /// begin() - Retrieve an iterator to the first initializer.
1283 init_const_iterator init_begin() const { return IvarInitializers; }
1285 /// init_end() - Retrieve an iterator past the last initializer.
1286 init_iterator init_end() {
1287 return IvarInitializers + NumIvarInitializers;
1289 /// end() - Retrieve an iterator past the last initializer.
1290 init_const_iterator init_end() const {
1291 return IvarInitializers + NumIvarInitializers;
1293 /// getNumArgs - Number of ivars which must be initialized.
1294 unsigned getNumIvarInitializers() const {
1295 return NumIvarInitializers;
1298 void setNumIvarInitializers(unsigned numNumIvarInitializers) {
1299 NumIvarInitializers = numNumIvarInitializers;
1302 void setIvarInitializers(ASTContext &C,
1303 CXXCtorInitializer ** initializers,
1304 unsigned numInitializers);
1306 bool hasCXXStructors() const { return HasCXXStructors; }
1307 void setHasCXXStructors(bool val) { HasCXXStructors = val; }
1309 bool hasSynthBitfield() const { return HasSynthBitfield; }
1310 void setHasSynthBitfield (bool val) { HasSynthBitfield = val; }
1312 /// getIdentifier - Get the identifier that names the class
1313 /// interface associated with this implementation.
1314 IdentifierInfo *getIdentifier() const {
1315 return getClassInterface()->getIdentifier();
1318 /// getName - Get the name of identifier for the class interface associated
1319 /// with this implementation as a StringRef.
1321 // FIXME: This is a bad API, we are overriding the NamedDecl::getName, to mean
1322 // something different.
1323 llvm::StringRef getName() const {
1324 assert(getIdentifier() && "Name is not a simple identifier");
1325 return getIdentifier()->getName();
1328 /// getNameAsCString - Get the name of identifier for the class
1329 /// interface associated with this implementation as a C string
1332 // FIXME: Move to StringRef API.
1333 const char *getNameAsCString() const {
1334 return getName().data();
1337 /// @brief Get the name of the class associated with this interface.
1339 // FIXME: Move to StringRef API.
1340 std::string getNameAsString() const {
1344 const ObjCInterfaceDecl *getSuperClass() const { return SuperClass; }
1345 ObjCInterfaceDecl *getSuperClass() { return SuperClass; }
1347 void setSuperClass(ObjCInterfaceDecl * superCls) { SuperClass = superCls; }
1349 typedef specific_decl_iterator<ObjCIvarDecl> ivar_iterator;
1350 ivar_iterator ivar_begin() const {
1351 return ivar_iterator(decls_begin());
1353 ivar_iterator ivar_end() const {
1354 return ivar_iterator(decls_end());
1356 unsigned ivar_size() const {
1357 return std::distance(ivar_begin(), ivar_end());
1359 bool ivar_empty() const {
1360 return ivar_begin() == ivar_end();
1363 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1364 static bool classof(const ObjCImplementationDecl *D) { return true; }
1365 static bool classofKind(Kind K) { return K == ObjCImplementation; }
1367 friend class ASTDeclReader;
1368 friend class ASTDeclWriter;
1371 llvm::raw_ostream &operator<<(llvm::raw_ostream &OS,
1372 const ObjCImplementationDecl *ID);
1374 /// ObjCCompatibleAliasDecl - Represents alias of a class. This alias is
1375 /// declared as @compatibility_alias alias class.
1376 class ObjCCompatibleAliasDecl : public NamedDecl {
1377 /// Class that this is an alias of.
1378 ObjCInterfaceDecl *AliasedClass;
1380 ObjCCompatibleAliasDecl(DeclContext *DC, SourceLocation L, IdentifierInfo *Id,
1381 ObjCInterfaceDecl* aliasedClass)
1382 : NamedDecl(ObjCCompatibleAlias, DC, L, Id), AliasedClass(aliasedClass) {}
1384 static ObjCCompatibleAliasDecl *Create(ASTContext &C, DeclContext *DC,
1385 SourceLocation L, IdentifierInfo *Id,
1386 ObjCInterfaceDecl* aliasedClass);
1388 const ObjCInterfaceDecl *getClassInterface() const { return AliasedClass; }
1389 ObjCInterfaceDecl *getClassInterface() { return AliasedClass; }
1390 void setClassInterface(ObjCInterfaceDecl *D) { AliasedClass = D; }
1392 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1393 static bool classof(const ObjCCompatibleAliasDecl *D) { return true; }
1394 static bool classofKind(Kind K) { return K == ObjCCompatibleAlias; }
1398 /// ObjCPropertyDecl - Represents one property declaration in an interface.
1400 /// @property (assign, readwrite) int MyProperty;
1402 class ObjCPropertyDecl : public NamedDecl {
1404 enum PropertyAttributeKind {
1405 OBJC_PR_noattr = 0x00,
1406 OBJC_PR_readonly = 0x01,
1407 OBJC_PR_getter = 0x02,
1408 OBJC_PR_assign = 0x04,
1409 OBJC_PR_readwrite = 0x08,
1410 OBJC_PR_retain = 0x10,
1411 OBJC_PR_copy = 0x20,
1412 OBJC_PR_nonatomic = 0x40,
1413 OBJC_PR_setter = 0x80,
1414 OBJC_PR_atomic = 0x100,
1415 OBJC_PR_weak = 0x200,
1416 OBJC_PR_strong = 0x400,
1417 OBJC_PR_unsafe_unretained = 0x800
1418 // Adding a property should change NumPropertyAttrsBits
1422 /// \brief Number of bits fitting all the property attributes.
1423 NumPropertyAttrsBits = 12
1426 enum SetterKind { Assign, Retain, Copy };
1427 enum PropertyControl { None, Required, Optional };
1429 SourceLocation AtLoc; // location of @property
1430 TypeSourceInfo *DeclType;
1431 unsigned PropertyAttributes : NumPropertyAttrsBits;
1432 unsigned PropertyAttributesAsWritten : NumPropertyAttrsBits;
1433 // @required/@optional
1434 unsigned PropertyImplementation : 2;
1436 Selector GetterName; // getter name of NULL if no getter
1437 Selector SetterName; // setter name of NULL if no setter
1439 ObjCMethodDecl *GetterMethodDecl; // Declaration of getter instance method
1440 ObjCMethodDecl *SetterMethodDecl; // Declaration of setter instance method
1441 ObjCIvarDecl *PropertyIvarDecl; // Synthesize ivar for this property
1443 ObjCPropertyDecl(DeclContext *DC, SourceLocation L, IdentifierInfo *Id,
1444 SourceLocation AtLocation, TypeSourceInfo *T)
1445 : NamedDecl(ObjCProperty, DC, L, Id), AtLoc(AtLocation), DeclType(T),
1446 PropertyAttributes(OBJC_PR_noattr),
1447 PropertyAttributesAsWritten(OBJC_PR_noattr),
1448 PropertyImplementation(None),
1449 GetterName(Selector()),
1450 SetterName(Selector()),
1451 GetterMethodDecl(0), SetterMethodDecl(0) , PropertyIvarDecl(0) {}
1453 static ObjCPropertyDecl *Create(ASTContext &C, DeclContext *DC,
1455 IdentifierInfo *Id, SourceLocation AtLocation,
1457 PropertyControl propControl = None);
1458 SourceLocation getAtLoc() const { return AtLoc; }
1459 void setAtLoc(SourceLocation L) { AtLoc = L; }
1461 TypeSourceInfo *getTypeSourceInfo() const { return DeclType; }
1462 QualType getType() const { return DeclType->getType(); }
1463 void setType(TypeSourceInfo *T) { DeclType = T; }
1465 PropertyAttributeKind getPropertyAttributes() const {
1466 return PropertyAttributeKind(PropertyAttributes);
1468 void setPropertyAttributes(PropertyAttributeKind PRVal) {
1469 PropertyAttributes |= PRVal;
1472 PropertyAttributeKind getPropertyAttributesAsWritten() const {
1473 return PropertyAttributeKind(PropertyAttributesAsWritten);
1476 bool hasWrittenStorageAttribute() const {
1477 return PropertyAttributesAsWritten & (OBJC_PR_assign | OBJC_PR_copy |
1478 OBJC_PR_unsafe_unretained | OBJC_PR_retain | OBJC_PR_strong |
1482 void setPropertyAttributesAsWritten(PropertyAttributeKind PRVal) {
1483 PropertyAttributesAsWritten = PRVal;
1486 void makeitReadWriteAttribute(void) {
1487 PropertyAttributes &= ~OBJC_PR_readonly;
1488 PropertyAttributes |= OBJC_PR_readwrite;
1491 // Helper methods for accessing attributes.
1493 /// isReadOnly - Return true iff the property has a setter.
1494 bool isReadOnly() const {
1495 return (PropertyAttributes & OBJC_PR_readonly);
1498 /// getSetterKind - Return the method used for doing assignment in
1499 /// the property setter. This is only valid if the property has been
1500 /// defined to have a setter.
1501 SetterKind getSetterKind() const {
1502 if (PropertyAttributes & (OBJC_PR_retain|OBJC_PR_strong))
1504 if (PropertyAttributes & OBJC_PR_copy)
1509 Selector getGetterName() const { return GetterName; }
1510 void setGetterName(Selector Sel) { GetterName = Sel; }
1512 Selector getSetterName() const { return SetterName; }
1513 void setSetterName(Selector Sel) { SetterName = Sel; }
1515 ObjCMethodDecl *getGetterMethodDecl() const { return GetterMethodDecl; }
1516 void setGetterMethodDecl(ObjCMethodDecl *gDecl) { GetterMethodDecl = gDecl; }
1518 ObjCMethodDecl *getSetterMethodDecl() const { return SetterMethodDecl; }
1519 void setSetterMethodDecl(ObjCMethodDecl *gDecl) { SetterMethodDecl = gDecl; }
1521 // Related to @optional/@required declared in @protocol
1522 void setPropertyImplementation(PropertyControl pc) {
1523 PropertyImplementation = pc;
1525 PropertyControl getPropertyImplementation() const {
1526 return PropertyControl(PropertyImplementation);
1529 void setPropertyIvarDecl(ObjCIvarDecl *Ivar) {
1530 PropertyIvarDecl = Ivar;
1532 ObjCIvarDecl *getPropertyIvarDecl() const {
1533 return PropertyIvarDecl;
1536 virtual SourceRange getSourceRange() const {
1537 return SourceRange(AtLoc, getLocation());
1540 /// Lookup a property by name in the specified DeclContext.
1541 static ObjCPropertyDecl *findPropertyDecl(const DeclContext *DC,
1542 IdentifierInfo *propertyID);
1544 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1545 static bool classof(const ObjCPropertyDecl *D) { return true; }
1546 static bool classofKind(Kind K) { return K == ObjCProperty; }
1549 /// ObjCPropertyImplDecl - Represents implementation declaration of a property
1550 /// in a class or category implementation block. For example:
1551 /// @synthesize prop1 = ivar1;
1553 class ObjCPropertyImplDecl : public Decl {
1560 SourceLocation AtLoc; // location of @synthesize or @dynamic
1562 /// \brief For @synthesize, the location of the ivar, if it was written in
1563 /// the source code.
1566 /// @synthesize int a = b
1568 SourceLocation IvarLoc;
1570 /// Property declaration being implemented
1571 ObjCPropertyDecl *PropertyDecl;
1573 /// Null for @dynamic. Required for @synthesize.
1574 ObjCIvarDecl *PropertyIvarDecl;
1576 /// Null for @dynamic. Non-null if property must be copy-constructed in getter
1577 Expr *GetterCXXConstructor;
1579 /// Null for @dynamic. Non-null if property has assignment operator to call
1580 /// in Setter synthesis.
1581 Expr *SetterCXXAssignment;
1583 ObjCPropertyImplDecl(DeclContext *DC, SourceLocation atLoc, SourceLocation L,
1584 ObjCPropertyDecl *property,
1586 ObjCIvarDecl *ivarDecl,
1587 SourceLocation ivarLoc)
1588 : Decl(ObjCPropertyImpl, DC, L), AtLoc(atLoc),
1589 IvarLoc(ivarLoc), PropertyDecl(property), PropertyIvarDecl(ivarDecl),
1590 GetterCXXConstructor(0), SetterCXXAssignment(0) {
1591 assert (PK == Dynamic || PropertyIvarDecl);
1595 static ObjCPropertyImplDecl *Create(ASTContext &C, DeclContext *DC,
1596 SourceLocation atLoc, SourceLocation L,
1597 ObjCPropertyDecl *property,
1599 ObjCIvarDecl *ivarDecl,
1600 SourceLocation ivarLoc);
1602 virtual SourceRange getSourceRange() const;
1604 SourceLocation getLocStart() const { return AtLoc; }
1605 void setAtLoc(SourceLocation Loc) { AtLoc = Loc; }
1607 ObjCPropertyDecl *getPropertyDecl() const {
1608 return PropertyDecl;
1610 void setPropertyDecl(ObjCPropertyDecl *Prop) { PropertyDecl = Prop; }
1612 Kind getPropertyImplementation() const {
1613 return PropertyIvarDecl ? Synthesize : Dynamic;
1616 ObjCIvarDecl *getPropertyIvarDecl() const {
1617 return PropertyIvarDecl;
1619 SourceLocation getPropertyIvarDeclLoc() const { return IvarLoc; }
1621 void setPropertyIvarDecl(ObjCIvarDecl *Ivar,
1622 SourceLocation IvarLoc) {
1623 PropertyIvarDecl = Ivar;
1624 this->IvarLoc = IvarLoc;
1627 Expr *getGetterCXXConstructor() const {
1628 return GetterCXXConstructor;
1630 void setGetterCXXConstructor(Expr *getterCXXConstructor) {
1631 GetterCXXConstructor = getterCXXConstructor;
1634 Expr *getSetterCXXAssignment() const {
1635 return SetterCXXAssignment;
1637 void setSetterCXXAssignment(Expr *setterCXXAssignment) {
1638 SetterCXXAssignment = setterCXXAssignment;
1641 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1642 static bool classof(const ObjCPropertyImplDecl *D) { return true; }
1643 static bool classofKind(Decl::Kind K) { return K == ObjCPropertyImpl; }
1645 friend class ASTDeclReader;
1648 } // end namespace clang