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"
28 class ObjCProtocolDecl;
29 class ObjCCategoryDecl;
30 class ObjCPropertyDecl;
31 class ObjCPropertyImplDecl;
32 class CXXCtorInitializer;
35 void operator=(const ObjCListBase &); // DO NOT IMPLEMENT
36 ObjCListBase(const ObjCListBase&); // DO NOT IMPLEMENT
38 /// List is an array of pointers to objects that are not owned by this object.
43 ObjCListBase() : List(0), NumElts(0) {}
44 unsigned size() const { return NumElts; }
45 bool empty() const { return NumElts == 0; }
48 void set(void *const* InList, unsigned Elts, ASTContext &Ctx);
52 /// ObjCList - This is a simple template class used to hold various lists of
53 /// decls etc, which is heavily used by the ObjC front-end. This only use case
54 /// this supports is setting the list all at once and then reading elements out
57 class ObjCList : public ObjCListBase {
59 void set(T* const* InList, unsigned Elts, ASTContext &Ctx) {
60 ObjCListBase::set(reinterpret_cast<void*const*>(InList), Elts, Ctx);
63 typedef T* const * iterator;
64 iterator begin() const { return (iterator)List; }
65 iterator end() const { return (iterator)List+NumElts; }
67 T* operator[](unsigned Idx) const {
68 assert(Idx < NumElts && "Invalid access");
73 /// \brief A list of Objective-C protocols, along with the source
74 /// locations at which they were referenced.
75 class ObjCProtocolList : public ObjCList<ObjCProtocolDecl> {
76 SourceLocation *Locations;
78 using ObjCList<ObjCProtocolDecl>::set;
81 ObjCProtocolList() : ObjCList<ObjCProtocolDecl>(), Locations(0) { }
83 typedef const SourceLocation *loc_iterator;
84 loc_iterator loc_begin() const { return Locations; }
85 loc_iterator loc_end() const { return Locations + size(); }
87 void set(ObjCProtocolDecl* const* InList, unsigned Elts,
88 const SourceLocation *Locs, ASTContext &Ctx);
92 /// ObjCMethodDecl - Represents an instance or class method declaration.
93 /// ObjC methods can be declared within 4 contexts: class interfaces,
94 /// categories, protocols, and class implementations. While C++ member
95 /// functions leverage C syntax, Objective-C method syntax is modeled after
96 /// Smalltalk (using colons to specify argument types/expressions).
97 /// Here are some brief examples:
99 /// Setter/getter instance methods:
100 /// - (void)setMenu:(NSMenu *)menu;
101 /// - (NSMenu *)menu;
103 /// Instance method that takes 2 NSView arguments:
104 /// - (void)replaceSubview:(NSView *)oldView with:(NSView *)newView;
106 /// Getter class method:
107 /// + (NSMenu *)defaultMenu;
109 /// A selector represents a unique name for a method. The selector names for
110 /// the above methods are setMenu:, menu, replaceSubview:with:, and defaultMenu.
112 class ObjCMethodDecl : public NamedDecl, public DeclContext {
114 enum ImplementationControl { None, Required, Optional };
116 // The conventional meaning of this method; an ObjCMethodFamily.
117 // This is not serialized; instead, it is computed on demand and
119 mutable unsigned Family : ObjCMethodFamilyBitWidth;
121 /// instance (true) or class (false) method.
122 unsigned IsInstance : 1;
123 unsigned IsVariadic : 1;
125 // Synthesized declaration method for a property setter/getter
126 unsigned IsSynthesized : 1;
128 // Method has a definition.
129 unsigned IsDefined : 1;
131 /// \brief Method redeclaration in the same interface.
132 unsigned IsRedeclaration : 1;
134 /// \brief Is redeclared in the same interface.
135 mutable unsigned HasRedeclaration : 1;
137 // NOTE: VC++ treats enums as signed, avoid using ImplementationControl enum
138 /// @required/@optional
139 unsigned DeclImplementation : 2;
141 // NOTE: VC++ treats enums as signed, avoid using the ObjCDeclQualifier enum
143 unsigned objcDeclQualifier : 6;
145 /// \brief Indicates whether this method has a related result type.
146 unsigned RelatedResultType : 1;
148 /// \brief Whether the locations of the selector identifiers are in a
149 /// "standard" position, a enum SelectorLocationsKind.
150 unsigned SelLocsKind : 2;
152 // Result type of this method.
153 QualType MethodDeclType;
155 // Type source information for the result type.
156 TypeSourceInfo *ResultTInfo;
158 /// \brief Array of ParmVarDecls for the formal parameters of this method
159 /// and optionally followed by selector locations.
160 void *ParamsAndSelLocs;
163 /// List of attributes for this method declaration.
164 SourceLocation EndLoc; // the location of the ';' or '}'.
166 // The following are only used for method definitions, null otherwise.
167 // FIXME: space savings opportunity, consider a sub-class.
170 /// SelfDecl - Decl for the implicit self parameter. This is lazily
171 /// constructed by createImplicitParams.
172 ImplicitParamDecl *SelfDecl;
173 /// CmdDecl - Decl for the implicit _cmd parameter. This is lazily
174 /// constructed by createImplicitParams.
175 ImplicitParamDecl *CmdDecl;
177 SelectorLocationsKind getSelLocsKind() const {
178 return (SelectorLocationsKind)SelLocsKind;
180 bool hasStandardSelLocs() const {
181 return getSelLocsKind() != SelLoc_NonStandard;
184 /// \brief Get a pointer to the stored selector identifiers locations array.
185 /// No locations will be stored if HasStandardSelLocs is true.
186 SourceLocation *getStoredSelLocs() {
187 return reinterpret_cast<SourceLocation*>(getParams() + NumParams);
189 const SourceLocation *getStoredSelLocs() const {
190 return reinterpret_cast<const SourceLocation*>(getParams() + NumParams);
193 /// \brief Get a pointer to the stored selector identifiers locations array.
194 /// No locations will be stored if HasStandardSelLocs is true.
195 ParmVarDecl **getParams() {
196 return reinterpret_cast<ParmVarDecl **>(ParamsAndSelLocs);
198 const ParmVarDecl *const *getParams() const {
199 return reinterpret_cast<const ParmVarDecl *const *>(ParamsAndSelLocs);
202 /// \brief Get the number of stored selector identifiers locations.
203 /// No locations will be stored if HasStandardSelLocs is true.
204 unsigned getNumStoredSelLocs() const {
205 if (hasStandardSelLocs())
207 return getNumSelectorLocs();
210 void setParamsAndSelLocs(ASTContext &C,
211 ArrayRef<ParmVarDecl*> Params,
212 ArrayRef<SourceLocation> SelLocs);
214 ObjCMethodDecl(SourceLocation beginLoc, SourceLocation endLoc,
215 Selector SelInfo, QualType T,
216 TypeSourceInfo *ResultTInfo,
217 DeclContext *contextDecl,
218 bool isInstance = true,
219 bool isVariadic = false,
220 bool isSynthesized = false,
221 bool isImplicitlyDeclared = false,
222 bool isDefined = false,
223 ImplementationControl impControl = None,
224 bool HasRelatedResultType = false)
225 : NamedDecl(ObjCMethod, contextDecl, beginLoc, SelInfo),
226 DeclContext(ObjCMethod), Family(InvalidObjCMethodFamily),
227 IsInstance(isInstance), IsVariadic(isVariadic),
228 IsSynthesized(isSynthesized),
229 IsDefined(isDefined), IsRedeclaration(0), HasRedeclaration(0),
230 DeclImplementation(impControl), objcDeclQualifier(OBJC_TQ_None),
231 RelatedResultType(HasRelatedResultType),
232 SelLocsKind(SelLoc_StandardNoSpace),
233 MethodDeclType(T), ResultTInfo(ResultTInfo),
234 ParamsAndSelLocs(0), NumParams(0),
235 EndLoc(endLoc), Body(0), SelfDecl(0), CmdDecl(0) {
236 setImplicit(isImplicitlyDeclared);
239 /// \brief A definition will return its interface declaration.
240 /// An interface declaration will return its definition.
241 /// Otherwise it will return itself.
242 virtual ObjCMethodDecl *getNextRedeclaration();
245 static ObjCMethodDecl *Create(ASTContext &C,
246 SourceLocation beginLoc,
247 SourceLocation endLoc,
250 TypeSourceInfo *ResultTInfo,
251 DeclContext *contextDecl,
252 bool isInstance = true,
253 bool isVariadic = false,
254 bool isSynthesized = false,
255 bool isImplicitlyDeclared = false,
256 bool isDefined = false,
257 ImplementationControl impControl = None,
258 bool HasRelatedResultType = false);
260 virtual ObjCMethodDecl *getCanonicalDecl();
261 const ObjCMethodDecl *getCanonicalDecl() const {
262 return const_cast<ObjCMethodDecl*>(this)->getCanonicalDecl();
265 ObjCDeclQualifier getObjCDeclQualifier() const {
266 return ObjCDeclQualifier(objcDeclQualifier);
268 void setObjCDeclQualifier(ObjCDeclQualifier QV) { objcDeclQualifier = QV; }
270 /// \brief Determine whether this method has a result type that is related
271 /// to the message receiver's type.
272 bool hasRelatedResultType() const { return RelatedResultType; }
274 /// \brief Note whether this method has a related result type.
275 void SetRelatedResultType(bool RRT = true) { RelatedResultType = RRT; }
277 /// \brief True if this is a method redeclaration in the same interface.
278 bool isRedeclaration() const { return IsRedeclaration; }
279 void setAsRedeclaration(const ObjCMethodDecl *PrevMethod);
281 // Location information, modeled after the Stmt API.
282 SourceLocation getLocStart() const { return getLocation(); }
283 SourceLocation getLocEnd() const { return EndLoc; }
284 void setEndLoc(SourceLocation Loc) { EndLoc = Loc; }
285 virtual SourceRange getSourceRange() const {
286 return SourceRange(getLocation(), EndLoc);
289 SourceLocation getSelectorStartLoc() const { return getSelectorLoc(0); }
290 SourceLocation getSelectorLoc(unsigned Index) const {
291 assert(Index < getNumSelectorLocs() && "Index out of range!");
292 if (hasStandardSelLocs())
293 return getStandardSelectorLoc(Index, getSelector(),
294 getSelLocsKind() == SelLoc_StandardWithSpace,
295 llvm::makeArrayRef(const_cast<ParmVarDecl**>(getParams()),
298 return getStoredSelLocs()[Index];
301 void getSelectorLocs(SmallVectorImpl<SourceLocation> &SelLocs) const;
303 unsigned getNumSelectorLocs() const {
306 Selector Sel = getSelector();
307 if (Sel.isUnarySelector())
309 return Sel.getNumArgs();
312 ObjCInterfaceDecl *getClassInterface();
313 const ObjCInterfaceDecl *getClassInterface() const {
314 return const_cast<ObjCMethodDecl*>(this)->getClassInterface();
317 Selector getSelector() const { return getDeclName().getObjCSelector(); }
319 QualType getResultType() const { return MethodDeclType; }
320 void setResultType(QualType T) { MethodDeclType = T; }
322 /// \brief Determine the type of an expression that sends a message to this
324 QualType getSendResultType() const {
325 return getResultType().getNonLValueExprType(getASTContext());
328 TypeSourceInfo *getResultTypeSourceInfo() const { return ResultTInfo; }
329 void setResultTypeSourceInfo(TypeSourceInfo *TInfo) { ResultTInfo = TInfo; }
331 // Iterator access to formal parameters.
332 unsigned param_size() const { return NumParams; }
333 typedef const ParmVarDecl *const *param_const_iterator;
334 typedef ParmVarDecl *const *param_iterator;
335 param_const_iterator param_begin() const { return getParams(); }
336 param_const_iterator param_end() const { return getParams() + NumParams; }
337 param_iterator param_begin() { return getParams(); }
338 param_iterator param_end() { return getParams() + NumParams; }
339 // This method returns and of the parameters which are part of the selector
340 // name mangling requirements.
341 param_const_iterator sel_param_end() const {
342 return param_begin() + getSelector().getNumArgs();
345 /// \brief Sets the method's parameters and selector source locations.
346 /// If the method is implicit (not coming from source) \arg SelLocs is
348 void setMethodParams(ASTContext &C,
349 ArrayRef<ParmVarDecl*> Params,
350 ArrayRef<SourceLocation> SelLocs =
351 ArrayRef<SourceLocation>());
353 // Iterator access to parameter types.
354 typedef std::const_mem_fun_t<QualType, ParmVarDecl> deref_fun;
355 typedef llvm::mapped_iterator<param_const_iterator, deref_fun>
358 arg_type_iterator arg_type_begin() const {
359 return llvm::map_iterator(param_begin(), deref_fun(&ParmVarDecl::getType));
361 arg_type_iterator arg_type_end() const {
362 return llvm::map_iterator(param_end(), deref_fun(&ParmVarDecl::getType));
365 /// createImplicitParams - Used to lazily create the self and cmd
366 /// implict parameters. This must be called prior to using getSelfDecl()
367 /// or getCmdDecl(). The call is ignored if the implicit paramters
368 /// have already been created.
369 void createImplicitParams(ASTContext &Context, const ObjCInterfaceDecl *ID);
371 ImplicitParamDecl * getSelfDecl() const { return SelfDecl; }
372 void setSelfDecl(ImplicitParamDecl *SD) { SelfDecl = SD; }
373 ImplicitParamDecl * getCmdDecl() const { return CmdDecl; }
374 void setCmdDecl(ImplicitParamDecl *CD) { CmdDecl = CD; }
376 /// Determines the family of this method.
377 ObjCMethodFamily getMethodFamily() const;
379 bool isInstanceMethod() const { return IsInstance; }
380 void setInstanceMethod(bool isInst) { IsInstance = isInst; }
381 bool isVariadic() const { return IsVariadic; }
382 void setVariadic(bool isVar) { IsVariadic = isVar; }
384 bool isClassMethod() const { return !IsInstance; }
386 bool isSynthesized() const { return IsSynthesized; }
387 void setSynthesized(bool isSynth) { IsSynthesized = isSynth; }
389 bool isDefined() const { return IsDefined; }
390 void setDefined(bool isDefined) { IsDefined = isDefined; }
392 // Related to protocols declared in @protocol
393 void setDeclImplementation(ImplementationControl ic) {
394 DeclImplementation = ic;
396 ImplementationControl getImplementationControl() const {
397 return ImplementationControl(DeclImplementation);
400 virtual Stmt *getBody() const {
403 CompoundStmt *getCompoundBody() { return (CompoundStmt*)Body; }
404 void setBody(Stmt *B) { Body = B; }
406 /// \brief Returns whether this specific method is a definition.
407 bool isThisDeclarationADefinition() const { return Body; }
409 // Implement isa/cast/dyncast/etc.
410 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
411 static bool classof(const ObjCMethodDecl *D) { return true; }
412 static bool classofKind(Kind K) { return K == ObjCMethod; }
413 static DeclContext *castToDeclContext(const ObjCMethodDecl *D) {
414 return static_cast<DeclContext *>(const_cast<ObjCMethodDecl*>(D));
416 static ObjCMethodDecl *castFromDeclContext(const DeclContext *DC) {
417 return static_cast<ObjCMethodDecl *>(const_cast<DeclContext*>(DC));
420 friend class ASTDeclReader;
421 friend class ASTDeclWriter;
424 /// ObjCContainerDecl - Represents a container for method declarations.
425 /// Current sub-classes are ObjCInterfaceDecl, ObjCCategoryDecl,
426 /// ObjCProtocolDecl, and ObjCImplDecl.
428 class ObjCContainerDecl : public NamedDecl, public DeclContext {
429 SourceLocation AtStart;
431 // These two locations in the range mark the end of the method container.
432 // The first points to the '@' token, and the second to the 'end' token.
436 ObjCContainerDecl(Kind DK, DeclContext *DC,
437 IdentifierInfo *Id, SourceLocation nameLoc,
438 SourceLocation atStartLoc)
439 : NamedDecl(DK, DC, nameLoc, Id), DeclContext(DK), AtStart(atStartLoc) {}
441 // Iterator access to properties.
442 typedef specific_decl_iterator<ObjCPropertyDecl> prop_iterator;
443 prop_iterator prop_begin() const {
444 return prop_iterator(decls_begin());
446 prop_iterator prop_end() const {
447 return prop_iterator(decls_end());
450 // Iterator access to instance/class methods.
451 typedef specific_decl_iterator<ObjCMethodDecl> method_iterator;
452 method_iterator meth_begin() const {
453 return method_iterator(decls_begin());
455 method_iterator meth_end() const {
456 return method_iterator(decls_end());
459 typedef filtered_decl_iterator<ObjCMethodDecl,
460 &ObjCMethodDecl::isInstanceMethod>
462 instmeth_iterator instmeth_begin() const {
463 return instmeth_iterator(decls_begin());
465 instmeth_iterator instmeth_end() const {
466 return instmeth_iterator(decls_end());
469 typedef filtered_decl_iterator<ObjCMethodDecl,
470 &ObjCMethodDecl::isClassMethod>
472 classmeth_iterator classmeth_begin() const {
473 return classmeth_iterator(decls_begin());
475 classmeth_iterator classmeth_end() const {
476 return classmeth_iterator(decls_end());
479 // Get the local instance/class method declared in this interface.
480 ObjCMethodDecl *getMethod(Selector Sel, bool isInstance) const;
481 ObjCMethodDecl *getInstanceMethod(Selector Sel) const {
482 return getMethod(Sel, true/*isInstance*/);
484 ObjCMethodDecl *getClassMethod(Selector Sel) const {
485 return getMethod(Sel, false/*isInstance*/);
487 ObjCIvarDecl *getIvarDecl(IdentifierInfo *Id) const;
489 ObjCPropertyDecl *FindPropertyDeclaration(IdentifierInfo *PropertyId) const;
491 SourceLocation getAtStartLoc() const { return AtStart; }
492 void setAtStartLoc(SourceLocation Loc) { AtStart = Loc; }
494 // Marks the end of the container.
495 SourceRange getAtEndRange() const {
498 void setAtEndRange(SourceRange atEnd) {
502 virtual SourceRange getSourceRange() const {
503 return SourceRange(AtStart, getAtEndRange().getEnd());
506 // Implement isa/cast/dyncast/etc.
507 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
508 static bool classof(const ObjCContainerDecl *D) { return true; }
509 static bool classofKind(Kind K) {
510 return K >= firstObjCContainer &&
511 K <= lastObjCContainer;
514 static DeclContext *castToDeclContext(const ObjCContainerDecl *D) {
515 return static_cast<DeclContext *>(const_cast<ObjCContainerDecl*>(D));
517 static ObjCContainerDecl *castFromDeclContext(const DeclContext *DC) {
518 return static_cast<ObjCContainerDecl *>(const_cast<DeclContext*>(DC));
522 /// ObjCInterfaceDecl - Represents an ObjC class declaration. For example:
524 /// // MostPrimitive declares no super class (not particularly useful).
525 /// @interface MostPrimitive
526 /// // no instance variables or methods.
529 /// // NSResponder inherits from NSObject & implements NSCoding (a protocol).
530 /// @interface NSResponder : NSObject <NSCoding>
531 /// { // instance variables are represented by ObjCIvarDecl.
532 /// id nextResponder; // nextResponder instance variable.
534 /// - (NSResponder *)nextResponder; // return a pointer to NSResponder.
535 /// - (void)mouseMoved:(NSEvent *)theEvent; // return void, takes a pointer
536 /// @end // to an NSEvent.
538 /// Unlike C/C++, forward class declarations are accomplished with @class.
539 /// Unlike C/C++, @class allows for a list of classes to be forward declared.
540 /// Unlike C++, ObjC is a single-rooted class model. In Cocoa, classes
541 /// typically inherit from NSObject (an exception is NSProxy).
543 class ObjCInterfaceDecl : public ObjCContainerDecl {
544 /// TypeForDecl - This indicates the Type object that represents this
545 /// TypeDecl. It is a cache maintained by ASTContext::getObjCInterfaceType
546 mutable const Type *TypeForDecl;
547 friend class ASTContext;
549 /// Class's super class.
550 ObjCInterfaceDecl *SuperClass;
552 /// Protocols referenced in the @interface declaration
553 ObjCProtocolList ReferencedProtocols;
555 /// Protocols reference in both the @interface and class extensions.
556 ObjCList<ObjCProtocolDecl> AllReferencedProtocols;
558 /// \brief List of categories and class extensions defined for this class.
560 /// Categories are stored as a linked list in the AST, since the categories
561 /// and class extensions come long after the initial interface declaration,
562 /// and we avoid dynamically-resized arrays in the AST wherever possible.
563 ObjCCategoryDecl *CategoryList;
565 /// IvarList - List of all ivars defined by this class; including class
566 /// extensions and implementation. This list is built lazily.
567 ObjCIvarDecl *IvarList;
569 bool ForwardDecl:1; // declared with @class.
570 bool InternalInterface:1; // true - no @interface for @implementation
572 /// \brief Indicates that the contents of this Objective-C class will be
573 /// completed by the external AST source when required.
574 mutable bool ExternallyCompleted : 1;
576 SourceLocation SuperClassLoc; // location of the super class identifier.
577 SourceLocation EndLoc; // marks the '>', '}', or identifier.
579 ObjCInterfaceDecl(DeclContext *DC, SourceLocation atLoc, IdentifierInfo *Id,
580 SourceLocation CLoc, bool FD, bool isInternal);
582 void LoadExternalDefinition() const;
584 static ObjCInterfaceDecl *Create(ASTContext &C, DeclContext *DC,
585 SourceLocation atLoc,
587 SourceLocation ClassLoc = SourceLocation(),
588 bool ForwardDecl = false,
589 bool isInternal = false);
591 /// \brief Indicate that this Objective-C class is complete, but that
592 /// the external AST source will be responsible for filling in its contents
593 /// when a complete class is required.
594 void setExternallyCompleted();
596 const ObjCProtocolList &getReferencedProtocols() const {
597 if (ExternallyCompleted)
598 LoadExternalDefinition();
600 return ReferencedProtocols;
603 ObjCImplementationDecl *getImplementation() const;
604 void setImplementation(ObjCImplementationDecl *ImplD);
606 ObjCCategoryDecl *FindCategoryDeclaration(IdentifierInfo *CategoryId) const;
608 // Get the local instance/class method declared in a category.
609 ObjCMethodDecl *getCategoryInstanceMethod(Selector Sel) const;
610 ObjCMethodDecl *getCategoryClassMethod(Selector Sel) const;
611 ObjCMethodDecl *getCategoryMethod(Selector Sel, bool isInstance) const {
612 return isInstance ? getInstanceMethod(Sel)
613 : getClassMethod(Sel);
616 typedef ObjCProtocolList::iterator protocol_iterator;
618 protocol_iterator protocol_begin() const {
619 if (ExternallyCompleted)
620 LoadExternalDefinition();
622 return ReferencedProtocols.begin();
624 protocol_iterator protocol_end() const {
625 if (ExternallyCompleted)
626 LoadExternalDefinition();
628 return ReferencedProtocols.end();
631 typedef ObjCProtocolList::loc_iterator protocol_loc_iterator;
633 protocol_loc_iterator protocol_loc_begin() const {
634 if (ExternallyCompleted)
635 LoadExternalDefinition();
637 return ReferencedProtocols.loc_begin();
640 protocol_loc_iterator protocol_loc_end() const {
641 if (ExternallyCompleted)
642 LoadExternalDefinition();
644 return ReferencedProtocols.loc_end();
647 typedef ObjCList<ObjCProtocolDecl>::iterator all_protocol_iterator;
649 all_protocol_iterator all_referenced_protocol_begin() const {
650 if (ExternallyCompleted)
651 LoadExternalDefinition();
653 return AllReferencedProtocols.empty() ? protocol_begin()
654 : AllReferencedProtocols.begin();
656 all_protocol_iterator all_referenced_protocol_end() const {
657 if (ExternallyCompleted)
658 LoadExternalDefinition();
660 return AllReferencedProtocols.empty() ? protocol_end()
661 : AllReferencedProtocols.end();
664 typedef specific_decl_iterator<ObjCIvarDecl> ivar_iterator;
666 ivar_iterator ivar_begin() const { return ivar_iterator(decls_begin()); }
667 ivar_iterator ivar_end() const { return ivar_iterator(decls_end()); }
669 unsigned ivar_size() const {
670 return std::distance(ivar_begin(), ivar_end());
673 bool ivar_empty() const { return ivar_begin() == ivar_end(); }
675 ObjCIvarDecl *all_declared_ivar_begin();
676 const ObjCIvarDecl *all_declared_ivar_begin() const {
677 // Even though this modifies IvarList, it's conceptually const:
678 // the ivar chain is essentially a cached property of ObjCInterfaceDecl.
679 return const_cast<ObjCInterfaceDecl *>(this)->all_declared_ivar_begin();
681 void setIvarList(ObjCIvarDecl *ivar) { IvarList = ivar; }
683 /// setProtocolList - Set the list of protocols that this interface
685 void setProtocolList(ObjCProtocolDecl *const* List, unsigned Num,
686 const SourceLocation *Locs, ASTContext &C) {
687 ReferencedProtocols.set(List, Num, Locs, C);
690 /// mergeClassExtensionProtocolList - Merge class extension's protocol list
691 /// into the protocol list for this class.
692 void mergeClassExtensionProtocolList(ObjCProtocolDecl *const* List,
696 bool isForwardDecl() const { return ForwardDecl; }
697 void setForwardDecl(bool val) { ForwardDecl = val; }
699 ObjCInterfaceDecl *getSuperClass() const {
700 if (ExternallyCompleted)
701 LoadExternalDefinition();
706 void setSuperClass(ObjCInterfaceDecl * superCls) { SuperClass = superCls; }
708 ObjCCategoryDecl* getCategoryList() const {
709 if (ExternallyCompleted)
710 LoadExternalDefinition();
715 void setCategoryList(ObjCCategoryDecl *category) {
716 CategoryList = category;
719 ObjCCategoryDecl* getFirstClassExtension() const;
722 *FindPropertyVisibleInPrimaryClass(IdentifierInfo *PropertyId) const;
724 /// isSuperClassOf - Return true if this class is the specified class or is a
725 /// super class of the specified interface class.
726 bool isSuperClassOf(const ObjCInterfaceDecl *I) const {
727 // If RHS is derived from LHS it is OK; else it is not OK.
731 I = I->getSuperClass();
736 /// isArcWeakrefUnavailable - Checks for a class or one of its super classes
737 /// to be incompatible with __weak references. Returns true if it is.
738 bool isArcWeakrefUnavailable() const {
739 const ObjCInterfaceDecl *Class = this;
741 if (Class->hasAttr<ArcWeakrefUnavailableAttr>())
743 Class = Class->getSuperClass();
748 ObjCIvarDecl *lookupInstanceVariable(IdentifierInfo *IVarName,
749 ObjCInterfaceDecl *&ClassDeclared);
750 ObjCIvarDecl *lookupInstanceVariable(IdentifierInfo *IVarName) {
751 ObjCInterfaceDecl *ClassDeclared;
752 return lookupInstanceVariable(IVarName, ClassDeclared);
755 // Lookup a method. First, we search locally. If a method isn't
756 // found, we search referenced protocols and class categories.
757 ObjCMethodDecl *lookupMethod(Selector Sel, bool isInstance) const;
758 ObjCMethodDecl *lookupInstanceMethod(Selector Sel) const {
759 return lookupMethod(Sel, true/*isInstance*/);
761 ObjCMethodDecl *lookupClassMethod(Selector Sel) const {
762 return lookupMethod(Sel, false/*isInstance*/);
764 ObjCInterfaceDecl *lookupInheritedClass(const IdentifierInfo *ICName);
766 // Lookup a method in the classes implementation hierarchy.
767 ObjCMethodDecl *lookupPrivateMethod(const Selector &Sel, bool Instance=true);
769 // Location information, modeled after the Stmt API.
770 SourceLocation getLocStart() const { return getAtStartLoc(); } // '@'interface
771 SourceLocation getLocEnd() const { return EndLoc; }
772 void setLocEnd(SourceLocation LE) { EndLoc = LE; }
774 void setSuperClassLoc(SourceLocation Loc) { SuperClassLoc = Loc; }
775 SourceLocation getSuperClassLoc() const { return SuperClassLoc; }
777 /// isImplicitInterfaceDecl - check that this is an implicitly declared
778 /// ObjCInterfaceDecl node. This is for legacy objective-c @implementation
779 /// declaration without an @interface declaration.
780 bool isImplicitInterfaceDecl() const { return InternalInterface; }
781 void setImplicitInterfaceDecl(bool val) { InternalInterface = val; }
783 /// ClassImplementsProtocol - Checks that 'lProto' protocol
784 /// has been implemented in IDecl class, its super class or categories (if
785 /// lookupCategory is true).
786 bool ClassImplementsProtocol(ObjCProtocolDecl *lProto,
788 bool RHSIsQualifiedID = false);
790 // Low-level accessor
791 const Type *getTypeForDecl() const { return TypeForDecl; }
792 void setTypeForDecl(const Type *TD) const { TypeForDecl = TD; }
794 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
795 static bool classof(const ObjCInterfaceDecl *D) { return true; }
796 static bool classofKind(Kind K) { return K == ObjCInterface; }
798 friend class ASTDeclReader;
799 friend class ASTDeclWriter;
802 /// ObjCIvarDecl - Represents an ObjC instance variable. In general, ObjC
803 /// instance variables are identical to C. The only exception is Objective-C
804 /// supports C++ style access control. For example:
806 /// @interface IvarExample : NSObject
808 /// id defaultToProtected;
810 /// id canBePublic; // same as C++.
812 /// id canBeProtected; // same as C++.
814 /// id canBePackage; // framework visibility (not available in C++).
817 class ObjCIvarDecl : public FieldDecl {
820 None, Private, Protected, Public, Package
824 ObjCIvarDecl(ObjCContainerDecl *DC, SourceLocation StartLoc,
825 SourceLocation IdLoc, IdentifierInfo *Id,
826 QualType T, TypeSourceInfo *TInfo, AccessControl ac, Expr *BW,
828 : FieldDecl(ObjCIvar, DC, StartLoc, IdLoc, Id, T, TInfo, BW,
829 /*Mutable=*/false, /*HasInit=*/false),
830 NextIvar(0), DeclAccess(ac), Synthesized(synthesized) {}
833 static ObjCIvarDecl *Create(ASTContext &C, ObjCContainerDecl *DC,
834 SourceLocation StartLoc, SourceLocation IdLoc,
835 IdentifierInfo *Id, QualType T,
836 TypeSourceInfo *TInfo,
837 AccessControl ac, Expr *BW = NULL,
838 bool synthesized=false);
840 /// \brief Return the class interface that this ivar is logically contained
841 /// in; this is either the interface where the ivar was declared, or the
842 /// interface the ivar is conceptually a part of in the case of synthesized
844 const ObjCInterfaceDecl *getContainingInterface() const;
846 ObjCIvarDecl *getNextIvar() { return NextIvar; }
847 const ObjCIvarDecl *getNextIvar() const { return NextIvar; }
848 void setNextIvar(ObjCIvarDecl *ivar) { NextIvar = ivar; }
850 void setAccessControl(AccessControl ac) { DeclAccess = ac; }
852 AccessControl getAccessControl() const { return AccessControl(DeclAccess); }
854 AccessControl getCanonicalAccessControl() const {
855 return DeclAccess == None ? Protected : AccessControl(DeclAccess);
858 void setSynthesize(bool synth) { Synthesized = synth; }
859 bool getSynthesize() const { return Synthesized; }
861 // Implement isa/cast/dyncast/etc.
862 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
863 static bool classof(const ObjCIvarDecl *D) { return true; }
864 static bool classofKind(Kind K) { return K == ObjCIvar; }
866 /// NextIvar - Next Ivar in the list of ivars declared in class; class's
867 /// extensions and class's implementation
868 ObjCIvarDecl *NextIvar;
870 // NOTE: VC++ treats enums as signed, avoid using the AccessControl enum
871 unsigned DeclAccess : 3;
872 unsigned Synthesized : 1;
876 /// ObjCAtDefsFieldDecl - Represents a field declaration created by an
878 class ObjCAtDefsFieldDecl : public FieldDecl {
880 ObjCAtDefsFieldDecl(DeclContext *DC, SourceLocation StartLoc,
881 SourceLocation IdLoc, IdentifierInfo *Id,
882 QualType T, Expr *BW)
883 : FieldDecl(ObjCAtDefsField, DC, StartLoc, IdLoc, Id, T,
884 /*TInfo=*/0, // FIXME: Do ObjCAtDefs have declarators ?
885 BW, /*Mutable=*/false, /*HasInit=*/false) {}
888 static ObjCAtDefsFieldDecl *Create(ASTContext &C, DeclContext *DC,
889 SourceLocation StartLoc,
890 SourceLocation IdLoc, IdentifierInfo *Id,
891 QualType T, Expr *BW);
893 // Implement isa/cast/dyncast/etc.
894 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
895 static bool classof(const ObjCAtDefsFieldDecl *D) { return true; }
896 static bool classofKind(Kind K) { return K == ObjCAtDefsField; }
899 /// ObjCProtocolDecl - Represents a protocol declaration. ObjC protocols
900 /// declare a pure abstract type (i.e no instance variables are permitted).
901 /// Protocols originally drew inspiration from C++ pure virtual functions (a C++
902 /// feature with nice semantics and lousy syntax:-). Here is an example:
904 /// @protocol NSDraggingInfo <refproto1, refproto2>
905 /// - (NSWindow *)draggingDestinationWindow;
906 /// - (NSImage *)draggedImage;
909 /// This says that NSDraggingInfo requires two methods and requires everything
910 /// that the two "referenced protocols" 'refproto1' and 'refproto2' require as
913 /// @interface ImplementsNSDraggingInfo : NSObject <NSDraggingInfo>
916 /// ObjC protocols inspired Java interfaces. Unlike Java, ObjC classes and
917 /// protocols are in distinct namespaces. For example, Cocoa defines both
918 /// an NSObject protocol and class (which isn't allowed in Java). As a result,
919 /// protocols are referenced using angle brackets as follows:
921 /// id <NSDraggingInfo> anyObjectThatImplementsNSDraggingInfo;
923 class ObjCProtocolDecl : public ObjCContainerDecl {
924 /// Referenced protocols
925 ObjCProtocolList ReferencedProtocols;
927 bool isForwardProtoDecl; // declared with @protocol.
929 SourceLocation EndLoc; // marks the '>' or identifier.
931 ObjCProtocolDecl(DeclContext *DC, IdentifierInfo *Id,
932 SourceLocation nameLoc, SourceLocation atStartLoc)
933 : ObjCContainerDecl(ObjCProtocol, DC, Id, nameLoc, atStartLoc),
934 isForwardProtoDecl(true) {
938 static ObjCProtocolDecl *Create(ASTContext &C, DeclContext *DC,
940 SourceLocation nameLoc,
941 SourceLocation atStartLoc);
943 const ObjCProtocolList &getReferencedProtocols() const {
944 return ReferencedProtocols;
946 typedef ObjCProtocolList::iterator protocol_iterator;
947 protocol_iterator protocol_begin() const {return ReferencedProtocols.begin();}
948 protocol_iterator protocol_end() const { return ReferencedProtocols.end(); }
949 typedef ObjCProtocolList::loc_iterator protocol_loc_iterator;
950 protocol_loc_iterator protocol_loc_begin() const {
951 return ReferencedProtocols.loc_begin();
953 protocol_loc_iterator protocol_loc_end() const {
954 return ReferencedProtocols.loc_end();
956 unsigned protocol_size() const { return ReferencedProtocols.size(); }
958 /// setProtocolList - Set the list of protocols that this interface
960 void setProtocolList(ObjCProtocolDecl *const*List, unsigned Num,
961 const SourceLocation *Locs, ASTContext &C) {
962 ReferencedProtocols.set(List, Num, Locs, C);
965 ObjCProtocolDecl *lookupProtocolNamed(IdentifierInfo *PName);
967 // Lookup a method. First, we search locally. If a method isn't
968 // found, we search referenced protocols and class categories.
969 ObjCMethodDecl *lookupMethod(Selector Sel, bool isInstance) const;
970 ObjCMethodDecl *lookupInstanceMethod(Selector Sel) const {
971 return lookupMethod(Sel, true/*isInstance*/);
973 ObjCMethodDecl *lookupClassMethod(Selector Sel) const {
974 return lookupMethod(Sel, false/*isInstance*/);
977 bool isForwardDecl() const { return isForwardProtoDecl; }
978 void setForwardDecl(bool val) { isForwardProtoDecl = val; }
980 // Location information, modeled after the Stmt API.
981 SourceLocation getLocStart() const { return getAtStartLoc(); } // '@'protocol
982 SourceLocation getLocEnd() const { return EndLoc; }
983 void setLocEnd(SourceLocation LE) { EndLoc = LE; }
985 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
986 static bool classof(const ObjCProtocolDecl *D) { return true; }
987 static bool classofKind(Kind K) { return K == ObjCProtocol; }
990 /// ObjCClassDecl - Specifies a list of forward class declarations. For example:
992 /// @class NSCursor, NSImage, NSPasteboard, NSWindow;
994 class ObjCClassDecl : public Decl {
997 ObjCInterfaceDecl *ID;
1000 ObjCClassRef(ObjCInterfaceDecl *d, SourceLocation l) : ID(d), L(l) {}
1001 SourceLocation getLocation() const { return L; }
1002 ObjCInterfaceDecl *getInterface() const { return ID; }
1005 ObjCClassRef *ForwardDecl;
1007 ObjCClassDecl(DeclContext *DC, SourceLocation L,
1008 ObjCInterfaceDecl *const Elt, const SourceLocation Loc,
1011 static ObjCClassDecl *Create(ASTContext &C, DeclContext *DC, SourceLocation L,
1012 ObjCInterfaceDecl *const Elt = 0,
1013 const SourceLocation Locs = SourceLocation());
1015 ObjCInterfaceDecl *getForwardInterfaceDecl() { return ForwardDecl->getInterface(); }
1016 ObjCClassRef *getForwardDecl() { return ForwardDecl; }
1017 void setClass(ASTContext &C, ObjCInterfaceDecl*const Cls,
1018 const SourceLocation Locs);
1020 virtual SourceRange getSourceRange() const;
1022 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1023 static bool classof(const ObjCClassDecl *D) { return true; }
1024 static bool classofKind(Kind K) { return K == ObjCClass; }
1027 /// ObjCForwardProtocolDecl - Specifies a list of forward protocol declarations.
1030 /// @protocol NSTextInput, NSChangeSpelling, NSDraggingInfo;
1032 class ObjCForwardProtocolDecl : public Decl {
1033 ObjCProtocolList ReferencedProtocols;
1035 ObjCForwardProtocolDecl(DeclContext *DC, SourceLocation L,
1036 ObjCProtocolDecl *const *Elts, unsigned nElts,
1037 const SourceLocation *Locs, ASTContext &C);
1040 static ObjCForwardProtocolDecl *Create(ASTContext &C, DeclContext *DC,
1042 ObjCProtocolDecl *const *Elts,
1044 const SourceLocation *Locs);
1046 static ObjCForwardProtocolDecl *Create(ASTContext &C, DeclContext *DC,
1048 return Create(C, DC, L, 0, 0, 0);
1051 typedef ObjCProtocolList::iterator protocol_iterator;
1052 protocol_iterator protocol_begin() const {return ReferencedProtocols.begin();}
1053 protocol_iterator protocol_end() const { return ReferencedProtocols.end(); }
1054 typedef ObjCProtocolList::loc_iterator protocol_loc_iterator;
1055 protocol_loc_iterator protocol_loc_begin() const {
1056 return ReferencedProtocols.loc_begin();
1058 protocol_loc_iterator protocol_loc_end() const {
1059 return ReferencedProtocols.loc_end();
1062 unsigned protocol_size() const { return ReferencedProtocols.size(); }
1064 /// setProtocolList - Set the list of forward protocols.
1065 void setProtocolList(ObjCProtocolDecl *const*List, unsigned Num,
1066 const SourceLocation *Locs, ASTContext &C) {
1067 ReferencedProtocols.set(List, Num, Locs, C);
1069 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1070 static bool classof(const ObjCForwardProtocolDecl *D) { return true; }
1071 static bool classofKind(Kind K) { return K == ObjCForwardProtocol; }
1074 /// ObjCCategoryDecl - Represents a category declaration. A category allows
1075 /// you to add methods to an existing class (without subclassing or modifying
1076 /// the original class interface or implementation:-). Categories don't allow
1077 /// you to add instance data. The following example adds "myMethod" to all
1078 /// NSView's within a process:
1080 /// @interface NSView (MyViewMethods)
1084 /// Categories also allow you to split the implementation of a class across
1085 /// several files (a feature more naturally supported in C++).
1087 /// Categories were originally inspired by dynamic languages such as Common
1088 /// Lisp and Smalltalk. More traditional class-based languages (C++, Java)
1089 /// don't support this level of dynamism, which is both powerful and dangerous.
1091 class ObjCCategoryDecl : public ObjCContainerDecl {
1092 /// Interface belonging to this category
1093 ObjCInterfaceDecl *ClassInterface;
1095 /// referenced protocols in this category.
1096 ObjCProtocolList ReferencedProtocols;
1098 /// Next category belonging to this class.
1099 /// FIXME: this should not be a singly-linked list. Move storage elsewhere.
1100 ObjCCategoryDecl *NextClassCategory;
1102 /// true of class extension has at least one bitfield ivar.
1103 bool HasSynthBitfield : 1;
1105 /// \brief The location of the category name in this declaration.
1106 SourceLocation CategoryNameLoc;
1108 ObjCCategoryDecl(DeclContext *DC, SourceLocation AtLoc,
1109 SourceLocation ClassNameLoc, SourceLocation CategoryNameLoc,
1110 IdentifierInfo *Id, ObjCInterfaceDecl *IDecl)
1111 : ObjCContainerDecl(ObjCCategory, DC, Id, ClassNameLoc, AtLoc),
1112 ClassInterface(IDecl), NextClassCategory(0), HasSynthBitfield(false),
1113 CategoryNameLoc(CategoryNameLoc) {
1117 static ObjCCategoryDecl *Create(ASTContext &C, DeclContext *DC,
1118 SourceLocation AtLoc,
1119 SourceLocation ClassNameLoc,
1120 SourceLocation CategoryNameLoc,
1122 ObjCInterfaceDecl *IDecl);
1123 static ObjCCategoryDecl *Create(ASTContext &C, EmptyShell Empty);
1125 ObjCInterfaceDecl *getClassInterface() { return ClassInterface; }
1126 const ObjCInterfaceDecl *getClassInterface() const { return ClassInterface; }
1128 ObjCCategoryImplDecl *getImplementation() const;
1129 void setImplementation(ObjCCategoryImplDecl *ImplD);
1131 /// setProtocolList - Set the list of protocols that this interface
1133 void setProtocolList(ObjCProtocolDecl *const*List, unsigned Num,
1134 const SourceLocation *Locs, ASTContext &C) {
1135 ReferencedProtocols.set(List, Num, Locs, C);
1138 const ObjCProtocolList &getReferencedProtocols() const {
1139 return ReferencedProtocols;
1142 typedef ObjCProtocolList::iterator protocol_iterator;
1143 protocol_iterator protocol_begin() const {return ReferencedProtocols.begin();}
1144 protocol_iterator protocol_end() const { return ReferencedProtocols.end(); }
1145 unsigned protocol_size() const { return ReferencedProtocols.size(); }
1146 typedef ObjCProtocolList::loc_iterator protocol_loc_iterator;
1147 protocol_loc_iterator protocol_loc_begin() const {
1148 return ReferencedProtocols.loc_begin();
1150 protocol_loc_iterator protocol_loc_end() const {
1151 return ReferencedProtocols.loc_end();
1154 ObjCCategoryDecl *getNextClassCategory() const { return NextClassCategory; }
1156 bool IsClassExtension() const { return getIdentifier() == 0; }
1157 const ObjCCategoryDecl *getNextClassExtension() const;
1159 bool hasSynthBitfield() const { return HasSynthBitfield; }
1160 void setHasSynthBitfield (bool val) { HasSynthBitfield = val; }
1162 typedef specific_decl_iterator<ObjCIvarDecl> ivar_iterator;
1163 ivar_iterator ivar_begin() const {
1164 return ivar_iterator(decls_begin());
1166 ivar_iterator ivar_end() const {
1167 return ivar_iterator(decls_end());
1169 unsigned ivar_size() const {
1170 return std::distance(ivar_begin(), ivar_end());
1172 bool ivar_empty() const {
1173 return ivar_begin() == ivar_end();
1176 SourceLocation getCategoryNameLoc() const { return CategoryNameLoc; }
1177 void setCategoryNameLoc(SourceLocation Loc) { CategoryNameLoc = Loc; }
1179 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1180 static bool classof(const ObjCCategoryDecl *D) { return true; }
1181 static bool classofKind(Kind K) { return K == ObjCCategory; }
1183 friend class ASTDeclReader;
1184 friend class ASTDeclWriter;
1187 class ObjCImplDecl : public ObjCContainerDecl {
1188 /// Class interface for this class/category implementation
1189 ObjCInterfaceDecl *ClassInterface;
1192 ObjCImplDecl(Kind DK, DeclContext *DC,
1193 ObjCInterfaceDecl *classInterface,
1194 SourceLocation nameLoc, SourceLocation atStartLoc)
1195 : ObjCContainerDecl(DK, DC,
1196 classInterface? classInterface->getIdentifier() : 0,
1197 nameLoc, atStartLoc),
1198 ClassInterface(classInterface) {}
1201 const ObjCInterfaceDecl *getClassInterface() const { return ClassInterface; }
1202 ObjCInterfaceDecl *getClassInterface() { return ClassInterface; }
1203 void setClassInterface(ObjCInterfaceDecl *IFace);
1205 void addInstanceMethod(ObjCMethodDecl *method) {
1206 // FIXME: Context should be set correctly before we get here.
1207 method->setLexicalDeclContext(this);
1210 void addClassMethod(ObjCMethodDecl *method) {
1211 // FIXME: Context should be set correctly before we get here.
1212 method->setLexicalDeclContext(this);
1216 void addPropertyImplementation(ObjCPropertyImplDecl *property);
1218 ObjCPropertyImplDecl *FindPropertyImplDecl(IdentifierInfo *propertyId) const;
1219 ObjCPropertyImplDecl *FindPropertyImplIvarDecl(IdentifierInfo *ivarId) const;
1221 // Iterator access to properties.
1222 typedef specific_decl_iterator<ObjCPropertyImplDecl> propimpl_iterator;
1223 propimpl_iterator propimpl_begin() const {
1224 return propimpl_iterator(decls_begin());
1226 propimpl_iterator propimpl_end() const {
1227 return propimpl_iterator(decls_end());
1230 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1231 static bool classof(const ObjCImplDecl *D) { return true; }
1232 static bool classofKind(Kind K) {
1233 return K >= firstObjCImpl && K <= lastObjCImpl;
1237 /// ObjCCategoryImplDecl - An object of this class encapsulates a category
1238 /// @implementation declaration. If a category class has declaration of a
1239 /// property, its implementation must be specified in the category's
1240 /// @implementation declaration. Example:
1241 /// @interface I @end
1242 /// @interface I(CATEGORY)
1243 /// @property int p1, d1;
1245 /// @implementation I(CATEGORY)
1249 /// ObjCCategoryImplDecl
1250 class ObjCCategoryImplDecl : public ObjCImplDecl {
1254 ObjCCategoryImplDecl(DeclContext *DC, IdentifierInfo *Id,
1255 ObjCInterfaceDecl *classInterface,
1256 SourceLocation nameLoc, SourceLocation atStartLoc)
1257 : ObjCImplDecl(ObjCCategoryImpl, DC, classInterface, nameLoc, atStartLoc),
1260 static ObjCCategoryImplDecl *Create(ASTContext &C, DeclContext *DC,
1262 ObjCInterfaceDecl *classInterface,
1263 SourceLocation nameLoc,
1264 SourceLocation atStartLoc);
1266 /// getIdentifier - Get the identifier that names the category
1267 /// interface associated with this implementation.
1268 /// FIXME: This is a bad API, we are overriding the NamedDecl::getIdentifier()
1269 /// to mean something different. For example:
1270 /// ((NamedDecl *)SomeCategoryImplDecl)->getIdentifier()
1271 /// returns the class interface name, whereas
1272 /// ((ObjCCategoryImplDecl *)SomeCategoryImplDecl)->getIdentifier()
1273 /// returns the category name.
1274 IdentifierInfo *getIdentifier() const {
1277 void setIdentifier(IdentifierInfo *II) { Id = II; }
1279 ObjCCategoryDecl *getCategoryDecl() const;
1281 /// getName - Get the name of identifier for the class interface associated
1282 /// with this implementation as a StringRef.
1284 // FIXME: This is a bad API, we are overriding the NamedDecl::getName, to mean
1285 // something different.
1286 StringRef getName() const {
1287 return Id ? Id->getNameStart() : "";
1290 /// getNameAsCString - Get the name of identifier for the class
1291 /// interface associated with this implementation as a C string
1294 // FIXME: Deprecated, move clients to getName().
1295 const char *getNameAsCString() const {
1296 return Id ? Id->getNameStart() : "";
1299 /// @brief Get the name of the class associated with this interface.
1301 // FIXME: Deprecated, move clients to getName().
1302 std::string getNameAsString() const {
1306 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1307 static bool classof(const ObjCCategoryImplDecl *D) { return true; }
1308 static bool classofKind(Kind K) { return K == ObjCCategoryImpl;}
1311 raw_ostream &operator<<(raw_ostream &OS,
1312 const ObjCCategoryImplDecl *CID);
1314 /// ObjCImplementationDecl - Represents a class definition - this is where
1315 /// method definitions are specified. For example:
1318 /// @implementation MyClass
1319 /// - (void)myMethod { /* do something */ }
1323 /// Typically, instance variables are specified in the class interface,
1324 /// *not* in the implementation. Nevertheless (for legacy reasons), we
1325 /// allow instance variables to be specified in the implementation. When
1326 /// specified, they need to be *identical* to the interface.
1328 class ObjCImplementationDecl : public ObjCImplDecl {
1329 /// Implementation Class's super class.
1330 ObjCInterfaceDecl *SuperClass;
1331 /// Support for ivar initialization.
1332 /// IvarInitializers - The arguments used to initialize the ivars
1333 CXXCtorInitializer **IvarInitializers;
1334 unsigned NumIvarInitializers;
1336 /// true if class has a .cxx_[construct,destruct] method.
1337 bool HasCXXStructors : 1;
1339 /// true of class extension has at least one bitfield ivar.
1340 bool HasSynthBitfield : 1;
1342 ObjCImplementationDecl(DeclContext *DC,
1343 ObjCInterfaceDecl *classInterface,
1344 ObjCInterfaceDecl *superDecl,
1345 SourceLocation nameLoc, SourceLocation atStartLoc)
1346 : ObjCImplDecl(ObjCImplementation, DC, classInterface, nameLoc, atStartLoc),
1347 SuperClass(superDecl), IvarInitializers(0), NumIvarInitializers(0),
1348 HasCXXStructors(false), HasSynthBitfield(false) {}
1350 static ObjCImplementationDecl *Create(ASTContext &C, DeclContext *DC,
1351 ObjCInterfaceDecl *classInterface,
1352 ObjCInterfaceDecl *superDecl,
1353 SourceLocation nameLoc,
1354 SourceLocation atStartLoc);
1356 /// init_iterator - Iterates through the ivar initializer list.
1357 typedef CXXCtorInitializer **init_iterator;
1359 /// init_const_iterator - Iterates through the ivar initializer list.
1360 typedef CXXCtorInitializer * const * init_const_iterator;
1362 /// init_begin() - Retrieve an iterator to the first initializer.
1363 init_iterator init_begin() { return IvarInitializers; }
1364 /// begin() - Retrieve an iterator to the first initializer.
1365 init_const_iterator init_begin() const { return IvarInitializers; }
1367 /// init_end() - Retrieve an iterator past the last initializer.
1368 init_iterator init_end() {
1369 return IvarInitializers + NumIvarInitializers;
1371 /// end() - Retrieve an iterator past the last initializer.
1372 init_const_iterator init_end() const {
1373 return IvarInitializers + NumIvarInitializers;
1375 /// getNumArgs - Number of ivars which must be initialized.
1376 unsigned getNumIvarInitializers() const {
1377 return NumIvarInitializers;
1380 void setNumIvarInitializers(unsigned numNumIvarInitializers) {
1381 NumIvarInitializers = numNumIvarInitializers;
1384 void setIvarInitializers(ASTContext &C,
1385 CXXCtorInitializer ** initializers,
1386 unsigned numInitializers);
1388 bool hasCXXStructors() const { return HasCXXStructors; }
1389 void setHasCXXStructors(bool val) { HasCXXStructors = val; }
1391 bool hasSynthBitfield() const { return HasSynthBitfield; }
1392 void setHasSynthBitfield (bool val) { HasSynthBitfield = val; }
1394 /// getIdentifier - Get the identifier that names the class
1395 /// interface associated with this implementation.
1396 IdentifierInfo *getIdentifier() const {
1397 return getClassInterface()->getIdentifier();
1400 /// getName - Get the name of identifier for the class interface associated
1401 /// with this implementation as a StringRef.
1403 // FIXME: This is a bad API, we are overriding the NamedDecl::getName, to mean
1404 // something different.
1405 StringRef getName() const {
1406 assert(getIdentifier() && "Name is not a simple identifier");
1407 return getIdentifier()->getName();
1410 /// getNameAsCString - Get the name of identifier for the class
1411 /// interface associated with this implementation as a C string
1414 // FIXME: Move to StringRef API.
1415 const char *getNameAsCString() const {
1416 return getName().data();
1419 /// @brief Get the name of the class associated with this interface.
1421 // FIXME: Move to StringRef API.
1422 std::string getNameAsString() const {
1426 const ObjCInterfaceDecl *getSuperClass() const { return SuperClass; }
1427 ObjCInterfaceDecl *getSuperClass() { return SuperClass; }
1429 void setSuperClass(ObjCInterfaceDecl * superCls) { SuperClass = superCls; }
1431 typedef specific_decl_iterator<ObjCIvarDecl> ivar_iterator;
1432 ivar_iterator ivar_begin() const {
1433 return ivar_iterator(decls_begin());
1435 ivar_iterator ivar_end() const {
1436 return ivar_iterator(decls_end());
1438 unsigned ivar_size() const {
1439 return std::distance(ivar_begin(), ivar_end());
1441 bool ivar_empty() const {
1442 return ivar_begin() == ivar_end();
1445 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1446 static bool classof(const ObjCImplementationDecl *D) { return true; }
1447 static bool classofKind(Kind K) { return K == ObjCImplementation; }
1449 friend class ASTDeclReader;
1450 friend class ASTDeclWriter;
1453 raw_ostream &operator<<(raw_ostream &OS,
1454 const ObjCImplementationDecl *ID);
1456 /// ObjCCompatibleAliasDecl - Represents alias of a class. This alias is
1457 /// declared as @compatibility_alias alias class.
1458 class ObjCCompatibleAliasDecl : public NamedDecl {
1459 /// Class that this is an alias of.
1460 ObjCInterfaceDecl *AliasedClass;
1462 ObjCCompatibleAliasDecl(DeclContext *DC, SourceLocation L, IdentifierInfo *Id,
1463 ObjCInterfaceDecl* aliasedClass)
1464 : NamedDecl(ObjCCompatibleAlias, DC, L, Id), AliasedClass(aliasedClass) {}
1466 static ObjCCompatibleAliasDecl *Create(ASTContext &C, DeclContext *DC,
1467 SourceLocation L, IdentifierInfo *Id,
1468 ObjCInterfaceDecl* aliasedClass);
1470 const ObjCInterfaceDecl *getClassInterface() const { return AliasedClass; }
1471 ObjCInterfaceDecl *getClassInterface() { return AliasedClass; }
1472 void setClassInterface(ObjCInterfaceDecl *D) { AliasedClass = D; }
1474 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1475 static bool classof(const ObjCCompatibleAliasDecl *D) { return true; }
1476 static bool classofKind(Kind K) { return K == ObjCCompatibleAlias; }
1480 /// ObjCPropertyDecl - Represents one property declaration in an interface.
1482 /// @property (assign, readwrite) int MyProperty;
1484 class ObjCPropertyDecl : public NamedDecl {
1486 enum PropertyAttributeKind {
1487 OBJC_PR_noattr = 0x00,
1488 OBJC_PR_readonly = 0x01,
1489 OBJC_PR_getter = 0x02,
1490 OBJC_PR_assign = 0x04,
1491 OBJC_PR_readwrite = 0x08,
1492 OBJC_PR_retain = 0x10,
1493 OBJC_PR_copy = 0x20,
1494 OBJC_PR_nonatomic = 0x40,
1495 OBJC_PR_setter = 0x80,
1496 OBJC_PR_atomic = 0x100,
1497 OBJC_PR_weak = 0x200,
1498 OBJC_PR_strong = 0x400,
1499 OBJC_PR_unsafe_unretained = 0x800
1500 // Adding a property should change NumPropertyAttrsBits
1504 /// \brief Number of bits fitting all the property attributes.
1505 NumPropertyAttrsBits = 12
1508 enum SetterKind { Assign, Retain, Copy, Weak };
1509 enum PropertyControl { None, Required, Optional };
1511 SourceLocation AtLoc; // location of @property
1512 TypeSourceInfo *DeclType;
1513 unsigned PropertyAttributes : NumPropertyAttrsBits;
1514 unsigned PropertyAttributesAsWritten : NumPropertyAttrsBits;
1515 // @required/@optional
1516 unsigned PropertyImplementation : 2;
1518 Selector GetterName; // getter name of NULL if no getter
1519 Selector SetterName; // setter name of NULL if no setter
1521 ObjCMethodDecl *GetterMethodDecl; // Declaration of getter instance method
1522 ObjCMethodDecl *SetterMethodDecl; // Declaration of setter instance method
1523 ObjCIvarDecl *PropertyIvarDecl; // Synthesize ivar for this property
1525 ObjCPropertyDecl(DeclContext *DC, SourceLocation L, IdentifierInfo *Id,
1526 SourceLocation AtLocation, TypeSourceInfo *T)
1527 : NamedDecl(ObjCProperty, DC, L, Id), AtLoc(AtLocation), DeclType(T),
1528 PropertyAttributes(OBJC_PR_noattr),
1529 PropertyAttributesAsWritten(OBJC_PR_noattr),
1530 PropertyImplementation(None),
1531 GetterName(Selector()),
1532 SetterName(Selector()),
1533 GetterMethodDecl(0), SetterMethodDecl(0) , PropertyIvarDecl(0) {}
1535 static ObjCPropertyDecl *Create(ASTContext &C, DeclContext *DC,
1537 IdentifierInfo *Id, SourceLocation AtLocation,
1539 PropertyControl propControl = None);
1540 SourceLocation getAtLoc() const { return AtLoc; }
1541 void setAtLoc(SourceLocation L) { AtLoc = L; }
1543 TypeSourceInfo *getTypeSourceInfo() const { return DeclType; }
1544 QualType getType() const { return DeclType->getType(); }
1545 void setType(TypeSourceInfo *T) { DeclType = T; }
1547 PropertyAttributeKind getPropertyAttributes() const {
1548 return PropertyAttributeKind(PropertyAttributes);
1550 void setPropertyAttributes(PropertyAttributeKind PRVal) {
1551 PropertyAttributes |= PRVal;
1554 PropertyAttributeKind getPropertyAttributesAsWritten() const {
1555 return PropertyAttributeKind(PropertyAttributesAsWritten);
1558 bool hasWrittenStorageAttribute() const {
1559 return PropertyAttributesAsWritten & (OBJC_PR_assign | OBJC_PR_copy |
1560 OBJC_PR_unsafe_unretained | OBJC_PR_retain | OBJC_PR_strong |
1564 void setPropertyAttributesAsWritten(PropertyAttributeKind PRVal) {
1565 PropertyAttributesAsWritten = PRVal;
1568 void makeitReadWriteAttribute(void) {
1569 PropertyAttributes &= ~OBJC_PR_readonly;
1570 PropertyAttributes |= OBJC_PR_readwrite;
1573 // Helper methods for accessing attributes.
1575 /// isReadOnly - Return true iff the property has a setter.
1576 bool isReadOnly() const {
1577 return (PropertyAttributes & OBJC_PR_readonly);
1580 /// isAtomic - Return true if the property is atomic.
1581 bool isAtomic() const {
1582 return (PropertyAttributes & OBJC_PR_atomic);
1585 /// isRetaining - Return true if the property retains its value.
1586 bool isRetaining() const {
1587 return (PropertyAttributes &
1588 (OBJC_PR_retain | OBJC_PR_strong | OBJC_PR_copy));
1591 /// getSetterKind - Return the method used for doing assignment in
1592 /// the property setter. This is only valid if the property has been
1593 /// defined to have a setter.
1594 SetterKind getSetterKind() const {
1595 if (PropertyAttributes & OBJC_PR_strong)
1596 return getType()->isBlockPointerType() ? Copy : Retain;
1597 if (PropertyAttributes & OBJC_PR_retain)
1599 if (PropertyAttributes & OBJC_PR_copy)
1601 if (PropertyAttributes & OBJC_PR_weak)
1606 Selector getGetterName() const { return GetterName; }
1607 void setGetterName(Selector Sel) { GetterName = Sel; }
1609 Selector getSetterName() const { return SetterName; }
1610 void setSetterName(Selector Sel) { SetterName = Sel; }
1612 ObjCMethodDecl *getGetterMethodDecl() const { return GetterMethodDecl; }
1613 void setGetterMethodDecl(ObjCMethodDecl *gDecl) { GetterMethodDecl = gDecl; }
1615 ObjCMethodDecl *getSetterMethodDecl() const { return SetterMethodDecl; }
1616 void setSetterMethodDecl(ObjCMethodDecl *gDecl) { SetterMethodDecl = gDecl; }
1618 // Related to @optional/@required declared in @protocol
1619 void setPropertyImplementation(PropertyControl pc) {
1620 PropertyImplementation = pc;
1622 PropertyControl getPropertyImplementation() const {
1623 return PropertyControl(PropertyImplementation);
1626 void setPropertyIvarDecl(ObjCIvarDecl *Ivar) {
1627 PropertyIvarDecl = Ivar;
1629 ObjCIvarDecl *getPropertyIvarDecl() const {
1630 return PropertyIvarDecl;
1633 virtual SourceRange getSourceRange() const {
1634 return SourceRange(AtLoc, getLocation());
1637 /// Lookup a property by name in the specified DeclContext.
1638 static ObjCPropertyDecl *findPropertyDecl(const DeclContext *DC,
1639 IdentifierInfo *propertyID);
1641 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1642 static bool classof(const ObjCPropertyDecl *D) { return true; }
1643 static bool classofKind(Kind K) { return K == ObjCProperty; }
1646 /// ObjCPropertyImplDecl - Represents implementation declaration of a property
1647 /// in a class or category implementation block. For example:
1648 /// @synthesize prop1 = ivar1;
1650 class ObjCPropertyImplDecl : public Decl {
1657 SourceLocation AtLoc; // location of @synthesize or @dynamic
1659 /// \brief For @synthesize, the location of the ivar, if it was written in
1660 /// the source code.
1663 /// @synthesize int a = b
1665 SourceLocation IvarLoc;
1667 /// Property declaration being implemented
1668 ObjCPropertyDecl *PropertyDecl;
1670 /// Null for @dynamic. Required for @synthesize.
1671 ObjCIvarDecl *PropertyIvarDecl;
1673 /// Null for @dynamic. Non-null if property must be copy-constructed in getter
1674 Expr *GetterCXXConstructor;
1676 /// Null for @dynamic. Non-null if property has assignment operator to call
1677 /// in Setter synthesis.
1678 Expr *SetterCXXAssignment;
1680 ObjCPropertyImplDecl(DeclContext *DC, SourceLocation atLoc, SourceLocation L,
1681 ObjCPropertyDecl *property,
1683 ObjCIvarDecl *ivarDecl,
1684 SourceLocation ivarLoc)
1685 : Decl(ObjCPropertyImpl, DC, L), AtLoc(atLoc),
1686 IvarLoc(ivarLoc), PropertyDecl(property), PropertyIvarDecl(ivarDecl),
1687 GetterCXXConstructor(0), SetterCXXAssignment(0) {
1688 assert (PK == Dynamic || PropertyIvarDecl);
1692 static ObjCPropertyImplDecl *Create(ASTContext &C, DeclContext *DC,
1693 SourceLocation atLoc, SourceLocation L,
1694 ObjCPropertyDecl *property,
1696 ObjCIvarDecl *ivarDecl,
1697 SourceLocation ivarLoc);
1699 virtual SourceRange getSourceRange() const;
1701 SourceLocation getLocStart() const { return AtLoc; }
1702 void setAtLoc(SourceLocation Loc) { AtLoc = Loc; }
1704 ObjCPropertyDecl *getPropertyDecl() const {
1705 return PropertyDecl;
1707 void setPropertyDecl(ObjCPropertyDecl *Prop) { PropertyDecl = Prop; }
1709 Kind getPropertyImplementation() const {
1710 return PropertyIvarDecl ? Synthesize : Dynamic;
1713 ObjCIvarDecl *getPropertyIvarDecl() const {
1714 return PropertyIvarDecl;
1716 SourceLocation getPropertyIvarDeclLoc() const { return IvarLoc; }
1718 void setPropertyIvarDecl(ObjCIvarDecl *Ivar,
1719 SourceLocation IvarLoc) {
1720 PropertyIvarDecl = Ivar;
1721 this->IvarLoc = IvarLoc;
1724 Expr *getGetterCXXConstructor() const {
1725 return GetterCXXConstructor;
1727 void setGetterCXXConstructor(Expr *getterCXXConstructor) {
1728 GetterCXXConstructor = getterCXXConstructor;
1731 Expr *getSetterCXXAssignment() const {
1732 return SetterCXXAssignment;
1734 void setSetterCXXAssignment(Expr *setterCXXAssignment) {
1735 SetterCXXAssignment = setterCXXAssignment;
1738 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1739 static bool classof(const ObjCPropertyImplDecl *D) { return true; }
1740 static bool classofKind(Decl::Kind K) { return K == ObjCPropertyImpl; }
1742 friend class ASTDeclReader;
1745 } // end namespace clang