1 //===--- Decl.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 Decl subclasses.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CLANG_AST_DECL_H
15 #define LLVM_CLANG_AST_DECL_H
17 #include "clang/AST/APValue.h"
18 #include "clang/AST/DeclBase.h"
19 #include "clang/AST/Redeclarable.h"
20 #include "clang/AST/DeclarationName.h"
21 #include "clang/AST/ExternalASTSource.h"
22 #include "clang/Basic/Linkage.h"
27 class FunctionTemplateDecl;
31 class NestedNameSpecifier;
32 class TemplateParameterList;
33 class TemplateArgumentList;
34 class MemberSpecializationInfo;
35 class FunctionTemplateSpecializationInfo;
36 class DependentFunctionTemplateSpecializationInfo;
38 class UnresolvedSetImpl;
40 /// \brief A container of type source information.
42 /// A client can read the relevant info using TypeLoc wrappers, e.g:
44 /// TypeLoc TL = TypeSourceInfo->getTypeLoc();
45 /// if (PointerLoc *PL = dyn_cast<PointerLoc>(&TL))
46 /// PL->getStarLoc().print(OS, SrcMgr);
49 class TypeSourceInfo {
51 // Contains a memory block after the class, used for type source information,
52 // allocated by ASTContext.
53 friend class ASTContext;
54 TypeSourceInfo(QualType ty) : Ty(ty) { }
56 /// \brief Return the type wrapped by this type source info.
57 QualType getType() const { return Ty; }
59 /// \brief Return the TypeLoc wrapper for the type source info.
60 TypeLoc getTypeLoc() const; // implemented in TypeLoc.h
63 /// TranslationUnitDecl - The top declaration context.
64 class TranslationUnitDecl : public Decl, public DeclContext {
67 /// The (most recently entered) anonymous namespace for this
68 /// translation unit, if one has been created.
69 NamespaceDecl *AnonymousNamespace;
71 explicit TranslationUnitDecl(ASTContext &ctx)
72 : Decl(TranslationUnit, 0, SourceLocation()),
73 DeclContext(TranslationUnit),
74 Ctx(ctx), AnonymousNamespace(0) {}
76 ASTContext &getASTContext() const { return Ctx; }
78 NamespaceDecl *getAnonymousNamespace() const { return AnonymousNamespace; }
79 void setAnonymousNamespace(NamespaceDecl *D) { AnonymousNamespace = D; }
81 static TranslationUnitDecl *Create(ASTContext &C);
82 // Implement isa/cast/dyncast/etc.
83 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
84 static bool classof(const TranslationUnitDecl *D) { return true; }
85 static bool classofKind(Kind K) { return K == TranslationUnit; }
86 static DeclContext *castToDeclContext(const TranslationUnitDecl *D) {
87 return static_cast<DeclContext *>(const_cast<TranslationUnitDecl*>(D));
89 static TranslationUnitDecl *castFromDeclContext(const DeclContext *DC) {
90 return static_cast<TranslationUnitDecl *>(const_cast<DeclContext*>(DC));
94 /// NamedDecl - This represents a decl with a name. Many decls have names such
95 /// as ObjCMethodDecl, but not @class, etc.
96 class NamedDecl : public Decl {
97 /// Name - The name of this declaration, which is typically a normal
98 /// identifier but may also be a special kind of name (C++
99 /// constructor, Objective-C selector, etc.)
100 DeclarationName Name;
103 NamedDecl(Kind DK, DeclContext *DC, SourceLocation L, DeclarationName N)
104 : Decl(DK, DC, L), Name(N) { }
107 /// getIdentifier - Get the identifier that names this declaration,
108 /// if there is one. This will return NULL if this declaration has
109 /// no name (e.g., for an unnamed class) or if the name is a special
110 /// name (C++ constructor, Objective-C selector, etc.).
111 IdentifierInfo *getIdentifier() const { return Name.getAsIdentifierInfo(); }
113 /// getName - Get the name of identifier for this declaration as a StringRef.
114 /// This requires that the declaration have a name and that it be a simple
116 llvm::StringRef getName() const {
117 assert(Name.isIdentifier() && "Name is not a simple identifier");
118 return getIdentifier() ? getIdentifier()->getName() : "";
121 /// getNameAsCString - Get the name of identifier for this declaration as a
122 /// C string (const char*). This requires that the declaration have a name
123 /// and that it be a simple identifier.
125 // FIXME: Deprecated, move clients to getName().
126 const char *getNameAsCString() const {
127 assert(Name.isIdentifier() && "Name is not a simple identifier");
128 return getIdentifier() ? getIdentifier()->getNameStart() : "";
131 /// getNameAsString - Get a human-readable name for the declaration, even if
132 /// it is one of the special kinds of names (C++ constructor, Objective-C
133 /// selector, etc). Creating this name requires expensive string
134 /// manipulation, so it should be called only when performance doesn't matter.
135 /// For simple declarations, getNameAsCString() should suffice.
137 // FIXME: This function should be renamed to indicate that it is not just an
138 // alternate form of getName(), and clients should move as appropriate.
140 // FIXME: Deprecated, move clients to getName().
141 std::string getNameAsString() const { return Name.getAsString(); }
143 void printName(llvm::raw_ostream &os) const { return Name.printName(os); }
145 /// getDeclName - Get the actual, stored name of the declaration,
146 /// which may be a special name.
147 DeclarationName getDeclName() const { return Name; }
149 /// \brief Set the name of this declaration.
150 void setDeclName(DeclarationName N) { Name = N; }
152 /// getQualifiedNameAsString - Returns human-readable qualified name for
153 /// declaration, like A::B::i, for i being member of namespace A::B.
154 /// If declaration is not member of context which can be named (record,
155 /// namespace), it will return same result as getNameAsString().
156 /// Creating this name is expensive, so it should be called only when
157 /// performance doesn't matter.
158 std::string getQualifiedNameAsString() const;
159 std::string getQualifiedNameAsString(const PrintingPolicy &Policy) const;
161 /// getNameForDiagnostic - Appends a human-readable name for this
162 /// declaration into the given string.
164 /// This is the method invoked by Sema when displaying a NamedDecl
165 /// in a diagnostic. It does not necessarily produce the same
166 /// result as getNameAsString(); for example, class template
167 /// specializations are printed with their template arguments.
169 /// TODO: use an API that doesn't require so many temporary strings
170 virtual void getNameForDiagnostic(std::string &S,
171 const PrintingPolicy &Policy,
172 bool Qualified) const {
174 S += getQualifiedNameAsString(Policy);
176 S += getNameAsString();
179 /// declarationReplaces - Determine whether this declaration, if
180 /// known to be well-formed within its context, will replace the
181 /// declaration OldD if introduced into scope. A declaration will
182 /// replace another declaration if, for example, it is a
183 /// redeclaration of the same variable or function, but not if it is
184 /// a declaration of a different kind (function vs. class) or an
185 /// overloaded function.
186 bool declarationReplaces(NamedDecl *OldD) const;
188 /// \brief Determine whether this declaration has linkage.
189 bool hasLinkage() const;
191 /// \brief Determine whether this declaration is a C++ class member.
192 bool isCXXClassMember() const {
193 const DeclContext *DC = getDeclContext();
195 // C++0x [class.mem]p1:
196 // The enumerators of an unscoped enumeration defined in
197 // the class are members of the class.
198 // FIXME: support C++0x scoped enumerations.
199 if (isa<EnumDecl>(DC))
200 DC = DC->getParent();
202 return DC->isRecord();
205 /// \brief Given that this declaration is a C++ class member,
206 /// determine whether it's an instance member of its class.
207 bool isCXXInstanceMember() const;
209 /// \brief Determine what kind of linkage this entity has.
210 Linkage getLinkage() const;
212 /// \brief Looks through UsingDecls and ObjCCompatibleAliasDecls for
213 /// the underlying named decl.
214 NamedDecl *getUnderlyingDecl();
215 const NamedDecl *getUnderlyingDecl() const {
216 return const_cast<NamedDecl*>(this)->getUnderlyingDecl();
219 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
220 static bool classof(const NamedDecl *D) { return true; }
221 static bool classofKind(Kind K) { return K >= firstNamed && K <= lastNamed; }
224 inline llvm::raw_ostream &operator<<(llvm::raw_ostream &OS,
225 const NamedDecl *ND) {
226 ND->getDeclName().printName(OS);
230 /// NamespaceDecl - Represent a C++ namespace.
231 class NamespaceDecl : public NamedDecl, public DeclContext {
232 SourceLocation LBracLoc, RBracLoc;
234 // For extended namespace definitions:
236 // namespace A { int x; }
237 // namespace A { int y; }
239 // there will be one NamespaceDecl for each declaration.
240 // NextNamespace points to the next extended declaration.
241 // OrigNamespace points to the original namespace declaration.
242 // OrigNamespace of the first namespace decl points to itself.
243 NamespaceDecl *NextNamespace;
245 /// \brief A pointer to either the original namespace definition for
246 /// this namespace (if the boolean value is false) or the anonymous
247 /// namespace that lives just inside this namespace (if the boolean
250 /// We can combine these two notions because the anonymous namespace
251 /// must only be stored in one of the namespace declarations (so all
252 /// of the namespace declarations can find it). We therefore choose
253 /// the original namespace declaration, since all of the namespace
254 /// declarations have a link directly to it; the original namespace
255 /// declaration itself only needs to know that it is the original
256 /// namespace declaration (which the boolean indicates).
257 llvm::PointerIntPair<NamespaceDecl *, 1, bool> OrigOrAnonNamespace;
259 NamespaceDecl(DeclContext *DC, SourceLocation L, IdentifierInfo *Id)
260 : NamedDecl(Namespace, DC, L, Id), DeclContext(Namespace),
261 NextNamespace(0), OrigOrAnonNamespace(0, true) { }
264 static NamespaceDecl *Create(ASTContext &C, DeclContext *DC,
265 SourceLocation L, IdentifierInfo *Id);
267 virtual void Destroy(ASTContext& C);
269 // \brief Returns true if this is an anonymous namespace declaration.
277 // q.v. C++ [namespace.unnamed]
278 bool isAnonymousNamespace() const {
279 return !getIdentifier();
282 /// \brief Return the next extended namespace declaration or null if this
284 NamespaceDecl *getNextNamespace() { return NextNamespace; }
285 const NamespaceDecl *getNextNamespace() const { return NextNamespace; }
287 /// \brief Set the next extended namespace declaration.
288 void setNextNamespace(NamespaceDecl *ND) { NextNamespace = ND; }
290 /// \brief Get the original (first) namespace declaration.
291 NamespaceDecl *getOriginalNamespace() const {
292 if (OrigOrAnonNamespace.getInt())
293 return const_cast<NamespaceDecl *>(this);
295 return OrigOrAnonNamespace.getPointer();
298 /// \brief Return true if this declaration is an original (first) declaration
299 /// of the namespace. This is false for non-original (subsequent) namespace
300 /// declarations and anonymous namespaces.
301 bool isOriginalNamespace() const {
302 return getOriginalNamespace() == this;
305 /// \brief Set the original (first) namespace declaration.
306 void setOriginalNamespace(NamespaceDecl *ND) {
308 OrigOrAnonNamespace.setPointer(ND);
309 OrigOrAnonNamespace.setInt(false);
313 NamespaceDecl *getAnonymousNamespace() const {
314 return getOriginalNamespace()->OrigOrAnonNamespace.getPointer();
317 void setAnonymousNamespace(NamespaceDecl *D) {
318 assert(!D || D->isAnonymousNamespace());
319 assert(!D || D->getParent() == this);
320 getOriginalNamespace()->OrigOrAnonNamespace.setPointer(D);
323 virtual NamespaceDecl *getCanonicalDecl() { return getOriginalNamespace(); }
324 const NamespaceDecl *getCanonicalDecl() const {
325 return getOriginalNamespace();
328 virtual SourceRange getSourceRange() const {
329 return SourceRange(getLocation(), RBracLoc);
332 SourceLocation getLBracLoc() const { return LBracLoc; }
333 SourceLocation getRBracLoc() const { return RBracLoc; }
334 void setLBracLoc(SourceLocation LBrace) { LBracLoc = LBrace; }
335 void setRBracLoc(SourceLocation RBrace) { RBracLoc = RBrace; }
337 // Implement isa/cast/dyncast/etc.
338 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
339 static bool classof(const NamespaceDecl *D) { return true; }
340 static bool classofKind(Kind K) { return K == Namespace; }
341 static DeclContext *castToDeclContext(const NamespaceDecl *D) {
342 return static_cast<DeclContext *>(const_cast<NamespaceDecl*>(D));
344 static NamespaceDecl *castFromDeclContext(const DeclContext *DC) {
345 return static_cast<NamespaceDecl *>(const_cast<DeclContext*>(DC));
348 friend class PCHDeclReader;
349 friend class PCHDeclWriter;
352 /// ValueDecl - Represent the declaration of a variable (in which case it is
353 /// an lvalue) a function (in which case it is a function designator) or
354 /// an enum constant.
355 class ValueDecl : public NamedDecl {
359 ValueDecl(Kind DK, DeclContext *DC, SourceLocation L,
360 DeclarationName N, QualType T)
361 : NamedDecl(DK, DC, L, N), DeclType(T) {}
363 QualType getType() const { return DeclType; }
364 void setType(QualType newType) { DeclType = newType; }
366 // Implement isa/cast/dyncast/etc.
367 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
368 static bool classof(const ValueDecl *D) { return true; }
369 static bool classofKind(Kind K) { return K >= firstValue && K <= lastValue; }
372 /// QualifierInfo - A struct with extended info about a syntactic
373 /// name qualifier, to be used for the case of out-of-line declarations.
374 struct QualifierInfo {
375 /// NNS - The syntactic name qualifier.
376 NestedNameSpecifier *NNS;
377 /// NNSRange - The source range for the qualifier.
378 SourceRange NNSRange;
379 /// NumTemplParamLists - The number of template parameter lists
380 /// that were matched against the template-ids occurring into the NNS.
381 unsigned NumTemplParamLists;
382 /// TemplParamLists - A new-allocated array of size NumTemplParamLists,
383 /// containing pointers to the matched template parameter lists.
384 TemplateParameterList** TemplParamLists;
386 /// Default constructor.
388 : NNS(0), NNSRange(), NumTemplParamLists(0), TemplParamLists(0) {}
389 /// setTemplateParameterListsInfo - Sets info about matched template
391 void setTemplateParameterListsInfo(ASTContext &Context,
393 TemplateParameterList **TPLists);
395 void Destroy(ASTContext &Context);
398 // Copy constructor and copy assignment are disabled.
399 QualifierInfo(const QualifierInfo&);
400 QualifierInfo& operator=(const QualifierInfo&);
403 /// \brief Represents a ValueDecl that came out of a declarator.
404 /// Contains type source information through TypeSourceInfo.
405 class DeclaratorDecl : public ValueDecl {
406 // A struct representing both a TInfo and a syntactic qualifier,
407 // to be used for the (uncommon) case of out-of-line declarations.
408 struct ExtInfo : public QualifierInfo {
409 TypeSourceInfo *TInfo;
412 llvm::PointerUnion<TypeSourceInfo*, ExtInfo*> DeclInfo;
414 bool hasExtInfo() const { return DeclInfo.is<ExtInfo*>(); }
415 ExtInfo *getExtInfo() { return DeclInfo.get<ExtInfo*>(); }
416 const ExtInfo *getExtInfo() const { return DeclInfo.get<ExtInfo*>(); }
419 DeclaratorDecl(Kind DK, DeclContext *DC, SourceLocation L,
420 DeclarationName N, QualType T, TypeSourceInfo *TInfo)
421 : ValueDecl(DK, DC, L, N, T), DeclInfo(TInfo) {}
424 virtual ~DeclaratorDecl();
425 virtual void Destroy(ASTContext &C);
427 TypeSourceInfo *getTypeSourceInfo() const {
429 ? getExtInfo()->TInfo
430 : DeclInfo.get<TypeSourceInfo*>();
432 void setTypeSourceInfo(TypeSourceInfo *TI) {
434 getExtInfo()->TInfo = TI;
439 /// getInnerLocStart - Return SourceLocation representing start of source
440 /// range ignoring outer template declarations.
441 virtual SourceLocation getInnerLocStart() const { return getLocation(); }
443 /// getOuterLocStart - Return SourceLocation representing start of source
444 /// range taking into account any outer template declarations.
445 SourceLocation getOuterLocStart() const;
446 SourceRange getSourceRange() const {
447 return SourceRange(getOuterLocStart(), getLocation());
450 NestedNameSpecifier *getQualifier() const {
451 return hasExtInfo() ? getExtInfo()->NNS : 0;
453 SourceRange getQualifierRange() const {
454 return hasExtInfo() ? getExtInfo()->NNSRange : SourceRange();
456 void setQualifierInfo(NestedNameSpecifier *Qualifier,
457 SourceRange QualifierRange);
459 unsigned getNumTemplateParameterLists() const {
460 return hasExtInfo() ? getExtInfo()->NumTemplParamLists : 0;
462 TemplateParameterList *getTemplateParameterList(unsigned index) const {
463 assert(index < getNumTemplateParameterLists());
464 return getExtInfo()->TemplParamLists[index];
466 void setTemplateParameterListsInfo(ASTContext &Context, unsigned NumTPLists,
467 TemplateParameterList **TPLists) {
468 getExtInfo()->setTemplateParameterListsInfo(Context, NumTPLists, TPLists);
471 SourceLocation getTypeSpecStartLoc() const;
473 // Implement isa/cast/dyncast/etc.
474 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
475 static bool classof(const DeclaratorDecl *D) { return true; }
476 static bool classofKind(Kind K) {
477 return K >= firstDeclarator && K <= lastDeclarator;
481 /// \brief Structure used to store a statement, the constant value to
482 /// which it was evaluated (if any), and whether or not the statement
483 /// is an integral constant expression (if known).
484 struct EvaluatedStmt {
485 EvaluatedStmt() : WasEvaluated(false), IsEvaluating(false), CheckedICE(false),
486 CheckingICE(false), IsICE(false) { }
488 /// \brief Whether this statement was already evaluated.
489 bool WasEvaluated : 1;
491 /// \brief Whether this statement is being evaluated.
492 bool IsEvaluating : 1;
494 /// \brief Whether we already checked whether this statement was an
495 /// integral constant expression.
498 /// \brief Whether we are checking whether this statement is an
499 /// integral constant expression.
500 bool CheckingICE : 1;
502 /// \brief Whether this statement is an integral constant
503 /// expression. Only valid if CheckedICE is true.
510 // \brief Describes the kind of template specialization that a
511 // particular template specialization declaration represents.
512 enum TemplateSpecializationKind {
513 /// This template specialization was formed from a template-id but
514 /// has not yet been declared, defined, or instantiated.
516 /// This template specialization was implicitly instantiated from a
517 /// template. (C++ [temp.inst]).
518 TSK_ImplicitInstantiation,
519 /// This template specialization was declared or defined by an
520 /// explicit specialization (C++ [temp.expl.spec]) or partial
521 /// specialization (C++ [temp.class.spec]).
522 TSK_ExplicitSpecialization,
523 /// This template specialization was instantiated from a template
524 /// due to an explicit instantiation declaration request
525 /// (C++0x [temp.explicit]).
526 TSK_ExplicitInstantiationDeclaration,
527 /// This template specialization was instantiated from a template
528 /// due to an explicit instantiation definition request
529 /// (C++ [temp.explicit]).
530 TSK_ExplicitInstantiationDefinition
533 /// VarDecl - An instance of this class is created to represent a variable
534 /// declaration or definition.
535 class VarDecl : public DeclaratorDecl, public Redeclarable<VarDecl> {
538 None, Auto, Register, Extern, Static, PrivateExtern
541 /// getStorageClassSpecifierString - Return the string used to
542 /// specify the storage class \arg SC.
544 /// It is illegal to call this function with SC == None.
545 static const char *getStorageClassSpecifierString(StorageClass SC);
548 /// \brief Placeholder type used in Init to denote an unparsed C++ default
550 struct UnparsedDefaultArgument;
552 /// \brief Placeholder type used in Init to denote an uninstantiated C++
553 /// default argument.
554 struct UninstantiatedDefaultArgument;
556 typedef llvm::PointerUnion4<Stmt *, EvaluatedStmt *,
557 UnparsedDefaultArgument *,
558 UninstantiatedDefaultArgument *> InitType;
560 /// \brief The initializer for this variable or, for a ParmVarDecl, the
561 /// C++ default argument.
562 mutable InitType Init;
565 // FIXME: This can be packed into the bitfields in Decl.
567 unsigned SClassAsWritten : 3;
568 bool ThreadSpecified : 1;
569 bool HasCXXDirectInit : 1;
571 /// DeclaredInCondition - Whether this variable was declared in a
572 /// condition, e.g., if (int x = foo()) { ... }.
573 bool DeclaredInCondition : 1;
575 /// \brief Whether this variable is the exception variable in a C++ catch
576 /// or an Objective-C @catch statement.
577 bool ExceptionVar : 1;
579 /// \brief Whether this local variable could be allocated in the return
580 /// slot of its function, enabling the named return value optimization (NRVO).
581 bool NRVOVariable : 1;
583 friend class StmtIteratorBase;
585 VarDecl(Kind DK, DeclContext *DC, SourceLocation L, IdentifierInfo *Id,
586 QualType T, TypeSourceInfo *TInfo, StorageClass SC,
587 StorageClass SCAsWritten)
588 : DeclaratorDecl(DK, DC, L, Id, T, TInfo), Init(),
589 ThreadSpecified(false), HasCXXDirectInit(false),
590 DeclaredInCondition(false), ExceptionVar(false), NRVOVariable(false) {
592 SClassAsWritten = SCAsWritten;
595 typedef Redeclarable<VarDecl> redeclarable_base;
596 virtual VarDecl *getNextRedeclaration() { return RedeclLink.getNext(); }
599 typedef redeclarable_base::redecl_iterator redecl_iterator;
600 redecl_iterator redecls_begin() const {
601 return redeclarable_base::redecls_begin();
603 redecl_iterator redecls_end() const {
604 return redeclarable_base::redecls_end();
607 static VarDecl *Create(ASTContext &C, DeclContext *DC,
608 SourceLocation L, IdentifierInfo *Id,
609 QualType T, TypeSourceInfo *TInfo, StorageClass S,
610 StorageClass SCAsWritten);
612 virtual void Destroy(ASTContext& C);
615 virtual SourceLocation getInnerLocStart() const;
616 virtual SourceRange getSourceRange() const;
618 StorageClass getStorageClass() const { return (StorageClass)SClass; }
619 StorageClass getStorageClassAsWritten() const {
620 return (StorageClass) SClassAsWritten;
622 void setStorageClass(StorageClass SC) { SClass = SC; }
623 void setStorageClassAsWritten(StorageClass SC) { SClassAsWritten = SC; }
625 void setThreadSpecified(bool T) { ThreadSpecified = T; }
626 bool isThreadSpecified() const {
627 return ThreadSpecified;
630 /// hasLocalStorage - Returns true if a variable with function scope
631 /// is a non-static local variable.
632 bool hasLocalStorage() const {
633 if (getStorageClass() == None)
634 return !isFileVarDecl();
636 // Return true for: Auto, Register.
637 // Return false for: Extern, Static, PrivateExtern.
639 return getStorageClass() <= Register;
642 /// isStaticLocal - Returns true if a variable with function scope is a
643 /// static local variable.
644 bool isStaticLocal() const {
645 return getStorageClass() == Static && !isFileVarDecl();
648 /// hasExternStorage - Returns true if a variable has extern or
649 /// __private_extern__ storage.
650 bool hasExternalStorage() const {
651 return getStorageClass() == Extern || getStorageClass() == PrivateExtern;
654 /// hasGlobalStorage - Returns true for all variables that do not
655 /// have local storage. This includs all global variables as well
656 /// as static variables declared within a function.
657 bool hasGlobalStorage() const { return !hasLocalStorage(); }
659 /// \brief Determines whether this variable is a variable with
660 /// external, C linkage.
661 bool isExternC() const;
663 /// isBlockVarDecl - Returns true for local variable declarations. Note that
664 /// this includes static variables inside of functions. It also includes
665 /// variables inside blocks.
667 /// void foo() { int x; static int y; extern int z; }
669 bool isBlockVarDecl() const {
670 if (getKind() != Decl::Var)
672 if (const DeclContext *DC = getDeclContext())
673 return DC->getLookupContext()->isFunctionOrMethod();
677 /// isFunctionOrMethodVarDecl - Similar to isBlockVarDecl, but excludes
678 /// variables declared in blocks.
679 bool isFunctionOrMethodVarDecl() const {
680 if (getKind() != Decl::Var)
682 if (const DeclContext *DC = getDeclContext())
683 return DC->getLookupContext()->isFunctionOrMethod() &&
684 DC->getLookupContext()->getDeclKind() != Decl::Block;
688 /// \brief Determines whether this is a static data member.
690 /// This will only be true in C++, and applies to, e.g., the
697 bool isStaticDataMember() const {
698 // If it wasn't static, it would be a FieldDecl.
699 return getDeclContext()->isRecord();
702 virtual VarDecl *getCanonicalDecl();
703 const VarDecl *getCanonicalDecl() const {
704 return const_cast<VarDecl*>(this)->getCanonicalDecl();
707 enum DefinitionKind {
708 DeclarationOnly, ///< This declaration is only a declaration.
709 TentativeDefinition, ///< This declaration is a tentative definition.
710 Definition ///< This declaration is definitely a definition.
713 /// \brief Check whether this declaration is a definition. If this could be
714 /// a tentative definition (in C), don't check whether there's an overriding
716 DefinitionKind isThisDeclarationADefinition() const;
718 /// \brief Get the tentative definition that acts as the real definition in
719 /// a TU. Returns null if there is a proper definition available.
720 VarDecl *getActingDefinition();
721 const VarDecl *getActingDefinition() const {
722 return const_cast<VarDecl*>(this)->getActingDefinition();
725 /// \brief Determine whether this is a tentative definition of a
727 bool isTentativeDefinitionNow() const;
729 /// \brief Get the real (not just tentative) definition for this declaration.
730 VarDecl *getDefinition();
731 const VarDecl *getDefinition() const {
732 return const_cast<VarDecl*>(this)->getDefinition();
735 /// \brief Determine whether this is or was instantiated from an out-of-line
736 /// definition of a static data member.
737 virtual bool isOutOfLine() const;
739 /// \brief If this is a static data member, find its out-of-line definition.
740 VarDecl *getOutOfLineDefinition();
742 /// isFileVarDecl - Returns true for file scoped variable declaration.
743 bool isFileVarDecl() const {
744 if (getKind() != Decl::Var)
746 if (const DeclContext *Ctx = getDeclContext()) {
747 Ctx = Ctx->getLookupContext();
748 if (isa<TranslationUnitDecl>(Ctx) || isa<NamespaceDecl>(Ctx) )
751 if (isStaticDataMember())
757 /// getAnyInitializer - Get the initializer for this variable, no matter which
758 /// declaration it is attached to.
759 const Expr *getAnyInitializer() const {
761 return getAnyInitializer(D);
764 /// getAnyInitializer - Get the initializer for this variable, no matter which
765 /// declaration it is attached to. Also get that declaration.
766 const Expr *getAnyInitializer(const VarDecl *&D) const;
768 bool hasInit() const {
769 return !Init.isNull();
771 const Expr *getInit() const {
775 const Stmt *S = Init.dyn_cast<Stmt *>();
777 if (EvaluatedStmt *ES = Init.dyn_cast<EvaluatedStmt*>())
780 return (const Expr*) S;
786 Stmt *S = Init.dyn_cast<Stmt *>();
788 if (EvaluatedStmt *ES = Init.dyn_cast<EvaluatedStmt*>())
795 /// \brief Retrieve the address of the initializer expression.
796 Stmt **getInitAddress() {
797 if (EvaluatedStmt *ES = Init.dyn_cast<EvaluatedStmt*>())
800 // This union hack tip-toes around strict-aliasing rules.
810 void setInit(Expr *I);
812 EvaluatedStmt *EnsureEvaluatedStmt() const {
813 EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>();
815 Stmt *S = Init.get<Stmt *>();
816 Eval = new (getASTContext()) EvaluatedStmt;
823 /// \brief Check whether we are in the process of checking whether the
824 /// initializer can be evaluated.
825 bool isEvaluatingValue() const {
826 if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>())
827 return Eval->IsEvaluating;
832 /// \brief Note that we now are checking whether the initializer can be
834 void setEvaluatingValue() const {
835 EvaluatedStmt *Eval = EnsureEvaluatedStmt();
836 Eval->IsEvaluating = true;
839 /// \brief Note that constant evaluation has computed the given
840 /// value for this variable's initializer.
841 void setEvaluatedValue(const APValue &Value) const {
842 EvaluatedStmt *Eval = EnsureEvaluatedStmt();
843 Eval->IsEvaluating = false;
844 Eval->WasEvaluated = true;
845 Eval->Evaluated = Value;
848 /// \brief Return the already-evaluated value of this variable's
849 /// initializer, or NULL if the value is not yet known. Returns pointer
850 /// to untyped APValue if the value could not be evaluated.
851 APValue *getEvaluatedValue() const {
852 if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>())
853 if (Eval->WasEvaluated)
854 return &Eval->Evaluated;
859 /// \brief Determines whether it is already known whether the
860 /// initializer is an integral constant expression or not.
861 bool isInitKnownICE() const {
862 if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>())
863 return Eval->CheckedICE;
868 /// \brief Determines whether the initializer is an integral
869 /// constant expression.
871 /// \pre isInitKnownICE()
872 bool isInitICE() const {
873 assert(isInitKnownICE() &&
874 "Check whether we already know that the initializer is an ICE");
875 return Init.get<EvaluatedStmt *>()->IsICE;
878 /// \brief Check whether we are in the process of checking the initializer
879 /// is an integral constant expression.
880 bool isCheckingICE() const {
881 if (EvaluatedStmt *Eval = Init.dyn_cast<EvaluatedStmt *>())
882 return Eval->CheckingICE;
887 /// \brief Note that we now are checking whether the initializer is an
888 /// integral constant expression.
889 void setCheckingICE() const {
890 EvaluatedStmt *Eval = EnsureEvaluatedStmt();
891 Eval->CheckingICE = true;
894 /// \brief Note that we now know whether the initializer is an
895 /// integral constant expression.
896 void setInitKnownICE(bool IsICE) const {
897 EvaluatedStmt *Eval = EnsureEvaluatedStmt();
898 Eval->CheckingICE = false;
899 Eval->CheckedICE = true;
903 void setCXXDirectInitializer(bool T) { HasCXXDirectInit = T; }
905 /// hasCXXDirectInitializer - If true, the initializer was a direct
906 /// initializer, e.g: "int x(1);". The Init expression will be the expression
907 /// inside the parens or a "ClassType(a,b,c)" class constructor expression for
908 /// class types. Clients can distinguish between "int x(1);" and "int x=1;"
909 /// by checking hasCXXDirectInitializer.
911 bool hasCXXDirectInitializer() const {
912 return HasCXXDirectInit;
915 /// isDeclaredInCondition - Whether this variable was declared as
916 /// part of a condition in an if/switch/while statement, e.g.,
918 /// if (int x = foo()) { ... }
920 bool isDeclaredInCondition() const {
921 return DeclaredInCondition;
923 void setDeclaredInCondition(bool InCondition) {
924 DeclaredInCondition = InCondition;
927 /// \brief Determine whether this variable is the exception variable in a
928 /// C++ catch statememt or an Objective-C @catch statement.
929 bool isExceptionVariable() const {
932 void setExceptionVariable(bool EV) { ExceptionVar = EV; }
934 /// \brief Determine whether this local variable can be used with the named
935 /// return value optimization (NRVO).
937 /// The named return value optimization (NRVO) works by marking certain
938 /// non-volatile local variables of class type as NRVO objects. These
939 /// locals can be allocated within the return slot of their containing
940 /// function, in which case there is no need to copy the object to the
941 /// return slot when returning from the function. Within the function body,
942 /// each return that returns the NRVO object will have this variable as its
944 bool isNRVOVariable() const { return NRVOVariable; }
945 void setNRVOVariable(bool NRVO) { NRVOVariable = NRVO; }
947 /// \brief If this variable is an instantiated static data member of a
948 /// class template specialization, returns the templated static data member
949 /// from which it was instantiated.
950 VarDecl *getInstantiatedFromStaticDataMember() const;
952 /// \brief If this variable is a static data member, determine what kind of
953 /// template specialization or instantiation this is.
954 TemplateSpecializationKind getTemplateSpecializationKind() const;
956 /// \brief If this variable is an instantiation of a static data member of a
957 /// class template specialization, retrieves the member specialization
959 MemberSpecializationInfo *getMemberSpecializationInfo() const;
961 /// \brief For a static data member that was instantiated from a static
962 /// data member of a class template, set the template specialiation kind.
963 void setTemplateSpecializationKind(TemplateSpecializationKind TSK,
964 SourceLocation PointOfInstantiation = SourceLocation());
966 // Implement isa/cast/dyncast/etc.
967 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
968 static bool classof(const VarDecl *D) { return true; }
969 static bool classofKind(Kind K) { return K >= firstVar && K <= lastVar; }
972 class ImplicitParamDecl : public VarDecl {
974 ImplicitParamDecl(Kind DK, DeclContext *DC, SourceLocation L,
975 IdentifierInfo *Id, QualType Tw)
976 : VarDecl(DK, DC, L, Id, Tw, /*TInfo=*/0, VarDecl::None, VarDecl::None) {}
978 static ImplicitParamDecl *Create(ASTContext &C, DeclContext *DC,
979 SourceLocation L, IdentifierInfo *Id,
981 // Implement isa/cast/dyncast/etc.
982 static bool classof(const ImplicitParamDecl *D) { return true; }
983 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
984 static bool classofKind(Kind K) { return K == ImplicitParam; }
987 /// ParmVarDecl - Represent a parameter to a function.
988 class ParmVarDecl : public VarDecl {
989 // NOTE: VC++ treats enums as signed, avoid using the ObjCDeclQualifier enum
990 /// FIXME: Also can be paced into the bitfields in Decl.
992 unsigned objcDeclQualifier : 6;
993 bool HasInheritedDefaultArg : 1;
996 ParmVarDecl(Kind DK, DeclContext *DC, SourceLocation L,
997 IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo,
998 StorageClass S, StorageClass SCAsWritten, Expr *DefArg)
999 : VarDecl(DK, DC, L, Id, T, TInfo, S, SCAsWritten),
1000 objcDeclQualifier(OBJC_TQ_None), HasInheritedDefaultArg(false) {
1001 setDefaultArg(DefArg);
1005 static ParmVarDecl *Create(ASTContext &C, DeclContext *DC,
1006 SourceLocation L,IdentifierInfo *Id,
1007 QualType T, TypeSourceInfo *TInfo,
1008 StorageClass S, StorageClass SCAsWritten,
1011 ObjCDeclQualifier getObjCDeclQualifier() const {
1012 return ObjCDeclQualifier(objcDeclQualifier);
1014 void setObjCDeclQualifier(ObjCDeclQualifier QTVal) {
1015 objcDeclQualifier = QTVal;
1018 Expr *getDefaultArg();
1019 const Expr *getDefaultArg() const {
1020 return const_cast<ParmVarDecl *>(this)->getDefaultArg();
1023 void setDefaultArg(Expr *defarg) {
1024 Init = reinterpret_cast<Stmt *>(defarg);
1027 unsigned getNumDefaultArgTemporaries() const;
1028 CXXTemporary *getDefaultArgTemporary(unsigned i);
1029 const CXXTemporary *getDefaultArgTemporary(unsigned i) const {
1030 return const_cast<ParmVarDecl *>(this)->getDefaultArgTemporary(i);
1033 /// \brief Retrieve the source range that covers the entire default
1035 SourceRange getDefaultArgRange() const;
1036 void setUninstantiatedDefaultArg(Expr *arg) {
1037 Init = reinterpret_cast<UninstantiatedDefaultArgument *>(arg);
1039 Expr *getUninstantiatedDefaultArg() {
1040 return (Expr *)Init.get<UninstantiatedDefaultArgument *>();
1042 const Expr *getUninstantiatedDefaultArg() const {
1043 return (const Expr *)Init.get<UninstantiatedDefaultArgument *>();
1046 /// hasDefaultArg - Determines whether this parameter has a default argument,
1047 /// either parsed or not.
1048 bool hasDefaultArg() const {
1049 return getInit() || hasUnparsedDefaultArg() ||
1050 hasUninstantiatedDefaultArg();
1053 /// hasUnparsedDefaultArg - Determines whether this parameter has a
1054 /// default argument that has not yet been parsed. This will occur
1055 /// during the processing of a C++ class whose member functions have
1056 /// default arguments, e.g.,
1060 /// void f(int x = 17); // x has an unparsed default argument now
1061 /// }; // x has a regular default argument now
1063 bool hasUnparsedDefaultArg() const {
1064 return Init.is<UnparsedDefaultArgument*>();
1067 bool hasUninstantiatedDefaultArg() const {
1068 return Init.is<UninstantiatedDefaultArgument*>();
1071 /// setUnparsedDefaultArg - Specify that this parameter has an
1072 /// unparsed default argument. The argument will be replaced with a
1073 /// real default argument via setDefaultArg when the class
1074 /// definition enclosing the function declaration that owns this
1075 /// default argument is completed.
1076 void setUnparsedDefaultArg() {
1077 Init = (UnparsedDefaultArgument *)0;
1080 bool hasInheritedDefaultArg() const {
1081 return HasInheritedDefaultArg;
1084 void setHasInheritedDefaultArg(bool I = true) {
1085 HasInheritedDefaultArg = I;
1088 QualType getOriginalType() const {
1089 if (getTypeSourceInfo())
1090 return getTypeSourceInfo()->getType();
1094 /// setOwningFunction - Sets the function declaration that owns this
1095 /// ParmVarDecl. Since ParmVarDecls are often created before the
1096 /// FunctionDecls that own them, this routine is required to update
1097 /// the DeclContext appropriately.
1098 void setOwningFunction(DeclContext *FD) { setDeclContext(FD); }
1100 // Implement isa/cast/dyncast/etc.
1101 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1102 static bool classof(const ParmVarDecl *D) { return true; }
1103 static bool classofKind(Kind K) { return K == ParmVar; }
1106 /// FunctionDecl - An instance of this class is created to represent a
1107 /// function declaration or definition.
1109 /// Since a given function can be declared several times in a program,
1110 /// there may be several FunctionDecls that correspond to that
1111 /// function. Only one of those FunctionDecls will be found when
1112 /// traversing the list of declarations in the context of the
1113 /// FunctionDecl (e.g., the translation unit); this FunctionDecl
1114 /// contains all of the information known about the function. Other,
1115 /// previous declarations of the function are available via the
1116 /// getPreviousDeclaration() chain.
1117 class FunctionDecl : public DeclaratorDecl, public DeclContext,
1118 public Redeclarable<FunctionDecl> {
1121 None, Extern, Static, PrivateExtern
1124 /// \brief The kind of templated function a FunctionDecl can be.
1125 enum TemplatedKind {
1127 TK_FunctionTemplate,
1128 TK_MemberSpecialization,
1129 TK_FunctionTemplateSpecialization,
1130 TK_DependentFunctionTemplateSpecialization
1134 /// ParamInfo - new[]'d array of pointers to VarDecls for the formal
1135 /// parameters of this function. This is null if a prototype or if there are
1137 ParmVarDecl **ParamInfo;
1139 LazyDeclStmtPtr Body;
1141 // FIXME: This can be packed into the bitfields in Decl.
1142 // NOTE: VC++ treats enums as signed, avoid using the StorageClass enum
1143 unsigned SClass : 2;
1144 unsigned SClassAsWritten : 2;
1146 bool IsVirtualAsWritten : 1;
1148 bool HasInheritedPrototype : 1;
1149 bool HasWrittenPrototype : 1;
1151 bool IsTrivial : 1; // sunk from CXXMethodDecl
1152 bool IsCopyAssignment : 1; // sunk from CXXMethodDecl
1153 bool HasImplicitReturnZero : 1;
1155 /// \brief End part of this FunctionDecl's source range.
1157 /// We could compute the full range in getSourceRange(). However, when we're
1158 /// dealing with a function definition deserialized from a PCH/AST file,
1159 /// we can only compute the full range once the function body has been
1160 /// de-serialized, so it's far better to have the (sometimes-redundant)
1162 SourceLocation EndRangeLoc;
1164 /// \brief The template or declaration that this declaration
1165 /// describes or was instantiated from, respectively.
1167 /// For non-templates, this value will be NULL. For function
1168 /// declarations that describe a function template, this will be a
1169 /// pointer to a FunctionTemplateDecl. For member functions
1170 /// of class template specializations, this will be a MemberSpecializationInfo
1171 /// pointer containing information about the specialization.
1172 /// For function template specializations, this will be a
1173 /// FunctionTemplateSpecializationInfo, which contains information about
1174 /// the template being specialized and the template arguments involved in
1175 /// that specialization.
1176 llvm::PointerUnion4<FunctionTemplateDecl *,
1177 MemberSpecializationInfo *,
1178 FunctionTemplateSpecializationInfo *,
1179 DependentFunctionTemplateSpecializationInfo *>
1180 TemplateOrSpecialization;
1183 FunctionDecl(Kind DK, DeclContext *DC, SourceLocation L,
1184 DeclarationName N, QualType T, TypeSourceInfo *TInfo,
1185 StorageClass S, StorageClass SCAsWritten, bool isInline)
1186 : DeclaratorDecl(DK, DC, L, N, T, TInfo),
1188 ParamInfo(0), Body(),
1189 SClass(S), SClassAsWritten(SCAsWritten), IsInline(isInline),
1190 IsVirtualAsWritten(false), IsPure(false), HasInheritedPrototype(false),
1191 HasWrittenPrototype(true), IsDeleted(false), IsTrivial(false),
1192 IsCopyAssignment(false),
1193 HasImplicitReturnZero(false),
1194 EndRangeLoc(L), TemplateOrSpecialization() {}
1196 virtual ~FunctionDecl() {}
1197 virtual void Destroy(ASTContext& C);
1199 typedef Redeclarable<FunctionDecl> redeclarable_base;
1200 virtual FunctionDecl *getNextRedeclaration() { return RedeclLink.getNext(); }
1203 typedef redeclarable_base::redecl_iterator redecl_iterator;
1204 redecl_iterator redecls_begin() const {
1205 return redeclarable_base::redecls_begin();
1207 redecl_iterator redecls_end() const {
1208 return redeclarable_base::redecls_end();
1211 static FunctionDecl *Create(ASTContext &C, DeclContext *DC, SourceLocation L,
1212 DeclarationName N, QualType T,
1213 TypeSourceInfo *TInfo,
1214 StorageClass S = None,
1215 StorageClass SCAsWritten = None,
1216 bool isInline = false,
1217 bool hasWrittenPrototype = true);
1219 virtual void getNameForDiagnostic(std::string &S,
1220 const PrintingPolicy &Policy,
1221 bool Qualified) const;
1223 virtual SourceRange getSourceRange() const {
1224 return SourceRange(getOuterLocStart(), EndRangeLoc);
1226 void setLocEnd(SourceLocation E) {
1230 /// \brief Returns true if the function has a body (definition). The
1231 /// function body might be in any of the (re-)declarations of this
1232 /// function. The variant that accepts a FunctionDecl pointer will
1233 /// set that function declaration to the actual declaration
1234 /// containing the body (if there is one).
1235 bool hasBody(const FunctionDecl *&Definition) const;
1237 virtual bool hasBody() const {
1238 const FunctionDecl* Definition;
1239 return hasBody(Definition);
1242 /// getBody - Retrieve the body (definition) of the function. The
1243 /// function body might be in any of the (re-)declarations of this
1244 /// function. The variant that accepts a FunctionDecl pointer will
1245 /// set that function declaration to the actual declaration
1246 /// containing the body (if there is one).
1247 /// NOTE: For checking if there is a body, use hasBody() instead, to avoid
1248 /// unnecessary PCH de-serialization of the body.
1249 Stmt *getBody(const FunctionDecl *&Definition) const;
1251 virtual Stmt *getBody() const {
1252 const FunctionDecl* Definition;
1253 return getBody(Definition);
1256 /// isThisDeclarationADefinition - Returns whether this specific
1257 /// declaration of the function is also a definition. This does not
1258 /// determine whether the function has been defined (e.g., in a
1259 /// previous definition); for that information, use getBody.
1260 /// FIXME: Should return true if function is deleted or defaulted. However,
1261 /// CodeGenModule.cpp uses it, and I don't know if this would break it.
1262 bool isThisDeclarationADefinition() const { return Body; }
1264 void setBody(Stmt *B);
1265 void setLazyBody(uint64_t Offset) { Body = Offset; }
1267 /// Whether this function is variadic.
1268 bool isVariadic() const;
1270 /// Whether this function is marked as virtual explicitly.
1271 bool isVirtualAsWritten() const { return IsVirtualAsWritten; }
1272 void setVirtualAsWritten(bool V) { IsVirtualAsWritten = V; }
1274 /// Whether this virtual function is pure, i.e. makes the containing class
1276 bool isPure() const { return IsPure; }
1277 void setPure(bool P = true) { IsPure = P; }
1279 /// Whether this function is "trivial" in some specialized C++ senses.
1280 /// Can only be true for default constructors, copy constructors,
1281 /// copy assignment operators, and destructors. Not meaningful until
1282 /// the class has been fully built by Sema.
1283 bool isTrivial() const { return IsTrivial; }
1284 void setTrivial(bool IT) { IsTrivial = IT; }
1286 bool isCopyAssignment() const { return IsCopyAssignment; }
1287 void setCopyAssignment(bool CA) { IsCopyAssignment = CA; }
1289 /// Whether falling off this function implicitly returns null/zero.
1290 /// If a more specific implicit return value is required, front-ends
1291 /// should synthesize the appropriate return statements.
1292 bool hasImplicitReturnZero() const { return HasImplicitReturnZero; }
1293 void setHasImplicitReturnZero(bool IRZ) { HasImplicitReturnZero = IRZ; }
1295 /// \brief Whether this function has a prototype, either because one
1296 /// was explicitly written or because it was "inherited" by merging
1297 /// a declaration without a prototype with a declaration that has a
1299 bool hasPrototype() const {
1300 return HasWrittenPrototype || HasInheritedPrototype;
1303 bool hasWrittenPrototype() const { return HasWrittenPrototype; }
1304 void setHasWrittenPrototype(bool P) { HasWrittenPrototype = P; }
1306 /// \brief Whether this function inherited its prototype from a
1307 /// previous declaration.
1308 bool hasInheritedPrototype() const { return HasInheritedPrototype; }
1309 void setHasInheritedPrototype(bool P = true) { HasInheritedPrototype = P; }
1311 /// \brief Whether this function has been deleted.
1313 /// A function that is "deleted" (via the C++0x "= delete" syntax)
1314 /// acts like a normal function, except that it cannot actually be
1315 /// called or have its address taken. Deleted functions are
1316 /// typically used in C++ overload resolution to attract arguments
1317 /// whose type or lvalue/rvalue-ness would permit the use of a
1318 /// different overload that would behave incorrectly. For example,
1319 /// one might use deleted functions to ban implicit conversion from
1320 /// a floating-point number to an Integer type:
1323 /// struct Integer {
1324 /// Integer(long); // construct from a long
1325 /// Integer(double) = delete; // no construction from float or double
1326 /// Integer(long double) = delete; // no construction from long double
1329 bool isDeleted() const { return IsDeleted; }
1330 void setDeleted(bool D = true) { IsDeleted = D; }
1332 /// \brief Determines whether this is a function "main", which is
1333 /// the entry point into an executable program.
1334 bool isMain() const;
1336 /// \brief Determines whether this function is a function with
1337 /// external, C linkage.
1338 bool isExternC() const;
1340 /// \brief Determines whether this is a global function.
1341 bool isGlobal() const;
1343 void setPreviousDeclaration(FunctionDecl * PrevDecl);
1345 virtual const FunctionDecl *getCanonicalDecl() const;
1346 virtual FunctionDecl *getCanonicalDecl();
1348 unsigned getBuiltinID() const;
1350 // Iterator access to formal parameters.
1351 unsigned param_size() const { return getNumParams(); }
1352 typedef ParmVarDecl **param_iterator;
1353 typedef ParmVarDecl * const *param_const_iterator;
1355 param_iterator param_begin() { return ParamInfo; }
1356 param_iterator param_end() { return ParamInfo+param_size(); }
1358 param_const_iterator param_begin() const { return ParamInfo; }
1359 param_const_iterator param_end() const { return ParamInfo+param_size(); }
1361 /// getNumParams - Return the number of parameters this function must have
1362 /// based on its FunctionType. This is the length of the ParamInfo array
1363 /// after it has been created.
1364 unsigned getNumParams() const;
1366 const ParmVarDecl *getParamDecl(unsigned i) const {
1367 assert(i < getNumParams() && "Illegal param #");
1368 return ParamInfo[i];
1370 ParmVarDecl *getParamDecl(unsigned i) {
1371 assert(i < getNumParams() && "Illegal param #");
1372 return ParamInfo[i];
1374 void setParams(ParmVarDecl **NewParamInfo, unsigned NumParams);
1376 /// getMinRequiredArguments - Returns the minimum number of arguments
1377 /// needed to call this function. This may be fewer than the number of
1378 /// function parameters, if some of the parameters have default
1379 /// arguments (in C++).
1380 unsigned getMinRequiredArguments() const;
1382 QualType getResultType() const {
1383 return getType()->getAs<FunctionType>()->getResultType();
1386 /// \brief Determine the type of an expression that calls this function.
1387 QualType getCallResultType() const {
1388 return getType()->getAs<FunctionType>()->getCallResultType(getASTContext());
1391 StorageClass getStorageClass() const { return StorageClass(SClass); }
1392 void setStorageClass(StorageClass SC) { SClass = SC; }
1394 StorageClass getStorageClassAsWritten() const {
1395 return StorageClass(SClassAsWritten);
1397 void setStorageClassAsWritten(StorageClass SC) { SClassAsWritten = SC; }
1399 /// \brief Determine whether the "inline" keyword was specified for this
1401 bool isInlineSpecified() const { return IsInline; }
1403 /// Set whether the "inline" keyword was specified for this function.
1404 void setInlineSpecified(bool I) { IsInline = I; }
1406 /// \brief Determine whether this function should be inlined, because it is
1407 /// either marked "inline" or is a member function of a C++ class that
1408 /// was defined in the class body.
1409 bool isInlined() const;
1411 bool isInlineDefinitionExternallyVisible() const;
1413 /// isOverloadedOperator - Whether this function declaration
1414 /// represents an C++ overloaded operator, e.g., "operator+".
1415 bool isOverloadedOperator() const {
1416 return getOverloadedOperator() != OO_None;
1419 OverloadedOperatorKind getOverloadedOperator() const;
1421 const IdentifierInfo *getLiteralIdentifier() const;
1423 /// \brief If this function is an instantiation of a member function
1424 /// of a class template specialization, retrieves the function from
1425 /// which it was instantiated.
1427 /// This routine will return non-NULL for (non-templated) member
1428 /// functions of class templates and for instantiations of function
1429 /// templates. For example, given:
1432 /// template<typename T>
1438 /// The declaration for X<int>::f is a (non-templated) FunctionDecl
1439 /// whose parent is the class template specialization X<int>. For
1440 /// this declaration, getInstantiatedFromFunction() will return
1441 /// the FunctionDecl X<T>::A. When a complete definition of
1442 /// X<int>::A is required, it will be instantiated from the
1443 /// declaration returned by getInstantiatedFromMemberFunction().
1444 FunctionDecl *getInstantiatedFromMemberFunction() const;
1446 /// \brief What kind of templated function this is.
1447 TemplatedKind getTemplatedKind() const;
1449 /// \brief If this function is an instantiation of a member function of a
1450 /// class template specialization, retrieves the member specialization
1452 MemberSpecializationInfo *getMemberSpecializationInfo() const;
1454 /// \brief Specify that this record is an instantiation of the
1455 /// member function FD.
1456 void setInstantiationOfMemberFunction(FunctionDecl *FD,
1457 TemplateSpecializationKind TSK);
1459 /// \brief Retrieves the function template that is described by this
1460 /// function declaration.
1462 /// Every function template is represented as a FunctionTemplateDecl
1463 /// and a FunctionDecl (or something derived from FunctionDecl). The
1464 /// former contains template properties (such as the template
1465 /// parameter lists) while the latter contains the actual
1466 /// description of the template's
1467 /// contents. FunctionTemplateDecl::getTemplatedDecl() retrieves the
1468 /// FunctionDecl that describes the function template,
1469 /// getDescribedFunctionTemplate() retrieves the
1470 /// FunctionTemplateDecl from a FunctionDecl.
1471 FunctionTemplateDecl *getDescribedFunctionTemplate() const {
1472 return TemplateOrSpecialization.dyn_cast<FunctionTemplateDecl*>();
1475 void setDescribedFunctionTemplate(FunctionTemplateDecl *Template) {
1476 TemplateOrSpecialization = Template;
1479 /// \brief Determine whether this function is a function template
1481 bool isFunctionTemplateSpecialization() const {
1482 return getPrimaryTemplate() != 0;
1485 /// \brief If this function is actually a function template specialization,
1486 /// retrieve information about this function template specialization.
1487 /// Otherwise, returns NULL.
1488 FunctionTemplateSpecializationInfo *getTemplateSpecializationInfo() const {
1489 return TemplateOrSpecialization.
1490 dyn_cast<FunctionTemplateSpecializationInfo*>();
1493 /// \brief Determines whether this function is a function template
1494 /// specialization or a member of a class template specialization that can
1495 /// be implicitly instantiated.
1496 bool isImplicitlyInstantiable() const;
1498 /// \brief Retrieve the function declaration from which this function could
1499 /// be instantiated, if it is an instantiation (rather than a non-template
1500 /// or a specialization, for example).
1501 FunctionDecl *getTemplateInstantiationPattern() const;
1503 /// \brief Retrieve the primary template that this function template
1504 /// specialization either specializes or was instantiated from.
1506 /// If this function declaration is not a function template specialization,
1508 FunctionTemplateDecl *getPrimaryTemplate() const;
1510 /// \brief Retrieve the template arguments used to produce this function
1511 /// template specialization from the primary template.
1513 /// If this function declaration is not a function template specialization,
1515 const TemplateArgumentList *getTemplateSpecializationArgs() const;
1517 /// \brief Retrieve the template argument list as written in the sources,
1520 /// If this function declaration is not a function template specialization
1521 /// or if it had no explicit template argument list, returns NULL.
1522 /// Note that it an explicit template argument list may be written empty,
1523 /// e.g., template<> void foo<>(char* s);
1524 const TemplateArgumentListInfo*
1525 getTemplateSpecializationArgsAsWritten() const;
1527 /// \brief Specify that this function declaration is actually a function
1528 /// template specialization.
1530 /// \param Template the function template that this function template
1531 /// specialization specializes.
1533 /// \param TemplateArgs the template arguments that produced this
1534 /// function template specialization from the template.
1536 /// \param InsertPos If non-NULL, the position in the function template
1537 /// specialization set where the function template specialization data will
1540 /// \param TSK the kind of template specialization this is.
1542 /// \param TemplateArgsAsWritten location info of template arguments.
1544 /// \param PointOfInstantiation point at which the function template
1545 /// specialization was first instantiated.
1546 void setFunctionTemplateSpecialization(FunctionTemplateDecl *Template,
1547 const TemplateArgumentList *TemplateArgs,
1549 TemplateSpecializationKind TSK = TSK_ImplicitInstantiation,
1550 const TemplateArgumentListInfo *TemplateArgsAsWritten = 0,
1551 SourceLocation PointOfInstantiation = SourceLocation());
1553 /// \brief Specify that this function declaration is actually a function
1554 /// template specialization.
1556 /// \param Template the function template that this function template
1557 /// specialization specializes.
1559 /// \param NumTemplateArgs number of template arguments that produced this
1560 /// function template specialization from the template.
1562 /// \param TemplateArgs array of template arguments that produced this
1563 /// function template specialization from the template.
1565 /// \param TSK the kind of template specialization this is.
1567 /// \param NumTemplateArgsAsWritten number of template arguments that produced
1568 /// this function template specialization from the template.
1570 /// \param TemplateArgsAsWritten array of location info for the template
1573 /// \param LAngleLoc location of left angle token.
1575 /// \param RAngleLoc location of right angle token.
1577 /// \param PointOfInstantiation point at which the function template
1578 /// specialization was first instantiated.
1579 void setFunctionTemplateSpecialization(FunctionTemplateDecl *Template,
1580 unsigned NumTemplateArgs,
1581 const TemplateArgument *TemplateArgs,
1582 TemplateSpecializationKind TSK,
1583 unsigned NumTemplateArgsAsWritten,
1584 TemplateArgumentLoc *TemplateArgsAsWritten,
1585 SourceLocation LAngleLoc,
1586 SourceLocation RAngleLoc,
1587 SourceLocation PointOfInstantiation);
1589 /// \brief Specifies that this function declaration is actually a
1590 /// dependent function template specialization.
1591 void setDependentTemplateSpecialization(ASTContext &Context,
1592 const UnresolvedSetImpl &Templates,
1593 const TemplateArgumentListInfo &TemplateArgs);
1595 DependentFunctionTemplateSpecializationInfo *
1596 getDependentSpecializationInfo() const {
1597 return TemplateOrSpecialization.
1598 dyn_cast<DependentFunctionTemplateSpecializationInfo*>();
1601 /// \brief Determine what kind of template instantiation this function
1603 TemplateSpecializationKind getTemplateSpecializationKind() const;
1605 /// \brief Determine what kind of template instantiation this function
1607 void setTemplateSpecializationKind(TemplateSpecializationKind TSK,
1608 SourceLocation PointOfInstantiation = SourceLocation());
1610 /// \brief Retrieve the (first) point of instantiation of a function template
1611 /// specialization or a member of a class template specialization.
1613 /// \returns the first point of instantiation, if this function was
1614 /// instantiated from a template; otherwie, returns an invalid source
1616 SourceLocation getPointOfInstantiation() const;
1618 /// \brief Determine whether this is or was instantiated from an out-of-line
1619 /// definition of a member function.
1620 virtual bool isOutOfLine() const;
1622 // Implement isa/cast/dyncast/etc.
1623 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1624 static bool classof(const FunctionDecl *D) { return true; }
1625 static bool classofKind(Kind K) {
1626 return K >= firstFunction && K <= lastFunction;
1628 static DeclContext *castToDeclContext(const FunctionDecl *D) {
1629 return static_cast<DeclContext *>(const_cast<FunctionDecl*>(D));
1631 static FunctionDecl *castFromDeclContext(const DeclContext *DC) {
1632 return static_cast<FunctionDecl *>(const_cast<DeclContext*>(DC));
1635 friend class PCHDeclReader;
1636 friend class PCHDeclWriter;
1640 /// FieldDecl - An instance of this class is created by Sema::ActOnField to
1641 /// represent a member of a struct/union/class.
1642 class FieldDecl : public DeclaratorDecl {
1643 // FIXME: This can be packed into the bitfields in Decl.
1647 FieldDecl(Kind DK, DeclContext *DC, SourceLocation L,
1648 IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo,
1649 Expr *BW, bool Mutable)
1650 : DeclaratorDecl(DK, DC, L, Id, T, TInfo), Mutable(Mutable), BitWidth(BW) {
1654 static FieldDecl *Create(ASTContext &C, DeclContext *DC, SourceLocation L,
1655 IdentifierInfo *Id, QualType T,
1656 TypeSourceInfo *TInfo, Expr *BW, bool Mutable);
1658 /// isMutable - Determines whether this field is mutable (C++ only).
1659 bool isMutable() const { return Mutable; }
1661 /// \brief Set whether this field is mutable (C++ only).
1662 void setMutable(bool M) { Mutable = M; }
1664 /// isBitfield - Determines whether this field is a bitfield.
1665 bool isBitField() const { return BitWidth != NULL; }
1667 /// @brief Determines whether this is an unnamed bitfield.
1668 bool isUnnamedBitfield() const { return BitWidth != NULL && !getDeclName(); }
1670 /// isAnonymousStructOrUnion - Determines whether this field is a
1671 /// representative for an anonymous struct or union. Such fields are
1672 /// unnamed and are implicitly generated by the implementation to
1673 /// store the data for the anonymous union or struct.
1674 bool isAnonymousStructOrUnion() const;
1676 Expr *getBitWidth() const { return BitWidth; }
1677 void setBitWidth(Expr *BW) { BitWidth = BW; }
1679 /// getParent - Returns the parent of this field declaration, which
1680 /// is the struct in which this method is defined.
1681 const RecordDecl *getParent() const {
1682 return cast<RecordDecl>(getDeclContext());
1685 RecordDecl *getParent() {
1686 return cast<RecordDecl>(getDeclContext());
1689 // Implement isa/cast/dyncast/etc.
1690 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1691 static bool classof(const FieldDecl *D) { return true; }
1692 static bool classofKind(Kind K) { return K >= firstField && K <= lastField; }
1695 /// EnumConstantDecl - An instance of this object exists for each enum constant
1696 /// that is defined. For example, in "enum X {a,b}", each of a/b are
1697 /// EnumConstantDecl's, X is an instance of EnumDecl, and the type of a/b is a
1698 /// TagType for the X EnumDecl.
1699 class EnumConstantDecl : public ValueDecl {
1700 Stmt *Init; // an integer constant expression
1701 llvm::APSInt Val; // The value.
1703 EnumConstantDecl(DeclContext *DC, SourceLocation L,
1704 IdentifierInfo *Id, QualType T, Expr *E,
1705 const llvm::APSInt &V)
1706 : ValueDecl(EnumConstant, DC, L, Id, T), Init((Stmt*)E), Val(V) {}
1708 virtual ~EnumConstantDecl() {}
1711 static EnumConstantDecl *Create(ASTContext &C, EnumDecl *DC,
1712 SourceLocation L, IdentifierInfo *Id,
1713 QualType T, Expr *E,
1714 const llvm::APSInt &V);
1716 virtual void Destroy(ASTContext& C);
1718 const Expr *getInitExpr() const { return (const Expr*) Init; }
1719 Expr *getInitExpr() { return (Expr*) Init; }
1720 const llvm::APSInt &getInitVal() const { return Val; }
1722 void setInitExpr(Expr *E) { Init = (Stmt*) E; }
1723 void setInitVal(const llvm::APSInt &V) { Val = V; }
1725 // Implement isa/cast/dyncast/etc.
1726 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1727 static bool classof(const EnumConstantDecl *D) { return true; }
1728 static bool classofKind(Kind K) { return K == EnumConstant; }
1730 friend class StmtIteratorBase;
1734 /// TypeDecl - Represents a declaration of a type.
1736 class TypeDecl : public NamedDecl {
1737 /// TypeForDecl - This indicates the Type object that represents
1738 /// this TypeDecl. It is a cache maintained by
1739 /// ASTContext::getTypedefType, ASTContext::getTagDeclType, and
1740 /// ASTContext::getTemplateTypeParmType, and TemplateTypeParmDecl.
1741 mutable Type *TypeForDecl;
1742 friend class ASTContext;
1743 friend class DeclContext;
1744 friend class TagDecl;
1745 friend class TemplateTypeParmDecl;
1746 friend class TagType;
1749 TypeDecl(Kind DK, DeclContext *DC, SourceLocation L,
1751 : NamedDecl(DK, DC, L, Id), TypeForDecl(0) {}
1754 // Low-level accessor
1755 Type *getTypeForDecl() const { return TypeForDecl; }
1756 void setTypeForDecl(Type *TD) { TypeForDecl = TD; }
1758 // Implement isa/cast/dyncast/etc.
1759 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1760 static bool classof(const TypeDecl *D) { return true; }
1761 static bool classofKind(Kind K) { return K >= firstType && K <= lastType; }
1765 class TypedefDecl : public TypeDecl, public Redeclarable<TypedefDecl> {
1766 /// UnderlyingType - This is the type the typedef is set to.
1767 TypeSourceInfo *TInfo;
1769 TypedefDecl(DeclContext *DC, SourceLocation L,
1770 IdentifierInfo *Id, TypeSourceInfo *TInfo)
1771 : TypeDecl(Typedef, DC, L, Id), TInfo(TInfo) {}
1773 virtual ~TypedefDecl();
1776 typedef Redeclarable<TypedefDecl> redeclarable_base;
1777 virtual TypedefDecl *getNextRedeclaration() { return RedeclLink.getNext(); }
1780 typedef redeclarable_base::redecl_iterator redecl_iterator;
1781 redecl_iterator redecls_begin() const {
1782 return redeclarable_base::redecls_begin();
1784 redecl_iterator redecls_end() const {
1785 return redeclarable_base::redecls_end();
1788 static TypedefDecl *Create(ASTContext &C, DeclContext *DC,
1789 SourceLocation L, IdentifierInfo *Id,
1790 TypeSourceInfo *TInfo);
1792 TypeSourceInfo *getTypeSourceInfo() const {
1796 /// Retrieves the canonical declaration of this typedef.
1797 TypedefDecl *getCanonicalDecl() {
1798 return getFirstDeclaration();
1800 const TypedefDecl *getCanonicalDecl() const {
1801 return getFirstDeclaration();
1804 QualType getUnderlyingType() const {
1805 return TInfo->getType();
1807 void setTypeSourceInfo(TypeSourceInfo *newType) {
1811 // Implement isa/cast/dyncast/etc.
1812 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1813 static bool classof(const TypedefDecl *D) { return true; }
1814 static bool classofKind(Kind K) { return K == Typedef; }
1819 /// TagDecl - Represents the declaration of a struct/union/class/enum.
1821 : public TypeDecl, public DeclContext, public Redeclarable<TagDecl> {
1823 // This is really ugly.
1824 typedef TagTypeKind TagKind;
1827 // FIXME: This can be packed into the bitfields in Decl.
1828 /// TagDeclKind - The TagKind enum.
1829 unsigned TagDeclKind : 2;
1831 /// IsDefinition - True if this is a definition ("struct foo {};"), false if
1832 /// it is a declaration ("struct foo;").
1833 bool IsDefinition : 1;
1835 /// IsEmbeddedInDeclarator - True if this tag declaration is
1836 /// "embedded" (i.e., defined or declared for the very first time)
1837 /// in the syntax of a declarator.
1838 bool IsEmbeddedInDeclarator : 1;
1841 // These are used by (and only defined for) EnumDecl.
1842 unsigned NumPositiveBits : 8;
1843 unsigned NumNegativeBits : 8;
1846 SourceLocation TagKeywordLoc;
1847 SourceLocation RBraceLoc;
1849 // A struct representing syntactic qualifier info,
1850 // to be used for the (uncommon) case of out-of-line declarations.
1851 typedef QualifierInfo ExtInfo;
1853 /// TypedefDeclOrQualifier - If the (out-of-line) tag declaration name
1854 /// is qualified, it points to the qualifier info (nns and range);
1855 /// otherwise, if the tag declaration is anonymous and it is part of
1856 /// a typedef, it points to the TypedefDecl (used for mangling);
1857 /// otherwise, it is a null (TypedefDecl) pointer.
1858 llvm::PointerUnion<TypedefDecl*, ExtInfo*> TypedefDeclOrQualifier;
1860 bool hasExtInfo() const { return TypedefDeclOrQualifier.is<ExtInfo*>(); }
1861 ExtInfo *getExtInfo() { return TypedefDeclOrQualifier.get<ExtInfo*>(); }
1862 const ExtInfo *getExtInfo() const {
1863 return TypedefDeclOrQualifier.get<ExtInfo*>();
1867 TagDecl(Kind DK, TagKind TK, DeclContext *DC,
1868 SourceLocation L, IdentifierInfo *Id,
1869 TagDecl *PrevDecl, SourceLocation TKL = SourceLocation())
1870 : TypeDecl(DK, DC, L, Id), DeclContext(DK), TagKeywordLoc(TKL),
1871 TypedefDeclOrQualifier((TypedefDecl*) 0) {
1872 assert((DK != Enum || TK == TTK_Enum) &&
1873 "EnumDecl not matched with TTK_Enum");
1875 IsDefinition = false;
1876 IsEmbeddedInDeclarator = false;
1877 setPreviousDeclaration(PrevDecl);
1880 typedef Redeclarable<TagDecl> redeclarable_base;
1881 virtual TagDecl *getNextRedeclaration() { return RedeclLink.getNext(); }
1884 void Destroy(ASTContext &C);
1886 typedef redeclarable_base::redecl_iterator redecl_iterator;
1887 redecl_iterator redecls_begin() const {
1888 return redeclarable_base::redecls_begin();
1890 redecl_iterator redecls_end() const {
1891 return redeclarable_base::redecls_end();
1894 SourceLocation getRBraceLoc() const { return RBraceLoc; }
1895 void setRBraceLoc(SourceLocation L) { RBraceLoc = L; }
1897 SourceLocation getTagKeywordLoc() const { return TagKeywordLoc; }
1898 void setTagKeywordLoc(SourceLocation TKL) { TagKeywordLoc = TKL; }
1900 /// getInnerLocStart - Return SourceLocation representing start of source
1901 /// range ignoring outer template declarations.
1902 virtual SourceLocation getInnerLocStart() const { return TagKeywordLoc; }
1904 /// getOuterLocStart - Return SourceLocation representing start of source
1905 /// range taking into account any outer template declarations.
1906 SourceLocation getOuterLocStart() const;
1907 virtual SourceRange getSourceRange() const;
1909 virtual TagDecl* getCanonicalDecl();
1910 const TagDecl* getCanonicalDecl() const {
1911 return const_cast<TagDecl*>(this)->getCanonicalDecl();
1914 /// isDefinition - Return true if this decl has its body specified.
1915 bool isDefinition() const {
1916 return IsDefinition;
1919 bool isEmbeddedInDeclarator() const {
1920 return IsEmbeddedInDeclarator;
1922 void setEmbeddedInDeclarator(bool isInDeclarator) {
1923 IsEmbeddedInDeclarator = isInDeclarator;
1926 /// \brief Whether this declaration declares a type that is
1927 /// dependent, i.e., a type that somehow depends on template
1929 bool isDependentType() const { return isDependentContext(); }
1931 /// @brief Starts the definition of this tag declaration.
1933 /// This method should be invoked at the beginning of the definition
1934 /// of this tag declaration. It will set the tag type into a state
1935 /// where it is in the process of being defined.
1936 void startDefinition();
1938 /// @brief Completes the definition of this tag declaration.
1939 void completeDefinition();
1941 /// getDefinition - Returns the TagDecl that actually defines this
1942 /// struct/union/class/enum. When determining whether or not a
1943 /// struct/union/class/enum is completely defined, one should use this method
1944 /// as opposed to 'isDefinition'. 'isDefinition' indicates whether or not a
1945 /// specific TagDecl is defining declaration, not whether or not the
1946 /// struct/union/class/enum type is defined. This method returns NULL if
1947 /// there is no TagDecl that defines the struct/union/class/enum.
1948 TagDecl* getDefinition() const;
1950 void setDefinition(bool V) { IsDefinition = V; }
1952 const char *getKindName() const {
1953 return TypeWithKeyword::getTagTypeKindName(getTagKind());
1956 TagKind getTagKind() const {
1957 return TagKind(TagDeclKind);
1960 void setTagKind(TagKind TK) { TagDeclKind = TK; }
1962 bool isStruct() const { return getTagKind() == TTK_Struct; }
1963 bool isClass() const { return getTagKind() == TTK_Class; }
1964 bool isUnion() const { return getTagKind() == TTK_Union; }
1965 bool isEnum() const { return getTagKind() == TTK_Enum; }
1967 TypedefDecl *getTypedefForAnonDecl() const {
1968 return hasExtInfo() ? 0 : TypedefDeclOrQualifier.get<TypedefDecl*>();
1971 void setTypedefForAnonDecl(TypedefDecl *TDD);
1973 NestedNameSpecifier *getQualifier() const {
1974 return hasExtInfo() ? getExtInfo()->NNS : 0;
1976 SourceRange getQualifierRange() const {
1977 return hasExtInfo() ? getExtInfo()->NNSRange : SourceRange();
1979 void setQualifierInfo(NestedNameSpecifier *Qualifier,
1980 SourceRange QualifierRange);
1982 unsigned getNumTemplateParameterLists() const {
1983 return hasExtInfo() ? getExtInfo()->NumTemplParamLists : 0;
1985 TemplateParameterList *getTemplateParameterList(unsigned i) const {
1986 assert(i < getNumTemplateParameterLists());
1987 return getExtInfo()->TemplParamLists[i];
1989 void setTemplateParameterListsInfo(ASTContext &Context, unsigned NumTPLists,
1990 TemplateParameterList **TPLists) {
1991 getExtInfo()->setTemplateParameterListsInfo(Context, NumTPLists, TPLists);
1994 // Implement isa/cast/dyncast/etc.
1995 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
1996 static bool classof(const TagDecl *D) { return true; }
1997 static bool classofKind(Kind K) { return K >= firstTag && K <= lastTag; }
1999 static DeclContext *castToDeclContext(const TagDecl *D) {
2000 return static_cast<DeclContext *>(const_cast<TagDecl*>(D));
2002 static TagDecl *castFromDeclContext(const DeclContext *DC) {
2003 return static_cast<TagDecl *>(const_cast<DeclContext*>(DC));
2006 friend class PCHDeclReader;
2007 friend class PCHDeclWriter;
2010 /// EnumDecl - Represents an enum. As an extension, we allow forward-declared
2012 class EnumDecl : public TagDecl {
2013 /// IntegerType - This represent the integer type that the enum corresponds
2014 /// to for code generation purposes. Note that the enumerator constants may
2015 /// have a different type than this does.
2016 QualType IntegerType;
2018 /// PromotionType - The integer type that values of this type should
2019 /// promote to. In C, enumerators are generally of an integer type
2020 /// directly, but gcc-style large enumerators (and all enumerators
2021 /// in C++) are of the enum type instead.
2022 QualType PromotionType;
2024 /// \brief If the enumeration was instantiated from an enumeration
2025 /// within a class or function template, this pointer refers to the
2026 /// enumeration declared within the template.
2027 EnumDecl *InstantiatedFrom;
2029 // The number of positive and negative bits required by the
2030 // enumerators are stored in the SubclassBits field.
2033 NumBitsMask = (1 << NumBitsWidth) - 1
2036 EnumDecl(DeclContext *DC, SourceLocation L,
2037 IdentifierInfo *Id, EnumDecl *PrevDecl, SourceLocation TKL)
2038 : TagDecl(Enum, TTK_Enum, DC, L, Id, PrevDecl, TKL), InstantiatedFrom(0) {
2039 IntegerType = QualType();
2042 EnumDecl *getCanonicalDecl() {
2043 return cast<EnumDecl>(TagDecl::getCanonicalDecl());
2045 const EnumDecl *getCanonicalDecl() const {
2046 return cast<EnumDecl>(TagDecl::getCanonicalDecl());
2049 const EnumDecl *getPreviousDeclaration() const {
2050 return cast_or_null<EnumDecl>(TagDecl::getPreviousDeclaration());
2052 EnumDecl *getPreviousDeclaration() {
2053 return cast_or_null<EnumDecl>(TagDecl::getPreviousDeclaration());
2056 static EnumDecl *Create(ASTContext &C, DeclContext *DC,
2057 SourceLocation L, IdentifierInfo *Id,
2058 SourceLocation TKL, EnumDecl *PrevDecl);
2059 static EnumDecl *Create(ASTContext &C, EmptyShell Empty);
2061 virtual void Destroy(ASTContext& C);
2063 /// completeDefinition - When created, the EnumDecl corresponds to a
2064 /// forward-declared enum. This method is used to mark the
2065 /// declaration as being defined; it's enumerators have already been
2066 /// added (via DeclContext::addDecl). NewType is the new underlying
2067 /// type of the enumeration type.
2068 void completeDefinition(QualType NewType,
2069 QualType PromotionType,
2070 unsigned NumPositiveBits,
2071 unsigned NumNegativeBits);
2073 // enumerator_iterator - Iterates through the enumerators of this
2075 typedef specific_decl_iterator<EnumConstantDecl> enumerator_iterator;
2077 enumerator_iterator enumerator_begin() const {
2078 const EnumDecl *E = cast_or_null<EnumDecl>(getDefinition());
2081 return enumerator_iterator(E->decls_begin());
2084 enumerator_iterator enumerator_end() const {
2085 const EnumDecl *E = cast_or_null<EnumDecl>(getDefinition());
2088 return enumerator_iterator(E->decls_end());
2091 /// getPromotionType - Return the integer type that enumerators
2092 /// should promote to.
2093 QualType getPromotionType() const { return PromotionType; }
2095 /// \brief Set the promotion type.
2096 void setPromotionType(QualType T) { PromotionType = T; }
2098 /// getIntegerType - Return the integer type this enum decl corresponds to.
2099 /// This returns a null qualtype for an enum forward definition.
2100 QualType getIntegerType() const { return IntegerType; }
2102 /// \brief Set the underlying integer type.
2103 void setIntegerType(QualType T) { IntegerType = T; }
2105 /// \brief Returns the width in bits requred to store all the
2106 /// non-negative enumerators of this enum.
2107 unsigned getNumPositiveBits() const {
2108 return NumPositiveBits;
2110 void setNumPositiveBits(unsigned Num) {
2111 NumPositiveBits = Num;
2112 assert(NumPositiveBits == Num && "can't store this bitcount");
2115 /// \brief Returns the width in bits requred to store all the
2116 /// negative enumerators of this enum. These widths include
2117 /// the rightmost leading 1; that is:
2119 /// MOST NEGATIVE ENUMERATOR PATTERN NUM NEGATIVE BITS
2120 /// ------------------------ ------- -----------------
2124 unsigned getNumNegativeBits() const {
2125 return NumNegativeBits;
2127 void setNumNegativeBits(unsigned Num) {
2128 NumNegativeBits = Num;
2131 /// \brief Returns the enumeration (declared within the template)
2132 /// from which this enumeration type was instantiated, or NULL if
2133 /// this enumeration was not instantiated from any template.
2134 EnumDecl *getInstantiatedFromMemberEnum() const {
2135 return InstantiatedFrom;
2138 void setInstantiationOfMemberEnum(EnumDecl *IF) { InstantiatedFrom = IF; }
2140 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
2141 static bool classof(const EnumDecl *D) { return true; }
2142 static bool classofKind(Kind K) { return K == Enum; }
2146 /// RecordDecl - Represents a struct/union/class. For example:
2147 /// struct X; // Forward declaration, no "body".
2148 /// union Y { int A, B; }; // Has body with members A and B (FieldDecls).
2149 /// This decl will be marked invalid if *any* members are invalid.
2151 class RecordDecl : public TagDecl {
2152 // FIXME: This can be packed into the bitfields in Decl.
2153 /// HasFlexibleArrayMember - This is true if this struct ends with a flexible
2154 /// array member (e.g. int X[]) or if this union contains a struct that does.
2155 /// If so, this cannot be contained in arrays or other structs as a member.
2156 bool HasFlexibleArrayMember : 1;
2158 /// AnonymousStructOrUnion - Whether this is the type of an anonymous struct
2160 bool AnonymousStructOrUnion : 1;
2162 /// HasObjectMember - This is true if this struct has at least one member
2163 /// containing an object.
2164 bool HasObjectMember : 1;
2167 RecordDecl(Kind DK, TagKind TK, DeclContext *DC,
2168 SourceLocation L, IdentifierInfo *Id,
2169 RecordDecl *PrevDecl, SourceLocation TKL);
2170 virtual ~RecordDecl();
2173 static RecordDecl *Create(ASTContext &C, TagKind TK, DeclContext *DC,
2174 SourceLocation L, IdentifierInfo *Id,
2175 SourceLocation TKL = SourceLocation(),
2176 RecordDecl* PrevDecl = 0);
2177 static RecordDecl *Create(ASTContext &C, EmptyShell Empty);
2179 const RecordDecl *getPreviousDeclaration() const {
2180 return cast_or_null<RecordDecl>(TagDecl::getPreviousDeclaration());
2182 RecordDecl *getPreviousDeclaration() {
2183 return cast_or_null<RecordDecl>(TagDecl::getPreviousDeclaration());
2186 virtual void Destroy(ASTContext& C);
2188 bool hasFlexibleArrayMember() const { return HasFlexibleArrayMember; }
2189 void setHasFlexibleArrayMember(bool V) { HasFlexibleArrayMember = V; }
2191 /// isAnonymousStructOrUnion - Whether this is an anonymous struct
2192 /// or union. To be an anonymous struct or union, it must have been
2193 /// declared without a name and there must be no objects of this
2194 /// type declared, e.g.,
2196 /// union { int i; float f; };
2198 /// is an anonymous union but neither of the following are:
2200 /// union X { int i; float f; };
2201 /// union { int i; float f; } obj;
2203 bool isAnonymousStructOrUnion() const { return AnonymousStructOrUnion; }
2204 void setAnonymousStructOrUnion(bool Anon) {
2205 AnonymousStructOrUnion = Anon;
2208 ValueDecl *getAnonymousStructOrUnionObject();
2209 const ValueDecl *getAnonymousStructOrUnionObject() const {
2210 return const_cast<RecordDecl*>(this)->getAnonymousStructOrUnionObject();
2213 bool hasObjectMember() const { return HasObjectMember; }
2214 void setHasObjectMember (bool val) { HasObjectMember = val; }
2216 /// \brief Determines whether this declaration represents the
2217 /// injected class name.
2219 /// The injected class name in C++ is the name of the class that
2220 /// appears inside the class itself. For example:
2224 /// // C is implicitly declared here as a synonym for the class name.
2227 /// C::C c; // same as "C c;"
2229 bool isInjectedClassName() const;
2231 /// getDefinition - Returns the RecordDecl that actually defines this
2232 /// struct/union/class. When determining whether or not a struct/union/class
2233 /// is completely defined, one should use this method as opposed to
2234 /// 'isDefinition'. 'isDefinition' indicates whether or not a specific
2235 /// RecordDecl is defining declaration, not whether or not the record
2236 /// type is defined. This method returns NULL if there is no RecordDecl
2237 /// that defines the struct/union/tag.
2238 RecordDecl* getDefinition() const {
2239 return cast_or_null<RecordDecl>(TagDecl::getDefinition());
2242 // Iterator access to field members. The field iterator only visits
2243 // the non-static data members of this class, ignoring any static
2244 // data members, functions, constructors, destructors, etc.
2245 typedef specific_decl_iterator<FieldDecl> field_iterator;
2247 field_iterator field_begin() const {
2248 return field_iterator(decls_begin());
2250 field_iterator field_end() const {
2251 return field_iterator(decls_end());
2254 // field_empty - Whether there are any fields (non-static data
2255 // members) in this record.
2256 bool field_empty() const {
2257 return field_begin() == field_end();
2260 /// completeDefinition - Notes that the definition of this type is
2262 void completeDefinition();
2264 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
2265 static bool classof(const RecordDecl *D) { return true; }
2266 static bool classofKind(Kind K) {
2267 return K >= firstRecord && K <= lastRecord;
2271 class FileScopeAsmDecl : public Decl {
2272 StringLiteral *AsmString;
2273 FileScopeAsmDecl(DeclContext *DC, SourceLocation L, StringLiteral *asmstring)
2274 : Decl(FileScopeAsm, DC, L), AsmString(asmstring) {}
2276 static FileScopeAsmDecl *Create(ASTContext &C, DeclContext *DC,
2277 SourceLocation L, StringLiteral *Str);
2279 const StringLiteral *getAsmString() const { return AsmString; }
2280 StringLiteral *getAsmString() { return AsmString; }
2281 void setAsmString(StringLiteral *Asm) { AsmString = Asm; }
2283 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
2284 static bool classof(const FileScopeAsmDecl *D) { return true; }
2285 static bool classofKind(Kind K) { return K == FileScopeAsm; }
2288 /// BlockDecl - This represents a block literal declaration, which is like an
2289 /// unnamed FunctionDecl. For example:
2290 /// ^{ statement-body } or ^(int arg1, float arg2){ statement-body }
2292 class BlockDecl : public Decl, public DeclContext {
2293 // FIXME: This can be packed into the bitfields in Decl.
2294 bool IsVariadic : 1;
2295 /// ParamInfo - new[]'d array of pointers to ParmVarDecls for the formal
2296 /// parameters of this function. This is null if a prototype or if there are
2298 ParmVarDecl **ParamInfo;
2302 TypeSourceInfo *SignatureAsWritten;
2305 BlockDecl(DeclContext *DC, SourceLocation CaretLoc)
2306 : Decl(Block, DC, CaretLoc), DeclContext(Block),
2307 IsVariadic(false), ParamInfo(0), NumParams(0), Body(0),
2308 SignatureAsWritten(0) {}
2310 virtual ~BlockDecl();
2311 virtual void Destroy(ASTContext& C);
2314 static BlockDecl *Create(ASTContext &C, DeclContext *DC, SourceLocation L);
2316 SourceLocation getCaretLocation() const { return getLocation(); }
2318 bool isVariadic() const { return IsVariadic; }
2319 void setIsVariadic(bool value) { IsVariadic = value; }
2321 CompoundStmt *getCompoundBody() const { return (CompoundStmt*) Body; }
2322 Stmt *getBody() const { return (Stmt*) Body; }
2323 void setBody(CompoundStmt *B) { Body = (Stmt*) B; }
2325 void setSignatureAsWritten(TypeSourceInfo *Sig) { SignatureAsWritten = Sig; }
2326 TypeSourceInfo *getSignatureAsWritten() const { return SignatureAsWritten; }
2328 // Iterator access to formal parameters.
2329 unsigned param_size() const { return getNumParams(); }
2330 typedef ParmVarDecl **param_iterator;
2331 typedef ParmVarDecl * const *param_const_iterator;
2333 bool param_empty() const { return NumParams == 0; }
2334 param_iterator param_begin() { return ParamInfo; }
2335 param_iterator param_end() { return ParamInfo+param_size(); }
2337 param_const_iterator param_begin() const { return ParamInfo; }
2338 param_const_iterator param_end() const { return ParamInfo+param_size(); }
2340 unsigned getNumParams() const;
2341 const ParmVarDecl *getParamDecl(unsigned i) const {
2342 assert(i < getNumParams() && "Illegal param #");
2343 return ParamInfo[i];
2345 ParmVarDecl *getParamDecl(unsigned i) {
2346 assert(i < getNumParams() && "Illegal param #");
2347 return ParamInfo[i];
2349 void setParams(ParmVarDecl **NewParamInfo, unsigned NumParams);
2351 // Implement isa/cast/dyncast/etc.
2352 static bool classof(const Decl *D) { return classofKind(D->getKind()); }
2353 static bool classof(const BlockDecl *D) { return true; }
2354 static bool classofKind(Kind K) { return K == Block; }
2355 static DeclContext *castToDeclContext(const BlockDecl *D) {
2356 return static_cast<DeclContext *>(const_cast<BlockDecl*>(D));
2358 static BlockDecl *castFromDeclContext(const DeclContext *DC) {
2359 return static_cast<BlockDecl *>(const_cast<DeclContext*>(DC));
2363 /// Insertion operator for diagnostics. This allows sending NamedDecl's
2364 /// into a diagnostic with <<.
2365 inline const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB,
2367 DB.AddTaggedVal(reinterpret_cast<intptr_t>(ND), Diagnostic::ak_nameddecl);
2371 } // end namespace clang