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
;
41 /// \brief A container of type source information.
43 /// A client can read the relevant info using TypeLoc wrappers, e.g:
45 /// TypeLoc TL = TypeSourceInfo->getTypeLoc();
46 /// if (PointerLoc *PL = dyn_cast<PointerLoc>(&TL))
47 /// PL->getStarLoc().print(OS, SrcMgr);
50 class TypeSourceInfo
{
52 // Contains a memory block after the class, used for type source information,
53 // allocated by ASTContext.
54 friend class ASTContext
;
55 TypeSourceInfo(QualType ty
) : Ty(ty
) { }
57 /// \brief Return the type wrapped by this type source info.
58 QualType
getType() const { return Ty
; }
60 /// \brief Return the TypeLoc wrapper for the type source info.
61 TypeLoc
getTypeLoc() const; // implemented in TypeLoc.h
64 /// TranslationUnitDecl - The top declaration context.
65 class TranslationUnitDecl
: public Decl
, public DeclContext
{
68 /// The (most recently entered) anonymous namespace for this
69 /// translation unit, if one has been created.
70 NamespaceDecl
*AnonymousNamespace
;
72 explicit TranslationUnitDecl(ASTContext
&ctx
)
73 : Decl(TranslationUnit
, 0, SourceLocation()),
74 DeclContext(TranslationUnit
),
75 Ctx(ctx
), AnonymousNamespace(0) {}
77 ASTContext
&getASTContext() const { return Ctx
; }
79 NamespaceDecl
*getAnonymousNamespace() const { return AnonymousNamespace
; }
80 void setAnonymousNamespace(NamespaceDecl
*D
) { AnonymousNamespace
= D
; }
82 static TranslationUnitDecl
*Create(ASTContext
&C
);
83 // Implement isa/cast/dyncast/etc.
84 static bool classof(const Decl
*D
) { return classofKind(D
->getKind()); }
85 static bool classof(const TranslationUnitDecl
*D
) { return true; }
86 static bool classofKind(Kind K
) { return K
== TranslationUnit
; }
87 static DeclContext
*castToDeclContext(const TranslationUnitDecl
*D
) {
88 return static_cast<DeclContext
*>(const_cast<TranslationUnitDecl
*>(D
));
90 static TranslationUnitDecl
*castFromDeclContext(const DeclContext
*DC
) {
91 return static_cast<TranslationUnitDecl
*>(const_cast<DeclContext
*>(DC
));
95 /// NamedDecl - This represents a decl with a name. Many decls have names such
96 /// as ObjCMethodDecl, but not @class, etc.
97 class NamedDecl
: public Decl
{
98 /// Name - The name of this declaration, which is typically a normal
99 /// identifier but may also be a special kind of name (C++
100 /// constructor, Objective-C selector, etc.)
101 DeclarationName Name
;
104 NamedDecl(Kind DK
, DeclContext
*DC
, SourceLocation L
, DeclarationName N
)
105 : Decl(DK
, DC
, L
), Name(N
) { }
108 /// getIdentifier - Get the identifier that names this declaration,
109 /// if there is one. This will return NULL if this declaration has
110 /// no name (e.g., for an unnamed class) or if the name is a special
111 /// name (C++ constructor, Objective-C selector, etc.).
112 IdentifierInfo
*getIdentifier() const { return Name
.getAsIdentifierInfo(); }
114 /// getName - Get the name of identifier for this declaration as a StringRef.
115 /// This requires that the declaration have a name and that it be a simple
117 llvm::StringRef
getName() const {
118 assert(Name
.isIdentifier() && "Name is not a simple identifier");
119 return getIdentifier() ? getIdentifier()->getName() : "";
122 /// getNameAsString - Get a human-readable name for the declaration, even if
123 /// it is one of the special kinds of names (C++ constructor, Objective-C
124 /// selector, etc). Creating this name requires expensive string
125 /// manipulation, so it should be called only when performance doesn't matter.
126 /// For simple declarations, getNameAsCString() should suffice.
128 // FIXME: This function should be renamed to indicate that it is not just an
129 // alternate form of getName(), and clients should move as appropriate.
131 // FIXME: Deprecated, move clients to getName().
132 std::string
getNameAsString() const { return Name
.getAsString(); }
134 void printName(llvm::raw_ostream
&os
) const { return Name
.printName(os
); }
136 /// getDeclName - Get the actual, stored name of the declaration,
137 /// which may be a special name.
138 DeclarationName
getDeclName() const { return Name
; }
140 /// \brief Set the name of this declaration.
141 void setDeclName(DeclarationName N
) { Name
= N
; }
143 /// getQualifiedNameAsString - Returns human-readable qualified name for
144 /// declaration, like A::B::i, for i being member of namespace A::B.
145 /// If declaration is not member of context which can be named (record,
146 /// namespace), it will return same result as getNameAsString().
147 /// Creating this name is expensive, so it should be called only when
148 /// performance doesn't matter.
149 std::string
getQualifiedNameAsString() const;
150 std::string
getQualifiedNameAsString(const PrintingPolicy
&Policy
) const;
152 /// getNameForDiagnostic - Appends a human-readable name for this
153 /// declaration into the given string.
155 /// This is the method invoked by Sema when displaying a NamedDecl
156 /// in a diagnostic. It does not necessarily produce the same
157 /// result as getNameAsString(); for example, class template
158 /// specializations are printed with their template arguments.
160 /// TODO: use an API that doesn't require so many temporary strings
161 void getNameForDiagnostic(std::string
&S
,
162 const PrintingPolicy
&Policy
,
163 bool Qualified
) const;
165 /// declarationReplaces - Determine whether this declaration, if
166 /// known to be well-formed within its context, will replace the
167 /// declaration OldD if introduced into scope. A declaration will
168 /// replace another declaration if, for example, it is a
169 /// redeclaration of the same variable or function, but not if it is
170 /// a declaration of a different kind (function vs. class) or an
171 /// overloaded function.
172 bool declarationReplaces(NamedDecl
*OldD
) const;
174 /// \brief Determine whether this declaration has linkage.
175 bool hasLinkage() const;
177 /// \brief Determine whether this declaration is a C++ class member.
178 bool isCXXClassMember() const {
179 const DeclContext
*DC
= getDeclContext();
181 // C++0x [class.mem]p1:
182 // The enumerators of an unscoped enumeration defined in
183 // the class are members of the class.
184 // FIXME: support C++0x scoped enumerations.
185 if (isa
<EnumDecl
>(DC
))
186 DC
= DC
->getParent();
188 return DC
->isRecord();
191 /// \brief Given that this declaration is a C++ class member,
192 /// determine whether it's an instance member of its class.
193 bool isCXXInstanceMember() const;
197 Visibility visibility_
;
201 LinkageInfo() : linkage_(ExternalLinkage
), visibility_(DefaultVisibility
),
203 LinkageInfo(Linkage L
, Visibility V
, bool E
)
204 : linkage_(L
), visibility_(V
), explicit_(E
) {}
206 static LinkageInfo
external() {
207 return LinkageInfo();
209 static LinkageInfo
internal() {
210 return LinkageInfo(InternalLinkage
, DefaultVisibility
, false);
212 static LinkageInfo
uniqueExternal() {
213 return LinkageInfo(UniqueExternalLinkage
, DefaultVisibility
, false);
215 static LinkageInfo
none() {
216 return LinkageInfo(NoLinkage
, DefaultVisibility
, false);
219 Linkage
linkage() const { return linkage_
; }
220 Visibility
visibility() const { return visibility_
; }
221 bool visibilityExplicit() const { return explicit_
; }
223 void setLinkage(Linkage L
) { linkage_
= L
; }
224 void setVisibility(Visibility V
) { visibility_
= V
; }
225 void setVisibility(Visibility V
, bool E
) { visibility_
= V
; explicit_
= E
; }
226 void setVisibility(LinkageInfo Other
) {
227 setVisibility(Other
.visibility(), Other
.visibilityExplicit());
230 void mergeLinkage(Linkage L
) {
231 setLinkage(minLinkage(linkage(), L
));
233 void mergeLinkage(LinkageInfo Other
) {
234 setLinkage(minLinkage(linkage(), Other
.linkage()));
237 void mergeVisibility(Visibility V
) {
238 setVisibility(minVisibility(visibility(), V
));
240 void mergeVisibility(Visibility V
, bool E
) {
241 setVisibility(minVisibility(visibility(), V
), visibilityExplicit() || E
);
243 void mergeVisibility(LinkageInfo Other
) {
244 mergeVisibility(Other
.visibility(), Other
.visibilityExplicit());
247 void merge(LinkageInfo Other
) {
249 mergeVisibility(Other
);
251 void merge(std::pair
<Linkage
,Visibility
> LV
) {
252 mergeLinkage(LV
.first
);
253 mergeVisibility(LV
.second
);
256 friend LinkageInfo
merge(LinkageInfo L
, LinkageInfo R
) {
262 /// \brief Determine what kind of linkage this entity has.
263 Linkage
getLinkage() const;
265 /// \brief Determines the visibility of this entity.
266 Visibility
getVisibility() const { return getLinkageAndVisibility().visibility(); }
268 /// \brief Determines the linkage and visibility of this entity.
269 LinkageInfo
getLinkageAndVisibility() const;
271 /// \brief Clear the linkage cache in response to a change
272 /// to the declaration.
273 void ClearLinkageCache();
275 /// \brief Looks through UsingDecls and ObjCCompatibleAliasDecls for
276 /// the underlying named decl.
277 NamedDecl
*getUnderlyingDecl();
278 const NamedDecl
*getUnderlyingDecl() const {
279 return const_cast<NamedDecl
*>(this)->getUnderlyingDecl();
282 static bool classof(const Decl
*D
) { return classofKind(D
->getKind()); }
283 static bool classof(const NamedDecl
*D
) { return true; }
284 static bool classofKind(Kind K
) { return K
>= firstNamed
&& K
<= lastNamed
; }
287 inline llvm::raw_ostream
&operator<<(llvm::raw_ostream
&OS
,
288 const NamedDecl
*ND
) {
289 ND
->getDeclName().printName(OS
);
293 /// LabelDecl - Represents the declaration of a label. Labels also have a
294 /// corresponding LabelStmt, which indicates the position that the label was
295 /// defined at. For normal labels, the location of the decl is the same as the
296 /// location of the statement. For GNU local labels (__label__), the decl
297 /// location is where the __label__ is.
298 class LabelDecl
: public NamedDecl
{
299 /// HasUnusedAttr - True if the label has __attribute__((unused)) on it.
300 /// FIXME: Just use attributes!
301 unsigned HasUnusedAttr
: 1;
304 LabelDecl(DeclContext
*DC
, SourceLocation L
, IdentifierInfo
*II
, LabelStmt
*S
)
305 : NamedDecl(Label
, DC
, L
, II
), TheStmt(S
) {}
308 static LabelDecl
*Create(ASTContext
&C
, DeclContext
*DC
,
309 SourceLocation L
, IdentifierInfo
*II
);
311 LabelStmt
*getStmt() const { return TheStmt
; }
312 void setStmt(LabelStmt
*T
) { TheStmt
= T
; }
314 bool hasUnusedAttribute() const { return HasUnusedAttr
; }
315 void setHasUnusedAttribute() { HasUnusedAttr
= true; }
317 // Implement isa/cast/dyncast/etc.
318 static bool classof(const Decl
*D
) { return classofKind(D
->getKind()); }
319 static bool classof(const LabelDecl
*D
) { return true; }
320 static bool classofKind(Kind K
) { return K
== Label
; }
323 /// NamespaceDecl - Represent a C++ namespace.
324 class NamespaceDecl
: public NamedDecl
, public DeclContext
{
327 SourceLocation LBracLoc
, RBracLoc
;
329 // For extended namespace definitions:
331 // namespace A { int x; }
332 // namespace A { int y; }
334 // there will be one NamespaceDecl for each declaration.
335 // NextNamespace points to the next extended declaration.
336 // OrigNamespace points to the original namespace declaration.
337 // OrigNamespace of the first namespace decl points to its anonymous namespace
338 LazyDeclPtr NextNamespace
;
340 /// \brief A pointer to either the original namespace definition for
341 /// this namespace (if the boolean value is false) or the anonymous
342 /// namespace that lives just inside this namespace (if the boolean
345 /// We can combine these two notions because the anonymous namespace
346 /// must only be stored in one of the namespace declarations (so all
347 /// of the namespace declarations can find it). We therefore choose
348 /// the original namespace declaration, since all of the namespace
349 /// declarations have a link directly to it; the original namespace
350 /// declaration itself only needs to know that it is the original
351 /// namespace declaration (which the boolean indicates).
352 llvm::PointerIntPair
<NamespaceDecl
*, 1, bool> OrigOrAnonNamespace
;
354 NamespaceDecl(DeclContext
*DC
, SourceLocation L
, IdentifierInfo
*Id
)
355 : NamedDecl(Namespace
, DC
, L
, Id
), DeclContext(Namespace
),
356 IsInline(false), NextNamespace(), OrigOrAnonNamespace(0, true) { }
359 static NamespaceDecl
*Create(ASTContext
&C
, DeclContext
*DC
,
360 SourceLocation L
, IdentifierInfo
*Id
);
362 /// \brief Returns true if this is an anonymous namespace declaration.
370 /// q.v. C++ [namespace.unnamed]
371 bool isAnonymousNamespace() const {
372 return !getIdentifier();
375 /// \brief Returns true if this is an inline namespace declaration.
376 bool isInline() const {
380 /// \brief Set whether this is an inline namespace declaration.
381 void setInline(bool Inline
) {
385 /// \brief Return the next extended namespace declaration or null if there
387 NamespaceDecl
*getNextNamespace();
388 const NamespaceDecl
*getNextNamespace() const {
389 return const_cast<NamespaceDecl
*>(this)->getNextNamespace();
392 /// \brief Set the next extended namespace declaration.
393 void setNextNamespace(NamespaceDecl
*ND
) { NextNamespace
= ND
; }
395 /// \brief Get the original (first) namespace declaration.
396 NamespaceDecl
*getOriginalNamespace() const {
397 if (OrigOrAnonNamespace
.getInt())
398 return const_cast<NamespaceDecl
*>(this);
400 return OrigOrAnonNamespace
.getPointer();
403 /// \brief Return true if this declaration is an original (first) declaration
404 /// of the namespace. This is false for non-original (subsequent) namespace
405 /// declarations and anonymous namespaces.
406 bool isOriginalNamespace() const {
407 return getOriginalNamespace() == this;
410 /// \brief Set the original (first) namespace declaration.
411 void setOriginalNamespace(NamespaceDecl
*ND
) {
413 OrigOrAnonNamespace
.setPointer(ND
);
414 OrigOrAnonNamespace
.setInt(false);
418 NamespaceDecl
*getAnonymousNamespace() const {
419 return getOriginalNamespace()->OrigOrAnonNamespace
.getPointer();
422 void setAnonymousNamespace(NamespaceDecl
*D
) {
423 assert(!D
|| D
->isAnonymousNamespace());
424 assert(!D
|| D
->getParent() == this);
425 getOriginalNamespace()->OrigOrAnonNamespace
.setPointer(D
);
428 NamespaceDecl
*getCanonicalDecl() { return getOriginalNamespace(); }
429 const NamespaceDecl
*getCanonicalDecl() const {
430 return getOriginalNamespace();
433 SourceRange
getSourceRange() const {
434 return SourceRange(getLocation(), RBracLoc
);
437 SourceLocation
getLBracLoc() const { return LBracLoc
; }
438 SourceLocation
getRBracLoc() const { return RBracLoc
; }
439 void setLBracLoc(SourceLocation L
) { LBracLoc
= L
; }
440 void setRBracLoc(SourceLocation R
) { RBracLoc
= R
; }
442 // Implement isa/cast/dyncast/etc.
443 static bool classof(const Decl
*D
) { return classofKind(D
->getKind()); }
444 static bool classof(const NamespaceDecl
*D
) { return true; }
445 static bool classofKind(Kind K
) { return K
== Namespace
; }
446 static DeclContext
*castToDeclContext(const NamespaceDecl
*D
) {
447 return static_cast<DeclContext
*>(const_cast<NamespaceDecl
*>(D
));
449 static NamespaceDecl
*castFromDeclContext(const DeclContext
*DC
) {
450 return static_cast<NamespaceDecl
*>(const_cast<DeclContext
*>(DC
));
453 friend class ASTDeclReader
;
454 friend class ASTDeclWriter
;
457 /// ValueDecl - Represent the declaration of a variable (in which case it is
458 /// an lvalue) a function (in which case it is a function designator) or
459 /// an enum constant.
460 class ValueDecl
: public NamedDecl
{
464 ValueDecl(Kind DK
, DeclContext
*DC
, SourceLocation L
,
465 DeclarationName N
, QualType T
)
466 : NamedDecl(DK
, DC
, L
, N
), DeclType(T
) {}
468 QualType
getType() const { return DeclType
; }
469 void setType(QualType newType
) { DeclType
= newType
; }
471 // Implement isa/cast/dyncast/etc.
472 static bool classof(const Decl
*D
) { return classofKind(D
->getKind()); }
473 static bool classof(const ValueDecl
*D
) { return true; }
474 static bool classofKind(Kind K
) { return K
>= firstValue
&& K
<= lastValue
; }
477 /// QualifierInfo - A struct with extended info about a syntactic
478 /// name qualifier, to be used for the case of out-of-line declarations.
479 struct QualifierInfo
{
480 /// NNS - The syntactic name qualifier.
481 NestedNameSpecifier
*NNS
;
482 /// NNSRange - The source range for the qualifier.
483 SourceRange NNSRange
;
484 /// NumTemplParamLists - The number of template parameter lists
485 /// that were matched against the template-ids occurring into the NNS.
486 unsigned NumTemplParamLists
;
487 /// TemplParamLists - A new-allocated array of size NumTemplParamLists,
488 /// containing pointers to the matched template parameter lists.
489 TemplateParameterList
** TemplParamLists
;
491 /// Default constructor.
493 : NNS(0), NNSRange(), NumTemplParamLists(0), TemplParamLists(0) {}
494 /// setTemplateParameterListsInfo - Sets info about matched template
496 void setTemplateParameterListsInfo(ASTContext
&Context
,
498 TemplateParameterList
**TPLists
);
501 // Copy constructor and copy assignment are disabled.
502 QualifierInfo(const QualifierInfo
&);
503 QualifierInfo
& operator=(const QualifierInfo
&);
506 /// \brief Represents a ValueDecl that came out of a declarator.
507 /// Contains type source information through TypeSourceInfo.
508 class DeclaratorDecl
: public ValueDecl
{
509 // A struct representing both a TInfo and a syntactic qualifier,
510 // to be used for the (uncommon) case of out-of-line declarations.
511 struct ExtInfo
: public QualifierInfo
{
512 TypeSourceInfo
*TInfo
;
515 llvm::PointerUnion
<TypeSourceInfo
*, ExtInfo
*> DeclInfo
;
517 bool hasExtInfo() const { return DeclInfo
.is
<ExtInfo
*>(); }
518 ExtInfo
*getExtInfo() { return DeclInfo
.get
<ExtInfo
*>(); }
519 const ExtInfo
*getExtInfo() const { return DeclInfo
.get
<ExtInfo
*>(); }
522 DeclaratorDecl(Kind DK
, DeclContext
*DC
, SourceLocation L
,
523 DeclarationName N
, QualType T
, TypeSourceInfo
*TInfo
)
524 : ValueDecl(DK
, DC
, L
, N
, T
), DeclInfo(TInfo
) {}
527 TypeSourceInfo
*getTypeSourceInfo() const {
529 ? getExtInfo()->TInfo
530 : DeclInfo
.get
<TypeSourceInfo
*>();
532 void setTypeSourceInfo(TypeSourceInfo
*TI
) {
534 getExtInfo()->TInfo
= TI
;
539 /// getInnerLocStart - Return SourceLocation representing start of source
540 /// range ignoring outer template declarations.
541 SourceLocation
getInnerLocStart() const;
543 /// getOuterLocStart - Return SourceLocation representing start of source
544 /// range taking into account any outer template declarations.
545 SourceLocation
getOuterLocStart() const;
546 SourceRange
getSourceRange() const {
547 return SourceRange(getOuterLocStart(), getLocation());
550 NestedNameSpecifier
*getQualifier() const {
551 return hasExtInfo() ? getExtInfo()->NNS
: 0;
553 SourceRange
getQualifierRange() const {
554 return hasExtInfo() ? getExtInfo()->NNSRange
: SourceRange();
556 void setQualifierInfo(NestedNameSpecifier
*Qualifier
,
557 SourceRange QualifierRange
);
559 unsigned getNumTemplateParameterLists() const {
560 return hasExtInfo() ? getExtInfo()->NumTemplParamLists
: 0;
562 TemplateParameterList
*getTemplateParameterList(unsigned index
) const {
563 assert(index
< getNumTemplateParameterLists());
564 return getExtInfo()->TemplParamLists
[index
];
566 void setTemplateParameterListsInfo(ASTContext
&Context
, unsigned NumTPLists
,
567 TemplateParameterList
**TPLists
) {
568 getExtInfo()->setTemplateParameterListsInfo(Context
, NumTPLists
, TPLists
);
571 SourceLocation
getTypeSpecStartLoc() const;
573 // Implement isa/cast/dyncast/etc.
574 static bool classof(const Decl
*D
) { return classofKind(D
->getKind()); }
575 static bool classof(const DeclaratorDecl
*D
) { return true; }
576 static bool classofKind(Kind K
) {
577 return K
>= firstDeclarator
&& K
<= lastDeclarator
;
580 friend class ASTDeclReader
;
581 friend class ASTDeclWriter
;
584 /// \brief Structure used to store a statement, the constant value to
585 /// which it was evaluated (if any), and whether or not the statement
586 /// is an integral constant expression (if known).
587 struct EvaluatedStmt
{
588 EvaluatedStmt() : WasEvaluated(false), IsEvaluating(false), CheckedICE(false),
589 CheckingICE(false), IsICE(false) { }
591 /// \brief Whether this statement was already evaluated.
592 bool WasEvaluated
: 1;
594 /// \brief Whether this statement is being evaluated.
595 bool IsEvaluating
: 1;
597 /// \brief Whether we already checked whether this statement was an
598 /// integral constant expression.
601 /// \brief Whether we are checking whether this statement is an
602 /// integral constant expression.
603 bool CheckingICE
: 1;
605 /// \brief Whether this statement is an integral constant
606 /// expression. Only valid if CheckedICE is true.
613 /// VarDecl - An instance of this class is created to represent a variable
614 /// declaration or definition.
615 class VarDecl
: public DeclaratorDecl
, public Redeclarable
<VarDecl
> {
617 typedef clang::StorageClass StorageClass
;
619 /// getStorageClassSpecifierString - Return the string used to
620 /// specify the storage class \arg SC.
622 /// It is illegal to call this function with SC == None.
623 static const char *getStorageClassSpecifierString(StorageClass SC
);
626 /// \brief Placeholder type used in Init to denote an unparsed C++ default
628 struct UnparsedDefaultArgument
;
630 /// \brief Placeholder type used in Init to denote an uninstantiated C++
631 /// default argument.
632 struct UninstantiatedDefaultArgument
;
634 typedef llvm::PointerUnion4
<Stmt
*, EvaluatedStmt
*,
635 UnparsedDefaultArgument
*,
636 UninstantiatedDefaultArgument
*> InitType
;
638 /// \brief The initializer for this variable or, for a ParmVarDecl, the
639 /// C++ default argument.
640 mutable InitType Init
;
643 // FIXME: This can be packed into the bitfields in Decl.
645 unsigned SClassAsWritten
: 3;
646 bool ThreadSpecified
: 1;
647 bool HasCXXDirectInit
: 1;
649 /// \brief Whether this variable is the exception variable in a C++ catch
650 /// or an Objective-C @catch statement.
651 bool ExceptionVar
: 1;
653 /// \brief Whether this local variable could be allocated in the return
654 /// slot of its function, enabling the named return value optimization (NRVO).
655 bool NRVOVariable
: 1;
657 friend class StmtIteratorBase
;
658 friend class ASTDeclReader
;
661 VarDecl(Kind DK
, DeclContext
*DC
, SourceLocation L
, IdentifierInfo
*Id
,
662 QualType T
, TypeSourceInfo
*TInfo
, StorageClass SC
,
663 StorageClass SCAsWritten
)
664 : DeclaratorDecl(DK
, DC
, L
, Id
, T
, TInfo
), Init(),
665 ThreadSpecified(false), HasCXXDirectInit(false),
666 ExceptionVar(false), NRVOVariable(false) {
668 SClassAsWritten
= SCAsWritten
;
671 typedef Redeclarable
<VarDecl
> redeclarable_base
;
672 VarDecl
*getNextRedeclaration() { return RedeclLink
.getNext(); }
676 typedef redeclarable_base::redecl_iterator redecl_iterator
;
677 redecl_iterator
redecls_begin() const {
678 return redeclarable_base::redecls_begin();
680 redecl_iterator
redecls_end() const {
681 return redeclarable_base::redecls_end();
684 static VarDecl
*Create(ASTContext
&C
, DeclContext
*DC
,
685 SourceLocation L
, IdentifierInfo
*Id
,
686 QualType T
, TypeSourceInfo
*TInfo
, StorageClass S
,
687 StorageClass SCAsWritten
);
689 SourceRange
getSourceRange() const;
691 StorageClass
getStorageClass() const { return (StorageClass
)SClass
; }
692 StorageClass
getStorageClassAsWritten() const {
693 return (StorageClass
) SClassAsWritten
;
695 void setStorageClass(StorageClass SC
);
696 void setStorageClassAsWritten(StorageClass SC
) {
697 assert(isLegalForVariable(SC
));
698 SClassAsWritten
= SC
;
701 void setThreadSpecified(bool T
) { ThreadSpecified
= T
; }
702 bool isThreadSpecified() const {
703 return ThreadSpecified
;
706 /// hasLocalStorage - Returns true if a variable with function scope
707 /// is a non-static local variable.
708 bool hasLocalStorage() const {
709 if (getStorageClass() == SC_None
)
710 return !isFileVarDecl();
712 // Return true for: Auto, Register.
713 // Return false for: Extern, Static, PrivateExtern.
715 return getStorageClass() >= SC_Auto
;
718 /// isStaticLocal - Returns true if a variable with function scope is a
719 /// static local variable.
720 bool isStaticLocal() const {
721 return getStorageClass() == SC_Static
&& !isFileVarDecl();
724 /// hasExternStorage - Returns true if a variable has extern or
725 /// __private_extern__ storage.
726 bool hasExternalStorage() const {
727 return getStorageClass() == SC_Extern
||
728 getStorageClass() == SC_PrivateExtern
;
731 /// hasGlobalStorage - Returns true for all variables that do not
732 /// have local storage. This includs all global variables as well
733 /// as static variables declared within a function.
734 bool hasGlobalStorage() const { return !hasLocalStorage(); }
736 /// \brief Determines whether this variable is a variable with
737 /// external, C linkage.
738 bool isExternC() const;
740 /// isLocalVarDecl - Returns true for local variable declarations
741 /// other than parameters. Note that this includes static variables
742 /// inside of functions. It also includes variables inside blocks.
744 /// void foo() { int x; static int y; extern int z; }
746 bool isLocalVarDecl() const {
747 if (getKind() != Decl::Var
)
749 if (const DeclContext
*DC
= getDeclContext())
750 return DC
->getRedeclContext()->isFunctionOrMethod();
754 /// isFunctionOrMethodVarDecl - Similar to isLocalVarDecl, but
755 /// excludes variables declared in blocks.
756 bool isFunctionOrMethodVarDecl() const {
757 if (getKind() != Decl::Var
)
759 const DeclContext
*DC
= getDeclContext()->getRedeclContext();
760 return DC
->isFunctionOrMethod() && DC
->getDeclKind() != Decl::Block
;
763 /// \brief Determines whether this is a static data member.
765 /// This will only be true in C++, and applies to, e.g., the
772 bool isStaticDataMember() const {
773 // If it wasn't static, it would be a FieldDecl.
774 return getKind() != Decl::ParmVar
&& getDeclContext()->isRecord();
777 VarDecl
*getCanonicalDecl();
778 const VarDecl
*getCanonicalDecl() const {
779 return const_cast<VarDecl
*>(this)->getCanonicalDecl();
782 enum DefinitionKind
{
783 DeclarationOnly
, ///< This declaration is only a declaration.
784 TentativeDefinition
, ///< This declaration is a tentative definition.
785 Definition
///< This declaration is definitely a definition.
788 /// \brief Check whether this declaration is a definition. If this could be
789 /// a tentative definition (in C), don't check whether there's an overriding
791 DefinitionKind
isThisDeclarationADefinition() const;
793 /// \brief Check whether this variable is defined in this
794 /// translation unit.
795 DefinitionKind
hasDefinition() const;
797 /// \brief Get the tentative definition that acts as the real definition in
798 /// a TU. Returns null if there is a proper definition available.
799 VarDecl
*getActingDefinition();
800 const VarDecl
*getActingDefinition() const {
801 return const_cast<VarDecl
*>(this)->getActingDefinition();
804 /// \brief Determine whether this is a tentative definition of a
806 bool isTentativeDefinitionNow() const;
808 /// \brief Get the real (not just tentative) definition for this declaration.
809 VarDecl
*getDefinition();
810 const VarDecl
*getDefinition() const {
811 return const_cast<VarDecl
*>(this)->getDefinition();
814 /// \brief Determine whether this is or was instantiated from an out-of-line
815 /// definition of a static data member.
816 bool isOutOfLine() const;
818 /// \brief If this is a static data member, find its out-of-line definition.
819 VarDecl
*getOutOfLineDefinition();
821 /// isFileVarDecl - Returns true for file scoped variable declaration.
822 bool isFileVarDecl() const {
823 if (getKind() != Decl::Var
)
826 if (getDeclContext()->getRedeclContext()->isFileContext())
829 if (isStaticDataMember())
835 /// getAnyInitializer - Get the initializer for this variable, no matter which
836 /// declaration it is attached to.
837 const Expr
*getAnyInitializer() const {
839 return getAnyInitializer(D
);
842 /// getAnyInitializer - Get the initializer for this variable, no matter which
843 /// declaration it is attached to. Also get that declaration.
844 const Expr
*getAnyInitializer(const VarDecl
*&D
) const;
846 bool hasInit() const {
847 return !Init
.isNull() && (Init
.is
<Stmt
*>() || Init
.is
<EvaluatedStmt
*>());
849 const Expr
*getInit() const {
853 const Stmt
*S
= Init
.dyn_cast
<Stmt
*>();
855 if (EvaluatedStmt
*ES
= Init
.dyn_cast
<EvaluatedStmt
*>())
858 return (const Expr
*) S
;
864 Stmt
*S
= Init
.dyn_cast
<Stmt
*>();
866 if (EvaluatedStmt
*ES
= Init
.dyn_cast
<EvaluatedStmt
*>())
873 /// \brief Retrieve the address of the initializer expression.
874 Stmt
**getInitAddress() {
875 if (EvaluatedStmt
*ES
= Init
.dyn_cast
<EvaluatedStmt
*>())
878 // This union hack tip-toes around strict-aliasing rules.
888 void setInit(Expr
*I
);
890 EvaluatedStmt
*EnsureEvaluatedStmt() const {
891 EvaluatedStmt
*Eval
= Init
.dyn_cast
<EvaluatedStmt
*>();
893 Stmt
*S
= Init
.get
<Stmt
*>();
894 Eval
= new (getASTContext()) EvaluatedStmt
;
901 /// \brief Check whether we are in the process of checking whether the
902 /// initializer can be evaluated.
903 bool isEvaluatingValue() const {
904 if (EvaluatedStmt
*Eval
= Init
.dyn_cast
<EvaluatedStmt
*>())
905 return Eval
->IsEvaluating
;
910 /// \brief Note that we now are checking whether the initializer can be
912 void setEvaluatingValue() const {
913 EvaluatedStmt
*Eval
= EnsureEvaluatedStmt();
914 Eval
->IsEvaluating
= true;
917 /// \brief Note that constant evaluation has computed the given
918 /// value for this variable's initializer.
919 void setEvaluatedValue(const APValue
&Value
) const {
920 EvaluatedStmt
*Eval
= EnsureEvaluatedStmt();
921 Eval
->IsEvaluating
= false;
922 Eval
->WasEvaluated
= true;
923 Eval
->Evaluated
= Value
;
926 /// \brief Return the already-evaluated value of this variable's
927 /// initializer, or NULL if the value is not yet known. Returns pointer
928 /// to untyped APValue if the value could not be evaluated.
929 APValue
*getEvaluatedValue() const {
930 if (EvaluatedStmt
*Eval
= Init
.dyn_cast
<EvaluatedStmt
*>())
931 if (Eval
->WasEvaluated
)
932 return &Eval
->Evaluated
;
937 /// \brief Determines whether it is already known whether the
938 /// initializer is an integral constant expression or not.
939 bool isInitKnownICE() const {
940 if (EvaluatedStmt
*Eval
= Init
.dyn_cast
<EvaluatedStmt
*>())
941 return Eval
->CheckedICE
;
946 /// \brief Determines whether the initializer is an integral
947 /// constant expression.
949 /// \pre isInitKnownICE()
950 bool isInitICE() const {
951 assert(isInitKnownICE() &&
952 "Check whether we already know that the initializer is an ICE");
953 return Init
.get
<EvaluatedStmt
*>()->IsICE
;
956 /// \brief Check whether we are in the process of checking the initializer
957 /// is an integral constant expression.
958 bool isCheckingICE() const {
959 if (EvaluatedStmt
*Eval
= Init
.dyn_cast
<EvaluatedStmt
*>())
960 return Eval
->CheckingICE
;
965 /// \brief Note that we now are checking whether the initializer is an
966 /// integral constant expression.
967 void setCheckingICE() const {
968 EvaluatedStmt
*Eval
= EnsureEvaluatedStmt();
969 Eval
->CheckingICE
= true;
972 /// \brief Note that we now know whether the initializer is an
973 /// integral constant expression.
974 void setInitKnownICE(bool IsICE
) const {
975 EvaluatedStmt
*Eval
= EnsureEvaluatedStmt();
976 Eval
->CheckingICE
= false;
977 Eval
->CheckedICE
= true;
981 void setCXXDirectInitializer(bool T
) { HasCXXDirectInit
= T
; }
983 /// hasCXXDirectInitializer - If true, the initializer was a direct
984 /// initializer, e.g: "int x(1);". The Init expression will be the expression
985 /// inside the parens or a "ClassType(a,b,c)" class constructor expression for
986 /// class types. Clients can distinguish between "int x(1);" and "int x=1;"
987 /// by checking hasCXXDirectInitializer.
989 bool hasCXXDirectInitializer() const {
990 return HasCXXDirectInit
;
993 /// \brief Determine whether this variable is the exception variable in a
994 /// C++ catch statememt or an Objective-C @catch statement.
995 bool isExceptionVariable() const {
998 void setExceptionVariable(bool EV
) { ExceptionVar
= EV
; }
1000 /// \brief Determine whether this local variable can be used with the named
1001 /// return value optimization (NRVO).
1003 /// The named return value optimization (NRVO) works by marking certain
1004 /// non-volatile local variables of class type as NRVO objects. These
1005 /// locals can be allocated within the return slot of their containing
1006 /// function, in which case there is no need to copy the object to the
1007 /// return slot when returning from the function. Within the function body,
1008 /// each return that returns the NRVO object will have this variable as its
1010 bool isNRVOVariable() const { return NRVOVariable
; }
1011 void setNRVOVariable(bool NRVO
) { NRVOVariable
= NRVO
; }
1013 /// \brief If this variable is an instantiated static data member of a
1014 /// class template specialization, returns the templated static data member
1015 /// from which it was instantiated.
1016 VarDecl
*getInstantiatedFromStaticDataMember() const;
1018 /// \brief If this variable is a static data member, determine what kind of
1019 /// template specialization or instantiation this is.
1020 TemplateSpecializationKind
getTemplateSpecializationKind() const;
1022 /// \brief If this variable is an instantiation of a static data member of a
1023 /// class template specialization, retrieves the member specialization
1025 MemberSpecializationInfo
*getMemberSpecializationInfo() const;
1027 /// \brief For a static data member that was instantiated from a static
1028 /// data member of a class template, set the template specialiation kind.
1029 void setTemplateSpecializationKind(TemplateSpecializationKind TSK
,
1030 SourceLocation PointOfInstantiation
= SourceLocation());
1032 // Implement isa/cast/dyncast/etc.
1033 static bool classof(const Decl
*D
) { return classofKind(D
->getKind()); }
1034 static bool classof(const VarDecl
*D
) { return true; }
1035 static bool classofKind(Kind K
) { return K
>= firstVar
&& K
<= lastVar
; }
1038 class ImplicitParamDecl
: public VarDecl
{
1040 ImplicitParamDecl(Kind DK
, DeclContext
*DC
, SourceLocation L
,
1041 IdentifierInfo
*Id
, QualType Tw
)
1042 : VarDecl(DK
, DC
, L
, Id
, Tw
, /*TInfo=*/0, SC_None
, SC_None
) {
1046 static ImplicitParamDecl
*Create(ASTContext
&C
, DeclContext
*DC
,
1047 SourceLocation L
, IdentifierInfo
*Id
,
1049 // Implement isa/cast/dyncast/etc.
1050 static bool classof(const ImplicitParamDecl
*D
) { return true; }
1051 static bool classof(const Decl
*D
) { return classofKind(D
->getKind()); }
1052 static bool classofKind(Kind K
) { return K
== ImplicitParam
; }
1055 /// ParmVarDecl - Represent a parameter to a function.
1056 class ParmVarDecl
: public VarDecl
{
1057 // NOTE: VC++ treats enums as signed, avoid using the ObjCDeclQualifier enum
1058 /// FIXME: Also can be paced into the bitfields in Decl.
1060 unsigned objcDeclQualifier
: 6;
1061 bool HasInheritedDefaultArg
: 1;
1064 ParmVarDecl(Kind DK
, DeclContext
*DC
, SourceLocation L
,
1065 IdentifierInfo
*Id
, QualType T
, TypeSourceInfo
*TInfo
,
1066 StorageClass S
, StorageClass SCAsWritten
, Expr
*DefArg
)
1067 : VarDecl(DK
, DC
, L
, Id
, T
, TInfo
, S
, SCAsWritten
),
1068 objcDeclQualifier(OBJC_TQ_None
), HasInheritedDefaultArg(false) {
1069 setDefaultArg(DefArg
);
1073 static ParmVarDecl
*Create(ASTContext
&C
, DeclContext
*DC
,
1074 SourceLocation L
,IdentifierInfo
*Id
,
1075 QualType T
, TypeSourceInfo
*TInfo
,
1076 StorageClass S
, StorageClass SCAsWritten
,
1079 ObjCDeclQualifier
getObjCDeclQualifier() const {
1080 return ObjCDeclQualifier(objcDeclQualifier
);
1082 void setObjCDeclQualifier(ObjCDeclQualifier QTVal
) {
1083 objcDeclQualifier
= QTVal
;
1086 Expr
*getDefaultArg();
1087 const Expr
*getDefaultArg() const {
1088 return const_cast<ParmVarDecl
*>(this)->getDefaultArg();
1091 void setDefaultArg(Expr
*defarg
) {
1092 Init
= reinterpret_cast<Stmt
*>(defarg
);
1095 unsigned getNumDefaultArgTemporaries() const;
1096 CXXTemporary
*getDefaultArgTemporary(unsigned i
);
1097 const CXXTemporary
*getDefaultArgTemporary(unsigned i
) const {
1098 return const_cast<ParmVarDecl
*>(this)->getDefaultArgTemporary(i
);
1101 /// \brief Retrieve the source range that covers the entire default
1103 SourceRange
getDefaultArgRange() const;
1104 void setUninstantiatedDefaultArg(Expr
*arg
) {
1105 Init
= reinterpret_cast<UninstantiatedDefaultArgument
*>(arg
);
1107 Expr
*getUninstantiatedDefaultArg() {
1108 return (Expr
*)Init
.get
<UninstantiatedDefaultArgument
*>();
1110 const Expr
*getUninstantiatedDefaultArg() const {
1111 return (const Expr
*)Init
.get
<UninstantiatedDefaultArgument
*>();
1114 /// hasDefaultArg - Determines whether this parameter has a default argument,
1115 /// either parsed or not.
1116 bool hasDefaultArg() const {
1117 return getInit() || hasUnparsedDefaultArg() ||
1118 hasUninstantiatedDefaultArg();
1121 /// hasUnparsedDefaultArg - Determines whether this parameter has a
1122 /// default argument that has not yet been parsed. This will occur
1123 /// during the processing of a C++ class whose member functions have
1124 /// default arguments, e.g.,
1128 /// void f(int x = 17); // x has an unparsed default argument now
1129 /// }; // x has a regular default argument now
1131 bool hasUnparsedDefaultArg() const {
1132 return Init
.is
<UnparsedDefaultArgument
*>();
1135 bool hasUninstantiatedDefaultArg() const {
1136 return Init
.is
<UninstantiatedDefaultArgument
*>();
1139 /// setUnparsedDefaultArg - Specify that this parameter has an
1140 /// unparsed default argument. The argument will be replaced with a
1141 /// real default argument via setDefaultArg when the class
1142 /// definition enclosing the function declaration that owns this
1143 /// default argument is completed.
1144 void setUnparsedDefaultArg() {
1145 Init
= (UnparsedDefaultArgument
*)0;
1148 bool hasInheritedDefaultArg() const {
1149 return HasInheritedDefaultArg
;
1152 void setHasInheritedDefaultArg(bool I
= true) {
1153 HasInheritedDefaultArg
= I
;
1156 QualType
getOriginalType() const {
1157 if (getTypeSourceInfo())
1158 return getTypeSourceInfo()->getType();
1162 /// \brief Determine whether this parameter is actually a function
1164 bool isParameterPack() const;
1166 /// setOwningFunction - Sets the function declaration that owns this
1167 /// ParmVarDecl. Since ParmVarDecls are often created before the
1168 /// FunctionDecls that own them, this routine is required to update
1169 /// the DeclContext appropriately.
1170 void setOwningFunction(DeclContext
*FD
) { setDeclContext(FD
); }
1172 // Implement isa/cast/dyncast/etc.
1173 static bool classof(const Decl
*D
) { return classofKind(D
->getKind()); }
1174 static bool classof(const ParmVarDecl
*D
) { return true; }
1175 static bool classofKind(Kind K
) { return K
== ParmVar
; }
1178 /// FunctionDecl - An instance of this class is created to represent a
1179 /// function declaration or definition.
1181 /// Since a given function can be declared several times in a program,
1182 /// there may be several FunctionDecls that correspond to that
1183 /// function. Only one of those FunctionDecls will be found when
1184 /// traversing the list of declarations in the context of the
1185 /// FunctionDecl (e.g., the translation unit); this FunctionDecl
1186 /// contains all of the information known about the function. Other,
1187 /// previous declarations of the function are available via the
1188 /// getPreviousDeclaration() chain.
1189 class FunctionDecl
: public DeclaratorDecl
, public DeclContext
,
1190 public Redeclarable
<FunctionDecl
> {
1192 typedef clang::StorageClass StorageClass
;
1194 /// \brief The kind of templated function a FunctionDecl can be.
1195 enum TemplatedKind
{
1197 TK_FunctionTemplate
,
1198 TK_MemberSpecialization
,
1199 TK_FunctionTemplateSpecialization
,
1200 TK_DependentFunctionTemplateSpecialization
1204 /// ParamInfo - new[]'d array of pointers to VarDecls for the formal
1205 /// parameters of this function. This is null if a prototype or if there are
1207 ParmVarDecl
**ParamInfo
;
1209 LazyDeclStmtPtr Body
;
1211 // FIXME: This can be packed into the bitfields in Decl.
1212 // NOTE: VC++ treats enums as signed, avoid using the StorageClass enum
1213 unsigned SClass
: 2;
1214 unsigned SClassAsWritten
: 2;
1216 bool IsInlineSpecified
: 1;
1217 bool IsVirtualAsWritten
: 1;
1219 bool HasInheritedPrototype
: 1;
1220 bool HasWrittenPrototype
: 1;
1222 bool IsTrivial
: 1; // sunk from CXXMethodDecl
1223 bool HasImplicitReturnZero
: 1;
1225 /// \brief End part of this FunctionDecl's source range.
1227 /// We could compute the full range in getSourceRange(). However, when we're
1228 /// dealing with a function definition deserialized from a PCH/AST file,
1229 /// we can only compute the full range once the function body has been
1230 /// de-serialized, so it's far better to have the (sometimes-redundant)
1232 SourceLocation EndRangeLoc
;
1234 /// \brief The template or declaration that this declaration
1235 /// describes or was instantiated from, respectively.
1237 /// For non-templates, this value will be NULL. For function
1238 /// declarations that describe a function template, this will be a
1239 /// pointer to a FunctionTemplateDecl. For member functions
1240 /// of class template specializations, this will be a MemberSpecializationInfo
1241 /// pointer containing information about the specialization.
1242 /// For function template specializations, this will be a
1243 /// FunctionTemplateSpecializationInfo, which contains information about
1244 /// the template being specialized and the template arguments involved in
1245 /// that specialization.
1246 llvm::PointerUnion4
<FunctionTemplateDecl
*,
1247 MemberSpecializationInfo
*,
1248 FunctionTemplateSpecializationInfo
*,
1249 DependentFunctionTemplateSpecializationInfo
*>
1250 TemplateOrSpecialization
;
1252 /// DNLoc - Provides source/type location info for the
1253 /// declaration name embedded in the DeclaratorDecl base class.
1254 DeclarationNameLoc DNLoc
;
1256 /// \brief Specify that this function declaration is actually a function
1257 /// template specialization.
1259 /// \param C the ASTContext.
1261 /// \param Template the function template that this function template
1262 /// specialization specializes.
1264 /// \param TemplateArgs the template arguments that produced this
1265 /// function template specialization from the template.
1267 /// \param InsertPos If non-NULL, the position in the function template
1268 /// specialization set where the function template specialization data will
1271 /// \param TSK the kind of template specialization this is.
1273 /// \param TemplateArgsAsWritten location info of template arguments.
1275 /// \param PointOfInstantiation point at which the function template
1276 /// specialization was first instantiated.
1277 void setFunctionTemplateSpecialization(ASTContext
&C
,
1278 FunctionTemplateDecl
*Template
,
1279 const TemplateArgumentList
*TemplateArgs
,
1281 TemplateSpecializationKind TSK
,
1282 const TemplateArgumentListInfo
*TemplateArgsAsWritten
,
1283 SourceLocation PointOfInstantiation
);
1285 /// \brief Specify that this record is an instantiation of the
1286 /// member function FD.
1287 void setInstantiationOfMemberFunction(ASTContext
&C
, FunctionDecl
*FD
,
1288 TemplateSpecializationKind TSK
);
1290 void setParams(ASTContext
&C
, ParmVarDecl
**NewParamInfo
, unsigned NumParams
);
1293 FunctionDecl(Kind DK
, DeclContext
*DC
, const DeclarationNameInfo
&NameInfo
,
1294 QualType T
, TypeSourceInfo
*TInfo
,
1295 StorageClass S
, StorageClass SCAsWritten
, bool isInlineSpecified
)
1296 : DeclaratorDecl(DK
, DC
, NameInfo
.getLoc(), NameInfo
.getName(), T
, TInfo
),
1298 ParamInfo(0), Body(),
1299 SClass(S
), SClassAsWritten(SCAsWritten
),
1300 IsInline(isInlineSpecified
), IsInlineSpecified(isInlineSpecified
),
1301 IsVirtualAsWritten(false), IsPure(false), HasInheritedPrototype(false),
1302 HasWrittenPrototype(true), IsDeleted(false), IsTrivial(false),
1303 HasImplicitReturnZero(false), EndRangeLoc(NameInfo
.getEndLoc()),
1304 TemplateOrSpecialization(),
1305 DNLoc(NameInfo
.getInfo()) {}
1307 typedef Redeclarable
<FunctionDecl
> redeclarable_base
;
1308 FunctionDecl
*getNextRedeclaration() { return RedeclLink
.getNext(); }
1314 typedef redeclarable_base::redecl_iterator redecl_iterator
;
1315 redecl_iterator
redecls_begin() const {
1316 return redeclarable_base::redecls_begin();
1318 redecl_iterator
redecls_end() const {
1319 return redeclarable_base::redecls_end();
1322 static FunctionDecl
*Create(ASTContext
&C
, DeclContext
*DC
, SourceLocation L
,
1323 DeclarationName N
, QualType T
,
1324 TypeSourceInfo
*TInfo
,
1325 StorageClass S
= SC_None
,
1326 StorageClass SCAsWritten
= SC_None
,
1327 bool isInlineSpecified
= false,
1328 bool hasWrittenPrototype
= true) {
1329 DeclarationNameInfo
NameInfo(N
, L
);
1330 return FunctionDecl::Create(C
, DC
, NameInfo
, T
, TInfo
, S
, SCAsWritten
,
1331 isInlineSpecified
, hasWrittenPrototype
);
1334 static FunctionDecl
*Create(ASTContext
&C
, DeclContext
*DC
,
1335 const DeclarationNameInfo
&NameInfo
,
1336 QualType T
, TypeSourceInfo
*TInfo
,
1337 StorageClass S
= SC_None
,
1338 StorageClass SCAsWritten
= SC_None
,
1339 bool isInlineSpecified
= false,
1340 bool hasWrittenPrototype
= true);
1342 DeclarationNameInfo
getNameInfo() const {
1343 return DeclarationNameInfo(getDeclName(), getLocation(), DNLoc
);
1346 SourceRange
getSourceRange() const {
1347 return SourceRange(getOuterLocStart(), EndRangeLoc
);
1349 void setLocEnd(SourceLocation E
) {
1353 /// \brief Returns true if the function has a body (definition). The
1354 /// function body might be in any of the (re-)declarations of this
1355 /// function. The variant that accepts a FunctionDecl pointer will
1356 /// set that function declaration to the actual declaration
1357 /// containing the body (if there is one).
1358 bool hasBody(const FunctionDecl
*&Definition
) const;
1360 bool hasBody() const {
1361 const FunctionDecl
* Definition
;
1362 return hasBody(Definition
);
1365 /// getBody - Retrieve the body (definition) of the function. The
1366 /// function body might be in any of the (re-)declarations of this
1367 /// function. The variant that accepts a FunctionDecl pointer will
1368 /// set that function declaration to the actual declaration
1369 /// containing the body (if there is one).
1370 /// NOTE: For checking if there is a body, use hasBody() instead, to avoid
1371 /// unnecessary AST de-serialization of the body.
1372 Stmt
*getBody(const FunctionDecl
*&Definition
) const;
1374 Stmt
*getBody() const {
1375 const FunctionDecl
* Definition
;
1376 return getBody(Definition
);
1379 /// isThisDeclarationADefinition - Returns whether this specific
1380 /// declaration of the function is also a definition. This does not
1381 /// determine whether the function has been defined (e.g., in a
1382 /// previous definition); for that information, use getBody.
1383 /// FIXME: Should return true if function is deleted or defaulted. However,
1384 /// CodeGenModule.cpp uses it, and I don't know if this would break it.
1385 bool isThisDeclarationADefinition() const { return Body
; }
1387 void setBody(Stmt
*B
);
1388 void setLazyBody(uint64_t Offset
) { Body
= Offset
; }
1390 /// Whether this function is variadic.
1391 bool isVariadic() const;
1393 /// Whether this function is marked as virtual explicitly.
1394 bool isVirtualAsWritten() const { return IsVirtualAsWritten
; }
1395 void setVirtualAsWritten(bool V
) { IsVirtualAsWritten
= V
; }
1397 /// Whether this virtual function is pure, i.e. makes the containing class
1399 bool isPure() const { return IsPure
; }
1400 void setPure(bool P
= true);
1402 /// Whether this function is "trivial" in some specialized C++ senses.
1403 /// Can only be true for default constructors, copy constructors,
1404 /// copy assignment operators, and destructors. Not meaningful until
1405 /// the class has been fully built by Sema.
1406 bool isTrivial() const { return IsTrivial
; }
1407 void setTrivial(bool IT
) { IsTrivial
= IT
; }
1409 /// Whether falling off this function implicitly returns null/zero.
1410 /// If a more specific implicit return value is required, front-ends
1411 /// should synthesize the appropriate return statements.
1412 bool hasImplicitReturnZero() const { return HasImplicitReturnZero
; }
1413 void setHasImplicitReturnZero(bool IRZ
) { HasImplicitReturnZero
= IRZ
; }
1415 /// \brief Whether this function has a prototype, either because one
1416 /// was explicitly written or because it was "inherited" by merging
1417 /// a declaration without a prototype with a declaration that has a
1419 bool hasPrototype() const {
1420 return HasWrittenPrototype
|| HasInheritedPrototype
;
1423 bool hasWrittenPrototype() const { return HasWrittenPrototype
; }
1425 /// \brief Whether this function inherited its prototype from a
1426 /// previous declaration.
1427 bool hasInheritedPrototype() const { return HasInheritedPrototype
; }
1428 void setHasInheritedPrototype(bool P
= true) { HasInheritedPrototype
= P
; }
1430 /// \brief Whether this function has been deleted.
1432 /// A function that is "deleted" (via the C++0x "= delete" syntax)
1433 /// acts like a normal function, except that it cannot actually be
1434 /// called or have its address taken. Deleted functions are
1435 /// typically used in C++ overload resolution to attract arguments
1436 /// whose type or lvalue/rvalue-ness would permit the use of a
1437 /// different overload that would behave incorrectly. For example,
1438 /// one might use deleted functions to ban implicit conversion from
1439 /// a floating-point number to an Integer type:
1442 /// struct Integer {
1443 /// Integer(long); // construct from a long
1444 /// Integer(double) = delete; // no construction from float or double
1445 /// Integer(long double) = delete; // no construction from long double
1448 bool isDeleted() const { return IsDeleted
; }
1449 void setDeleted(bool D
= true) { IsDeleted
= D
; }
1451 /// \brief Determines whether this is a function "main", which is
1452 /// the entry point into an executable program.
1453 bool isMain() const;
1455 /// \brief Determines whether this function is a function with
1456 /// external, C linkage.
1457 bool isExternC() const;
1459 /// \brief Determines whether this is a global function.
1460 bool isGlobal() const;
1462 void setPreviousDeclaration(FunctionDecl
* PrevDecl
);
1464 const FunctionDecl
*getCanonicalDecl() const;
1465 FunctionDecl
*getCanonicalDecl();
1467 unsigned getBuiltinID() const;
1469 // Iterator access to formal parameters.
1470 unsigned param_size() const { return getNumParams(); }
1471 typedef ParmVarDecl
**param_iterator
;
1472 typedef ParmVarDecl
* const *param_const_iterator
;
1474 param_iterator
param_begin() { return ParamInfo
; }
1475 param_iterator
param_end() { return ParamInfo
+param_size(); }
1477 param_const_iterator
param_begin() const { return ParamInfo
; }
1478 param_const_iterator
param_end() const { return ParamInfo
+param_size(); }
1480 /// getNumParams - Return the number of parameters this function must have
1481 /// based on its FunctionType. This is the length of the ParamInfo array
1482 /// after it has been created.
1483 unsigned getNumParams() const;
1485 const ParmVarDecl
*getParamDecl(unsigned i
) const {
1486 assert(i
< getNumParams() && "Illegal param #");
1487 return ParamInfo
[i
];
1489 ParmVarDecl
*getParamDecl(unsigned i
) {
1490 assert(i
< getNumParams() && "Illegal param #");
1491 return ParamInfo
[i
];
1493 void setParams(ParmVarDecl
**NewParamInfo
, unsigned NumParams
) {
1494 setParams(getASTContext(), NewParamInfo
, NumParams
);
1497 /// getMinRequiredArguments - Returns the minimum number of arguments
1498 /// needed to call this function. This may be fewer than the number of
1499 /// function parameters, if some of the parameters have default
1500 /// arguments (in C++).
1501 unsigned getMinRequiredArguments() const;
1503 QualType
getResultType() const {
1504 return getType()->getAs
<FunctionType
>()->getResultType();
1507 /// \brief Determine the type of an expression that calls this function.
1508 QualType
getCallResultType() const {
1509 return getType()->getAs
<FunctionType
>()->getCallResultType(getASTContext());
1512 StorageClass
getStorageClass() const { return StorageClass(SClass
); }
1513 void setStorageClass(StorageClass SC
);
1515 StorageClass
getStorageClassAsWritten() const {
1516 return StorageClass(SClassAsWritten
);
1519 /// \brief Determine whether the "inline" keyword was specified for this
1521 bool isInlineSpecified() const { return IsInlineSpecified
; }
1523 /// Set whether the "inline" keyword was specified for this function.
1524 void setInlineSpecified(bool I
) {
1525 IsInlineSpecified
= I
;
1529 /// Flag that this function is implicitly inline.
1530 void setImplicitlyInline() {
1534 /// \brief Determine whether this function should be inlined, because it is
1535 /// either marked "inline" or is a member function of a C++ class that
1536 /// was defined in the class body.
1537 bool isInlined() const;
1539 bool isInlineDefinitionExternallyVisible() const;
1541 /// isOverloadedOperator - Whether this function declaration
1542 /// represents an C++ overloaded operator, e.g., "operator+".
1543 bool isOverloadedOperator() const {
1544 return getOverloadedOperator() != OO_None
;
1547 OverloadedOperatorKind
getOverloadedOperator() const;
1549 const IdentifierInfo
*getLiteralIdentifier() const;
1551 /// \brief If this function is an instantiation of a member function
1552 /// of a class template specialization, retrieves the function from
1553 /// which it was instantiated.
1555 /// This routine will return non-NULL for (non-templated) member
1556 /// functions of class templates and for instantiations of function
1557 /// templates. For example, given:
1560 /// template<typename T>
1566 /// The declaration for X<int>::f is a (non-templated) FunctionDecl
1567 /// whose parent is the class template specialization X<int>. For
1568 /// this declaration, getInstantiatedFromFunction() will return
1569 /// the FunctionDecl X<T>::A. When a complete definition of
1570 /// X<int>::A is required, it will be instantiated from the
1571 /// declaration returned by getInstantiatedFromMemberFunction().
1572 FunctionDecl
*getInstantiatedFromMemberFunction() const;
1574 /// \brief What kind of templated function this is.
1575 TemplatedKind
getTemplatedKind() const;
1577 /// \brief If this function is an instantiation of a member function of a
1578 /// class template specialization, retrieves the member specialization
1580 MemberSpecializationInfo
*getMemberSpecializationInfo() const;
1582 /// \brief Specify that this record is an instantiation of the
1583 /// member function FD.
1584 void setInstantiationOfMemberFunction(FunctionDecl
*FD
,
1585 TemplateSpecializationKind TSK
) {
1586 setInstantiationOfMemberFunction(getASTContext(), FD
, TSK
);
1589 /// \brief Retrieves the function template that is described by this
1590 /// function declaration.
1592 /// Every function template is represented as a FunctionTemplateDecl
1593 /// and a FunctionDecl (or something derived from FunctionDecl). The
1594 /// former contains template properties (such as the template
1595 /// parameter lists) while the latter contains the actual
1596 /// description of the template's
1597 /// contents. FunctionTemplateDecl::getTemplatedDecl() retrieves the
1598 /// FunctionDecl that describes the function template,
1599 /// getDescribedFunctionTemplate() retrieves the
1600 /// FunctionTemplateDecl from a FunctionDecl.
1601 FunctionTemplateDecl
*getDescribedFunctionTemplate() const {
1602 return TemplateOrSpecialization
.dyn_cast
<FunctionTemplateDecl
*>();
1605 void setDescribedFunctionTemplate(FunctionTemplateDecl
*Template
) {
1606 TemplateOrSpecialization
= Template
;
1609 /// \brief Determine whether this function is a function template
1611 bool isFunctionTemplateSpecialization() const {
1612 return getPrimaryTemplate() != 0;
1615 /// \brief If this function is actually a function template specialization,
1616 /// retrieve information about this function template specialization.
1617 /// Otherwise, returns NULL.
1618 FunctionTemplateSpecializationInfo
*getTemplateSpecializationInfo() const {
1619 return TemplateOrSpecialization
.
1620 dyn_cast
<FunctionTemplateSpecializationInfo
*>();
1623 /// \brief Determines whether this function is a function template
1624 /// specialization or a member of a class template specialization that can
1625 /// be implicitly instantiated.
1626 bool isImplicitlyInstantiable() const;
1628 /// \brief Retrieve the function declaration from which this function could
1629 /// be instantiated, if it is an instantiation (rather than a non-template
1630 /// or a specialization, for example).
1631 FunctionDecl
*getTemplateInstantiationPattern() const;
1633 /// \brief Retrieve the primary template that this function template
1634 /// specialization either specializes or was instantiated from.
1636 /// If this function declaration is not a function template specialization,
1638 FunctionTemplateDecl
*getPrimaryTemplate() const;
1640 /// \brief Retrieve the template arguments used to produce this function
1641 /// template specialization from the primary template.
1643 /// If this function declaration is not a function template specialization,
1645 const TemplateArgumentList
*getTemplateSpecializationArgs() const;
1647 /// \brief Retrieve the template argument list as written in the sources,
1650 /// If this function declaration is not a function template specialization
1651 /// or if it had no explicit template argument list, returns NULL.
1652 /// Note that it an explicit template argument list may be written empty,
1653 /// e.g., template<> void foo<>(char* s);
1654 const TemplateArgumentListInfo
*
1655 getTemplateSpecializationArgsAsWritten() const;
1657 /// \brief Specify that this function declaration is actually a function
1658 /// template specialization.
1660 /// \param Template the function template that this function template
1661 /// specialization specializes.
1663 /// \param TemplateArgs the template arguments that produced this
1664 /// function template specialization from the template.
1666 /// \param InsertPos If non-NULL, the position in the function template
1667 /// specialization set where the function template specialization data will
1670 /// \param TSK the kind of template specialization this is.
1672 /// \param TemplateArgsAsWritten location info of template arguments.
1674 /// \param PointOfInstantiation point at which the function template
1675 /// specialization was first instantiated.
1676 void setFunctionTemplateSpecialization(FunctionTemplateDecl
*Template
,
1677 const TemplateArgumentList
*TemplateArgs
,
1679 TemplateSpecializationKind TSK
= TSK_ImplicitInstantiation
,
1680 const TemplateArgumentListInfo
*TemplateArgsAsWritten
= 0,
1681 SourceLocation PointOfInstantiation
= SourceLocation()) {
1682 setFunctionTemplateSpecialization(getASTContext(), Template
, TemplateArgs
,
1683 InsertPos
, TSK
, TemplateArgsAsWritten
,
1684 PointOfInstantiation
);
1687 /// \brief Specifies that this function declaration is actually a
1688 /// dependent function template specialization.
1689 void setDependentTemplateSpecialization(ASTContext
&Context
,
1690 const UnresolvedSetImpl
&Templates
,
1691 const TemplateArgumentListInfo
&TemplateArgs
);
1693 DependentFunctionTemplateSpecializationInfo
*
1694 getDependentSpecializationInfo() const {
1695 return TemplateOrSpecialization
.
1696 dyn_cast
<DependentFunctionTemplateSpecializationInfo
*>();
1699 /// \brief Determine what kind of template instantiation this function
1701 TemplateSpecializationKind
getTemplateSpecializationKind() const;
1703 /// \brief Determine what kind of template instantiation this function
1705 void setTemplateSpecializationKind(TemplateSpecializationKind TSK
,
1706 SourceLocation PointOfInstantiation
= SourceLocation());
1708 /// \brief Retrieve the (first) point of instantiation of a function template
1709 /// specialization or a member of a class template specialization.
1711 /// \returns the first point of instantiation, if this function was
1712 /// instantiated from a template; otherwie, returns an invalid source
1714 SourceLocation
getPointOfInstantiation() const;
1716 /// \brief Determine whether this is or was instantiated from an out-of-line
1717 /// definition of a member function.
1718 bool isOutOfLine() const;
1720 // Implement isa/cast/dyncast/etc.
1721 static bool classof(const Decl
*D
) { return classofKind(D
->getKind()); }
1722 static bool classof(const FunctionDecl
*D
) { return true; }
1723 static bool classofKind(Kind K
) {
1724 return K
>= firstFunction
&& K
<= lastFunction
;
1726 static DeclContext
*castToDeclContext(const FunctionDecl
*D
) {
1727 return static_cast<DeclContext
*>(const_cast<FunctionDecl
*>(D
));
1729 static FunctionDecl
*castFromDeclContext(const DeclContext
*DC
) {
1730 return static_cast<FunctionDecl
*>(const_cast<DeclContext
*>(DC
));
1733 friend class ASTDeclReader
;
1734 friend class ASTDeclWriter
;
1738 /// FieldDecl - An instance of this class is created by Sema::ActOnField to
1739 /// represent a member of a struct/union/class.
1740 class FieldDecl
: public DeclaratorDecl
{
1741 // FIXME: This can be packed into the bitfields in Decl.
1743 mutable unsigned CachedFieldIndex
: 31;
1747 FieldDecl(Kind DK
, DeclContext
*DC
, SourceLocation L
,
1748 IdentifierInfo
*Id
, QualType T
, TypeSourceInfo
*TInfo
,
1749 Expr
*BW
, bool Mutable
)
1750 : DeclaratorDecl(DK
, DC
, L
, Id
, T
, TInfo
),
1751 Mutable(Mutable
), CachedFieldIndex(0), BitWidth(BW
) {
1755 static FieldDecl
*Create(const ASTContext
&C
, DeclContext
*DC
,
1756 SourceLocation L
, IdentifierInfo
*Id
, QualType T
,
1757 TypeSourceInfo
*TInfo
, Expr
*BW
, bool Mutable
);
1759 /// getFieldIndex - Returns the index of this field within its record,
1760 /// as appropriate for passing to ASTRecordLayout::getFieldOffset.
1761 unsigned getFieldIndex() const;
1763 /// isMutable - Determines whether this field is mutable (C++ only).
1764 bool isMutable() const { return Mutable
; }
1766 /// \brief Set whether this field is mutable (C++ only).
1767 void setMutable(bool M
) { Mutable
= M
; }
1769 /// isBitfield - Determines whether this field is a bitfield.
1770 bool isBitField() const { return BitWidth
!= NULL
; }
1772 /// @brief Determines whether this is an unnamed bitfield.
1773 bool isUnnamedBitfield() const { return BitWidth
!= NULL
&& !getDeclName(); }
1775 /// isAnonymousStructOrUnion - Determines whether this field is a
1776 /// representative for an anonymous struct or union. Such fields are
1777 /// unnamed and are implicitly generated by the implementation to
1778 /// store the data for the anonymous union or struct.
1779 bool isAnonymousStructOrUnion() const;
1781 Expr
*getBitWidth() const { return BitWidth
; }
1782 void setBitWidth(Expr
*BW
) { BitWidth
= BW
; }
1784 /// getParent - Returns the parent of this field declaration, which
1785 /// is the struct in which this method is defined.
1786 const RecordDecl
*getParent() const {
1787 return cast
<RecordDecl
>(getDeclContext());
1790 RecordDecl
*getParent() {
1791 return cast
<RecordDecl
>(getDeclContext());
1794 // Implement isa/cast/dyncast/etc.
1795 static bool classof(const Decl
*D
) { return classofKind(D
->getKind()); }
1796 static bool classof(const FieldDecl
*D
) { return true; }
1797 static bool classofKind(Kind K
) { return K
>= firstField
&& K
<= lastField
; }
1800 /// EnumConstantDecl - An instance of this object exists for each enum constant
1801 /// that is defined. For example, in "enum X {a,b}", each of a/b are
1802 /// EnumConstantDecl's, X is an instance of EnumDecl, and the type of a/b is a
1803 /// TagType for the X EnumDecl.
1804 class EnumConstantDecl
: public ValueDecl
{
1805 Stmt
*Init
; // an integer constant expression
1806 llvm::APSInt Val
; // The value.
1808 EnumConstantDecl(DeclContext
*DC
, SourceLocation L
,
1809 IdentifierInfo
*Id
, QualType T
, Expr
*E
,
1810 const llvm::APSInt
&V
)
1811 : ValueDecl(EnumConstant
, DC
, L
, Id
, T
), Init((Stmt
*)E
), Val(V
) {}
1815 static EnumConstantDecl
*Create(ASTContext
&C
, EnumDecl
*DC
,
1816 SourceLocation L
, IdentifierInfo
*Id
,
1817 QualType T
, Expr
*E
,
1818 const llvm::APSInt
&V
);
1820 const Expr
*getInitExpr() const { return (const Expr
*) Init
; }
1821 Expr
*getInitExpr() { return (Expr
*) Init
; }
1822 const llvm::APSInt
&getInitVal() const { return Val
; }
1824 void setInitExpr(Expr
*E
) { Init
= (Stmt
*) E
; }
1825 void setInitVal(const llvm::APSInt
&V
) { Val
= V
; }
1827 SourceRange
getSourceRange() const;
1829 // Implement isa/cast/dyncast/etc.
1830 static bool classof(const Decl
*D
) { return classofKind(D
->getKind()); }
1831 static bool classof(const EnumConstantDecl
*D
) { return true; }
1832 static bool classofKind(Kind K
) { return K
== EnumConstant
; }
1834 friend class StmtIteratorBase
;
1837 /// IndirectFieldDecl - An instance of this class is created to represent a
1838 /// field injected from an anonymous union/struct into the parent scope.
1839 /// IndirectFieldDecl are always implicit.
1840 class IndirectFieldDecl
: public ValueDecl
{
1841 NamedDecl
**Chaining
;
1842 unsigned ChainingSize
;
1844 IndirectFieldDecl(DeclContext
*DC
, SourceLocation L
,
1845 DeclarationName N
, QualType T
,
1846 NamedDecl
**CH
, unsigned CHS
)
1847 : ValueDecl(IndirectField
, DC
, L
, N
, T
), Chaining(CH
), ChainingSize(CHS
) {}
1850 static IndirectFieldDecl
*Create(ASTContext
&C
, DeclContext
*DC
,
1851 SourceLocation L
, IdentifierInfo
*Id
,
1852 QualType T
, NamedDecl
**CH
, unsigned CHS
);
1854 typedef NamedDecl
* const *chain_iterator
;
1855 chain_iterator
chain_begin() const { return Chaining
; }
1856 chain_iterator
chain_end() const { return Chaining
+ChainingSize
; }
1858 unsigned getChainingSize() const { return ChainingSize
; }
1860 FieldDecl
*getAnonField() const {
1861 assert(ChainingSize
>= 2);
1862 return cast
<FieldDecl
>(Chaining
[ChainingSize
- 1]);
1865 VarDecl
*getVarDecl() const {
1866 assert(ChainingSize
>= 2);
1867 return dyn_cast
<VarDecl
>(*chain_begin());
1870 // Implement isa/cast/dyncast/etc.
1871 static bool classof(const Decl
*D
) { return classofKind(D
->getKind()); }
1872 static bool classof(const IndirectFieldDecl
*D
) { return true; }
1873 static bool classofKind(Kind K
) { return K
== IndirectField
; }
1874 friend class ASTDeclReader
;
1877 /// TypeDecl - Represents a declaration of a type.
1879 class TypeDecl
: public NamedDecl
{
1880 /// TypeForDecl - This indicates the Type object that represents
1881 /// this TypeDecl. It is a cache maintained by
1882 /// ASTContext::getTypedefType, ASTContext::getTagDeclType, and
1883 /// ASTContext::getTemplateTypeParmType, and TemplateTypeParmDecl.
1884 mutable const Type
*TypeForDecl
;
1885 friend class ASTContext
;
1886 friend class DeclContext
;
1887 friend class TagDecl
;
1888 friend class TemplateTypeParmDecl
;
1889 friend class TagType
;
1892 TypeDecl(Kind DK
, DeclContext
*DC
, SourceLocation L
,
1894 : NamedDecl(DK
, DC
, L
, Id
), TypeForDecl(0) {}
1897 // Low-level accessor
1898 const Type
*getTypeForDecl() const { return TypeForDecl
; }
1899 void setTypeForDecl(const Type
*TD
) { TypeForDecl
= TD
; }
1901 // Implement isa/cast/dyncast/etc.
1902 static bool classof(const Decl
*D
) { return classofKind(D
->getKind()); }
1903 static bool classof(const TypeDecl
*D
) { return true; }
1904 static bool classofKind(Kind K
) { return K
>= firstType
&& K
<= lastType
; }
1908 class TypedefDecl
: public TypeDecl
, public Redeclarable
<TypedefDecl
> {
1909 /// UnderlyingType - This is the type the typedef is set to.
1910 TypeSourceInfo
*TInfo
;
1912 TypedefDecl(DeclContext
*DC
, SourceLocation L
,
1913 IdentifierInfo
*Id
, TypeSourceInfo
*TInfo
)
1914 : TypeDecl(Typedef
, DC
, L
, Id
), TInfo(TInfo
) {}
1917 typedef Redeclarable
<TypedefDecl
> redeclarable_base
;
1918 TypedefDecl
*getNextRedeclaration() { return RedeclLink
.getNext(); }
1923 typedef redeclarable_base::redecl_iterator redecl_iterator
;
1924 redecl_iterator
redecls_begin() const {
1925 return redeclarable_base::redecls_begin();
1927 redecl_iterator
redecls_end() const {
1928 return redeclarable_base::redecls_end();
1931 static TypedefDecl
*Create(ASTContext
&C
, DeclContext
*DC
,
1932 SourceLocation L
, IdentifierInfo
*Id
,
1933 TypeSourceInfo
*TInfo
);
1935 TypeSourceInfo
*getTypeSourceInfo() const {
1939 /// Retrieves the canonical declaration of this typedef.
1940 TypedefDecl
*getCanonicalDecl() {
1941 return getFirstDeclaration();
1943 const TypedefDecl
*getCanonicalDecl() const {
1944 return getFirstDeclaration();
1947 QualType
getUnderlyingType() const {
1948 return TInfo
->getType();
1950 void setTypeSourceInfo(TypeSourceInfo
*newType
) {
1954 // Implement isa/cast/dyncast/etc.
1955 static bool classof(const Decl
*D
) { return classofKind(D
->getKind()); }
1956 static bool classof(const TypedefDecl
*D
) { return true; }
1957 static bool classofKind(Kind K
) { return K
== Typedef
; }
1962 /// TagDecl - Represents the declaration of a struct/union/class/enum.
1964 : public TypeDecl
, public DeclContext
, public Redeclarable
<TagDecl
> {
1966 // This is really ugly.
1967 typedef TagTypeKind TagKind
;
1970 // FIXME: This can be packed into the bitfields in Decl.
1971 /// TagDeclKind - The TagKind enum.
1972 unsigned TagDeclKind
: 2;
1974 /// IsDefinition - True if this is a definition ("struct foo {};"), false if
1975 /// it is a declaration ("struct foo;").
1976 bool IsDefinition
: 1;
1978 /// IsBeingDefined - True if this is currently being defined.
1979 bool IsBeingDefined
: 1;
1981 /// IsEmbeddedInDeclarator - True if this tag declaration is
1982 /// "embedded" (i.e., defined or declared for the very first time)
1983 /// in the syntax of a declarator.
1984 bool IsEmbeddedInDeclarator
: 1;
1987 // These are used by (and only defined for) EnumDecl.
1988 unsigned NumPositiveBits
: 8;
1989 unsigned NumNegativeBits
: 8;
1991 /// IsScoped - True if this tag declaration is a scoped enumeration. Only
1992 /// possible in C++0x mode.
1994 /// IsScopedUsingClassTag - If this tag declaration is a scoped enum,
1995 /// then this is true if the scoped enum was declared using the class
1996 /// tag, false if it was declared with the struct tag. No meaning is
1997 /// associated if this tag declaration is not a scoped enum.
1998 bool IsScopedUsingClassTag
: 1;
2000 /// IsFixed - True if this is an enumeration with fixed underlying type. Only
2001 /// possible in C++0x mode.
2005 SourceLocation TagKeywordLoc
;
2006 SourceLocation RBraceLoc
;
2008 // A struct representing syntactic qualifier info,
2009 // to be used for the (uncommon) case of out-of-line declarations.
2010 typedef QualifierInfo ExtInfo
;
2012 /// TypedefDeclOrQualifier - If the (out-of-line) tag declaration name
2013 /// is qualified, it points to the qualifier info (nns and range);
2014 /// otherwise, if the tag declaration is anonymous and it is part of
2015 /// a typedef, it points to the TypedefDecl (used for mangling);
2016 /// otherwise, it is a null (TypedefDecl) pointer.
2017 llvm::PointerUnion
<TypedefDecl
*, ExtInfo
*> TypedefDeclOrQualifier
;
2019 bool hasExtInfo() const { return TypedefDeclOrQualifier
.is
<ExtInfo
*>(); }
2020 ExtInfo
*getExtInfo() { return TypedefDeclOrQualifier
.get
<ExtInfo
*>(); }
2021 const ExtInfo
*getExtInfo() const {
2022 return TypedefDeclOrQualifier
.get
<ExtInfo
*>();
2026 TagDecl(Kind DK
, TagKind TK
, DeclContext
*DC
,
2027 SourceLocation L
, IdentifierInfo
*Id
,
2028 TagDecl
*PrevDecl
, SourceLocation TKL
= SourceLocation())
2029 : TypeDecl(DK
, DC
, L
, Id
), DeclContext(DK
), TagKeywordLoc(TKL
),
2030 TypedefDeclOrQualifier((TypedefDecl
*) 0) {
2031 assert((DK
!= Enum
|| TK
== TTK_Enum
) &&
2032 "EnumDecl not matched with TTK_Enum");
2034 IsDefinition
= false;
2035 IsBeingDefined
= false;
2036 IsEmbeddedInDeclarator
= false;
2037 setPreviousDeclaration(PrevDecl
);
2040 typedef Redeclarable
<TagDecl
> redeclarable_base
;
2041 TagDecl
*getNextRedeclaration() { return RedeclLink
.getNext(); }
2043 /// @brief Completes the definition of this tag declaration.
2045 /// This is a helper function for derived classes.
2046 void completeDefinition();
2051 typedef redeclarable_base::redecl_iterator redecl_iterator
;
2052 redecl_iterator
redecls_begin() const {
2053 return redeclarable_base::redecls_begin();
2055 redecl_iterator
redecls_end() const {
2056 return redeclarable_base::redecls_end();
2059 SourceLocation
getRBraceLoc() const { return RBraceLoc
; }
2060 void setRBraceLoc(SourceLocation L
) { RBraceLoc
= L
; }
2062 SourceLocation
getTagKeywordLoc() const { return TagKeywordLoc
; }
2063 void setTagKeywordLoc(SourceLocation TKL
) { TagKeywordLoc
= TKL
; }
2065 /// getInnerLocStart - Return SourceLocation representing start of source
2066 /// range ignoring outer template declarations.
2067 SourceLocation
getInnerLocStart() const;
2069 /// getOuterLocStart - Return SourceLocation representing start of source
2070 /// range taking into account any outer template declarations.
2071 SourceLocation
getOuterLocStart() const;
2072 SourceRange
getSourceRange() const;
2074 TagDecl
* getCanonicalDecl();
2075 const TagDecl
* getCanonicalDecl() const {
2076 return const_cast<TagDecl
*>(this)->getCanonicalDecl();
2079 /// isThisDeclarationADefinition() - Return true if this declaration
2080 /// defines the type. Provided for consistency.
2081 bool isThisDeclarationADefinition() const {
2082 return isDefinition();
2085 /// isDefinition - Return true if this decl has its body specified.
2086 bool isDefinition() const {
2087 return IsDefinition
;
2090 /// isBeingDefined - Return true if this decl is currently being defined.
2091 bool isBeingDefined() const {
2092 return IsBeingDefined
;
2095 bool isEmbeddedInDeclarator() const {
2096 return IsEmbeddedInDeclarator
;
2098 void setEmbeddedInDeclarator(bool isInDeclarator
) {
2099 IsEmbeddedInDeclarator
= isInDeclarator
;
2102 /// \brief Whether this declaration declares a type that is
2103 /// dependent, i.e., a type that somehow depends on template
2105 bool isDependentType() const { return isDependentContext(); }
2107 /// @brief Starts the definition of this tag declaration.
2109 /// This method should be invoked at the beginning of the definition
2110 /// of this tag declaration. It will set the tag type into a state
2111 /// where it is in the process of being defined.
2112 void startDefinition();
2114 /// getDefinition - Returns the TagDecl that actually defines this
2115 /// struct/union/class/enum. When determining whether or not a
2116 /// struct/union/class/enum is completely defined, one should use this method
2117 /// as opposed to 'isDefinition'. 'isDefinition' indicates whether or not a
2118 /// specific TagDecl is defining declaration, not whether or not the
2119 /// struct/union/class/enum type is defined. This method returns NULL if
2120 /// there is no TagDecl that defines the struct/union/class/enum.
2121 TagDecl
* getDefinition() const;
2123 void setDefinition(bool V
) { IsDefinition
= V
; }
2125 const char *getKindName() const {
2126 return TypeWithKeyword::getTagTypeKindName(getTagKind());
2129 TagKind
getTagKind() const {
2130 return TagKind(TagDeclKind
);
2133 void setTagKind(TagKind TK
) { TagDeclKind
= TK
; }
2135 bool isStruct() const { return getTagKind() == TTK_Struct
; }
2136 bool isClass() const { return getTagKind() == TTK_Class
; }
2137 bool isUnion() const { return getTagKind() == TTK_Union
; }
2138 bool isEnum() const { return getTagKind() == TTK_Enum
; }
2140 TypedefDecl
*getTypedefForAnonDecl() const {
2141 return hasExtInfo() ? 0 : TypedefDeclOrQualifier
.get
<TypedefDecl
*>();
2144 void setTypedefForAnonDecl(TypedefDecl
*TDD
);
2146 NestedNameSpecifier
*getQualifier() const {
2147 return hasExtInfo() ? getExtInfo()->NNS
: 0;
2149 SourceRange
getQualifierRange() const {
2150 return hasExtInfo() ? getExtInfo()->NNSRange
: SourceRange();
2152 void setQualifierInfo(NestedNameSpecifier
*Qualifier
,
2153 SourceRange QualifierRange
);
2155 unsigned getNumTemplateParameterLists() const {
2156 return hasExtInfo() ? getExtInfo()->NumTemplParamLists
: 0;
2158 TemplateParameterList
*getTemplateParameterList(unsigned i
) const {
2159 assert(i
< getNumTemplateParameterLists());
2160 return getExtInfo()->TemplParamLists
[i
];
2162 void setTemplateParameterListsInfo(ASTContext
&Context
, unsigned NumTPLists
,
2163 TemplateParameterList
**TPLists
) {
2164 getExtInfo()->setTemplateParameterListsInfo(Context
, NumTPLists
, TPLists
);
2167 // Implement isa/cast/dyncast/etc.
2168 static bool classof(const Decl
*D
) { return classofKind(D
->getKind()); }
2169 static bool classof(const TagDecl
*D
) { return true; }
2170 static bool classofKind(Kind K
) { return K
>= firstTag
&& K
<= lastTag
; }
2172 static DeclContext
*castToDeclContext(const TagDecl
*D
) {
2173 return static_cast<DeclContext
*>(const_cast<TagDecl
*>(D
));
2175 static TagDecl
*castFromDeclContext(const DeclContext
*DC
) {
2176 return static_cast<TagDecl
*>(const_cast<DeclContext
*>(DC
));
2179 friend class ASTDeclReader
;
2180 friend class ASTDeclWriter
;
2183 /// EnumDecl - Represents an enum. As an extension, we allow forward-declared
2185 class EnumDecl
: public TagDecl
{
2186 /// IntegerType - This represent the integer type that the enum corresponds
2187 /// to for code generation purposes. Note that the enumerator constants may
2188 /// have a different type than this does.
2190 /// If the underlying integer type was explicitly stated in the source
2191 /// code, this is a TypeSourceInfo* for that type. Otherwise this type
2192 /// was automatically deduced somehow, and this is a Type*.
2194 /// Normally if IsFixed(), this would contain a TypeSourceInfo*, but in
2195 /// some cases it won't.
2197 /// The underlying type of an enumeration never has any qualifiers, so
2198 /// we can get away with just storing a raw Type*, and thus save an
2199 /// extra pointer when TypeSourceInfo is needed.
2201 llvm::PointerUnion
<const Type
*, TypeSourceInfo
*> IntegerType
;
2203 /// PromotionType - The integer type that values of this type should
2204 /// promote to. In C, enumerators are generally of an integer type
2205 /// directly, but gcc-style large enumerators (and all enumerators
2206 /// in C++) are of the enum type instead.
2207 QualType PromotionType
;
2209 /// \brief If the enumeration was instantiated from an enumeration
2210 /// within a class or function template, this pointer refers to the
2211 /// enumeration declared within the template.
2212 EnumDecl
*InstantiatedFrom
;
2214 // The number of positive and negative bits required by the
2215 // enumerators are stored in the SubclassBits field.
2218 NumBitsMask
= (1 << NumBitsWidth
) - 1
2221 EnumDecl(DeclContext
*DC
, SourceLocation L
,
2222 IdentifierInfo
*Id
, EnumDecl
*PrevDecl
, SourceLocation TKL
,
2223 bool Scoped
, bool ScopedUsingClassTag
, bool Fixed
)
2224 : TagDecl(Enum
, TTK_Enum
, DC
, L
, Id
, PrevDecl
, TKL
), InstantiatedFrom(0) {
2225 assert(Scoped
|| !ScopedUsingClassTag
);
2226 IntegerType
= (const Type
*)0;
2227 NumNegativeBits
= 0;
2228 NumPositiveBits
= 0;
2230 IsScopedUsingClassTag
= ScopedUsingClassTag
;
2234 EnumDecl
*getCanonicalDecl() {
2235 return cast
<EnumDecl
>(TagDecl::getCanonicalDecl());
2237 const EnumDecl
*getCanonicalDecl() const {
2238 return cast
<EnumDecl
>(TagDecl::getCanonicalDecl());
2241 const EnumDecl
*getPreviousDeclaration() const {
2242 return cast_or_null
<EnumDecl
>(TagDecl::getPreviousDeclaration());
2244 EnumDecl
*getPreviousDeclaration() {
2245 return cast_or_null
<EnumDecl
>(TagDecl::getPreviousDeclaration());
2248 static EnumDecl
*Create(ASTContext
&C
, DeclContext
*DC
,
2249 SourceLocation L
, IdentifierInfo
*Id
,
2250 SourceLocation TKL
, EnumDecl
*PrevDecl
,
2251 bool IsScoped
, bool IsScopedUsingClassTag
,
2253 static EnumDecl
*Create(ASTContext
&C
, EmptyShell Empty
);
2255 /// completeDefinition - When created, the EnumDecl corresponds to a
2256 /// forward-declared enum. This method is used to mark the
2257 /// declaration as being defined; it's enumerators have already been
2258 /// added (via DeclContext::addDecl). NewType is the new underlying
2259 /// type of the enumeration type.
2260 void completeDefinition(QualType NewType
,
2261 QualType PromotionType
,
2262 unsigned NumPositiveBits
,
2263 unsigned NumNegativeBits
);
2265 // enumerator_iterator - Iterates through the enumerators of this
2267 typedef specific_decl_iterator
<EnumConstantDecl
> enumerator_iterator
;
2269 enumerator_iterator
enumerator_begin() const {
2270 const EnumDecl
*E
= cast_or_null
<EnumDecl
>(getDefinition());
2273 return enumerator_iterator(E
->decls_begin());
2276 enumerator_iterator
enumerator_end() const {
2277 const EnumDecl
*E
= cast_or_null
<EnumDecl
>(getDefinition());
2280 return enumerator_iterator(E
->decls_end());
2283 /// getPromotionType - Return the integer type that enumerators
2284 /// should promote to.
2285 QualType
getPromotionType() const { return PromotionType
; }
2287 /// \brief Set the promotion type.
2288 void setPromotionType(QualType T
) { PromotionType
= T
; }
2290 /// getIntegerType - Return the integer type this enum decl corresponds to.
2291 /// This returns a null qualtype for an enum forward definition.
2292 QualType
getIntegerType() const {
2295 if (const Type
* T
= IntegerType
.dyn_cast
<const Type
*>())
2296 return QualType(T
, 0);
2297 return IntegerType
.get
<TypeSourceInfo
*>()->getType();
2300 /// \brief Set the underlying integer type.
2301 void setIntegerType(QualType T
) { IntegerType
= T
.getTypePtrOrNull(); }
2303 /// \brief Set the underlying integer type source info.
2304 void setIntegerTypeSourceInfo(TypeSourceInfo
* TInfo
) { IntegerType
= TInfo
; }
2306 /// \brief Return the type source info for the underlying integer type,
2307 /// if no type source info exists, return 0.
2308 TypeSourceInfo
* getIntegerTypeSourceInfo() const {
2309 return IntegerType
.dyn_cast
<TypeSourceInfo
*>();
2312 /// \brief Returns the width in bits requred to store all the
2313 /// non-negative enumerators of this enum.
2314 unsigned getNumPositiveBits() const {
2315 return NumPositiveBits
;
2317 void setNumPositiveBits(unsigned Num
) {
2318 NumPositiveBits
= Num
;
2319 assert(NumPositiveBits
== Num
&& "can't store this bitcount");
2322 /// \brief Returns the width in bits requred to store all the
2323 /// negative enumerators of this enum. These widths include
2324 /// the rightmost leading 1; that is:
2326 /// MOST NEGATIVE ENUMERATOR PATTERN NUM NEGATIVE BITS
2327 /// ------------------------ ------- -----------------
2331 unsigned getNumNegativeBits() const {
2332 return NumNegativeBits
;
2334 void setNumNegativeBits(unsigned Num
) {
2335 NumNegativeBits
= Num
;
2338 /// \brief Returns true if this is a C++0x scoped enumeration.
2339 bool isScoped() const {
2343 /// \brief Returns true if this is a C++0x scoped enumeration.
2344 bool isScopedUsingClassTag() const {
2345 return IsScopedUsingClassTag
;
2348 /// \brief Returns true if this is a C++0x enumeration with fixed underlying
2350 bool isFixed() const {
2354 /// \brief Returns true if this can be considered a complete type.
2355 bool isComplete() const {
2356 return isDefinition() || isFixed();
2359 /// \brief Returns the enumeration (declared within the template)
2360 /// from which this enumeration type was instantiated, or NULL if
2361 /// this enumeration was not instantiated from any template.
2362 EnumDecl
*getInstantiatedFromMemberEnum() const {
2363 return InstantiatedFrom
;
2366 void setInstantiationOfMemberEnum(EnumDecl
*IF
) { InstantiatedFrom
= IF
; }
2368 static bool classof(const Decl
*D
) { return classofKind(D
->getKind()); }
2369 static bool classof(const EnumDecl
*D
) { return true; }
2370 static bool classofKind(Kind K
) { return K
== Enum
; }
2372 friend class ASTDeclReader
;
2376 /// RecordDecl - Represents a struct/union/class. For example:
2377 /// struct X; // Forward declaration, no "body".
2378 /// union Y { int A, B; }; // Has body with members A and B (FieldDecls).
2379 /// This decl will be marked invalid if *any* members are invalid.
2381 class RecordDecl
: public TagDecl
{
2382 // FIXME: This can be packed into the bitfields in Decl.
2383 /// HasFlexibleArrayMember - This is true if this struct ends with a flexible
2384 /// array member (e.g. int X[]) or if this union contains a struct that does.
2385 /// If so, this cannot be contained in arrays or other structs as a member.
2386 bool HasFlexibleArrayMember
: 1;
2388 /// AnonymousStructOrUnion - Whether this is the type of an anonymous struct
2390 bool AnonymousStructOrUnion
: 1;
2392 /// HasObjectMember - This is true if this struct has at least one member
2393 /// containing an object.
2394 bool HasObjectMember
: 1;
2396 /// \brief Whether the field declarations of this record have been loaded
2397 /// from external storage. To avoid unnecessary deserialization of
2398 /// methods/nested types we allow deserialization of just the fields
2400 mutable bool LoadedFieldsFromExternalStorage
: 1;
2401 friend class DeclContext
;
2404 RecordDecl(Kind DK
, TagKind TK
, DeclContext
*DC
,
2405 SourceLocation L
, IdentifierInfo
*Id
,
2406 RecordDecl
*PrevDecl
, SourceLocation TKL
);
2409 static RecordDecl
*Create(const ASTContext
&C
, TagKind TK
, DeclContext
*DC
,
2410 SourceLocation L
, IdentifierInfo
*Id
,
2411 SourceLocation TKL
= SourceLocation(),
2412 RecordDecl
* PrevDecl
= 0);
2413 static RecordDecl
*Create(const ASTContext
&C
, EmptyShell Empty
);
2415 const RecordDecl
*getPreviousDeclaration() const {
2416 return cast_or_null
<RecordDecl
>(TagDecl::getPreviousDeclaration());
2418 RecordDecl
*getPreviousDeclaration() {
2419 return cast_or_null
<RecordDecl
>(TagDecl::getPreviousDeclaration());
2422 bool hasFlexibleArrayMember() const { return HasFlexibleArrayMember
; }
2423 void setHasFlexibleArrayMember(bool V
) { HasFlexibleArrayMember
= V
; }
2425 /// isAnonymousStructOrUnion - Whether this is an anonymous struct
2426 /// or union. To be an anonymous struct or union, it must have been
2427 /// declared without a name and there must be no objects of this
2428 /// type declared, e.g.,
2430 /// union { int i; float f; };
2432 /// is an anonymous union but neither of the following are:
2434 /// union X { int i; float f; };
2435 /// union { int i; float f; } obj;
2437 bool isAnonymousStructOrUnion() const { return AnonymousStructOrUnion
; }
2438 void setAnonymousStructOrUnion(bool Anon
) {
2439 AnonymousStructOrUnion
= Anon
;
2442 bool hasObjectMember() const { return HasObjectMember
; }
2443 void setHasObjectMember (bool val
) { HasObjectMember
= val
; }
2445 /// \brief Determines whether this declaration represents the
2446 /// injected class name.
2448 /// The injected class name in C++ is the name of the class that
2449 /// appears inside the class itself. For example:
2453 /// // C is implicitly declared here as a synonym for the class name.
2456 /// C::C c; // same as "C c;"
2458 bool isInjectedClassName() const;
2460 /// getDefinition - Returns the RecordDecl that actually defines this
2461 /// struct/union/class. When determining whether or not a struct/union/class
2462 /// is completely defined, one should use this method as opposed to
2463 /// 'isDefinition'. 'isDefinition' indicates whether or not a specific
2464 /// RecordDecl is defining declaration, not whether or not the record
2465 /// type is defined. This method returns NULL if there is no RecordDecl
2466 /// that defines the struct/union/tag.
2467 RecordDecl
* getDefinition() const {
2468 return cast_or_null
<RecordDecl
>(TagDecl::getDefinition());
2471 // Iterator access to field members. The field iterator only visits
2472 // the non-static data members of this class, ignoring any static
2473 // data members, functions, constructors, destructors, etc.
2474 typedef specific_decl_iterator
<FieldDecl
> field_iterator
;
2476 field_iterator
field_begin() const;
2478 field_iterator
field_end() const {
2479 return field_iterator(decl_iterator());
2482 // field_empty - Whether there are any fields (non-static data
2483 // members) in this record.
2484 bool field_empty() const {
2485 return field_begin() == field_end();
2488 /// completeDefinition - Notes that the definition of this type is
2490 virtual void completeDefinition();
2492 static bool classof(const Decl
*D
) { return classofKind(D
->getKind()); }
2493 static bool classof(const RecordDecl
*D
) { return true; }
2494 static bool classofKind(Kind K
) {
2495 return K
>= firstRecord
&& K
<= lastRecord
;
2499 /// \brief Deserialize just the fields.
2500 void LoadFieldsFromExternalStorage() const;
2503 class FileScopeAsmDecl
: public Decl
{
2504 StringLiteral
*AsmString
;
2505 FileScopeAsmDecl(DeclContext
*DC
, SourceLocation L
, StringLiteral
*asmstring
)
2506 : Decl(FileScopeAsm
, DC
, L
), AsmString(asmstring
) {}
2508 static FileScopeAsmDecl
*Create(ASTContext
&C
, DeclContext
*DC
,
2509 SourceLocation L
, StringLiteral
*Str
);
2511 const StringLiteral
*getAsmString() const { return AsmString
; }
2512 StringLiteral
*getAsmString() { return AsmString
; }
2513 void setAsmString(StringLiteral
*Asm
) { AsmString
= Asm
; }
2515 static bool classof(const Decl
*D
) { return classofKind(D
->getKind()); }
2516 static bool classof(const FileScopeAsmDecl
*D
) { return true; }
2517 static bool classofKind(Kind K
) { return K
== FileScopeAsm
; }
2520 /// BlockDecl - This represents a block literal declaration, which is like an
2521 /// unnamed FunctionDecl. For example:
2522 /// ^{ statement-body } or ^(int arg1, float arg2){ statement-body }
2524 class BlockDecl
: public Decl
, public DeclContext
{
2526 /// A class which contains all the information about a particular
2534 /// The variable being captured.
2535 llvm::PointerIntPair
<VarDecl
*, 2> VariableAndFlags
;
2537 /// The copy expression, expressed in terms of a DeclRef (or
2538 /// BlockDeclRef) to the captured variable. Only required if the
2539 /// variable has a C++ class type.
2543 Capture(VarDecl
*variable
, bool byRef
, bool nested
, Expr
*copy
)
2544 : VariableAndFlags(variable
,
2545 (byRef
? flag_isByRef
: 0) | (nested
? flag_isNested
: 0)),
2548 /// The variable being captured.
2549 VarDecl
*getVariable() const { return VariableAndFlags
.getPointer(); }
2551 /// Whether this is a "by ref" capture, i.e. a capture of a __block
2553 bool isByRef() const { return VariableAndFlags
.getInt() & flag_isByRef
; }
2555 /// Whether this is a nested capture, i.e. the variable captured
2556 /// is not from outside the immediately enclosing function/block.
2557 bool isNested() const { return VariableAndFlags
.getInt() & flag_isNested
; }
2559 bool hasCopyExpr() const { return CopyExpr
!= 0; }
2560 Expr
*getCopyExpr() const { return CopyExpr
; }
2561 void setCopyExpr(Expr
*e
) { CopyExpr
= e
; }
2565 // FIXME: This can be packed into the bitfields in Decl.
2566 bool IsVariadic
: 1;
2567 bool CapturesCXXThis
: 1;
2568 /// ParamInfo - new[]'d array of pointers to ParmVarDecls for the formal
2569 /// parameters of this function. This is null if a prototype or if there are
2571 ParmVarDecl
**ParamInfo
;
2575 TypeSourceInfo
*SignatureAsWritten
;
2578 unsigned NumCaptures
;
2581 BlockDecl(DeclContext
*DC
, SourceLocation CaretLoc
)
2582 : Decl(Block
, DC
, CaretLoc
), DeclContext(Block
),
2583 IsVariadic(false), CapturesCXXThis(false),
2584 ParamInfo(0), NumParams(0), Body(0),
2585 SignatureAsWritten(0), Captures(0), NumCaptures(0) {}
2588 static BlockDecl
*Create(ASTContext
&C
, DeclContext
*DC
, SourceLocation L
);
2590 SourceLocation
getCaretLocation() const { return getLocation(); }
2592 bool isVariadic() const { return IsVariadic
; }
2593 void setIsVariadic(bool value
) { IsVariadic
= value
; }
2595 CompoundStmt
*getCompoundBody() const { return (CompoundStmt
*) Body
; }
2596 Stmt
*getBody() const { return (Stmt
*) Body
; }
2597 void setBody(CompoundStmt
*B
) { Body
= (Stmt
*) B
; }
2599 void setSignatureAsWritten(TypeSourceInfo
*Sig
) { SignatureAsWritten
= Sig
; }
2600 TypeSourceInfo
*getSignatureAsWritten() const { return SignatureAsWritten
; }
2602 // Iterator access to formal parameters.
2603 unsigned param_size() const { return getNumParams(); }
2604 typedef ParmVarDecl
**param_iterator
;
2605 typedef ParmVarDecl
* const *param_const_iterator
;
2607 bool param_empty() const { return NumParams
== 0; }
2608 param_iterator
param_begin() { return ParamInfo
; }
2609 param_iterator
param_end() { return ParamInfo
+param_size(); }
2611 param_const_iterator
param_begin() const { return ParamInfo
; }
2612 param_const_iterator
param_end() const { return ParamInfo
+param_size(); }
2614 unsigned getNumParams() const { return NumParams
; }
2615 const ParmVarDecl
*getParamDecl(unsigned i
) const {
2616 assert(i
< getNumParams() && "Illegal param #");
2617 return ParamInfo
[i
];
2619 ParmVarDecl
*getParamDecl(unsigned i
) {
2620 assert(i
< getNumParams() && "Illegal param #");
2621 return ParamInfo
[i
];
2623 void setParams(ParmVarDecl
**NewParamInfo
, unsigned NumParams
);
2625 /// hasCaptures - True if this block (or its nested blocks) captures
2626 /// anything of local storage from its enclosing scopes.
2627 bool hasCaptures() const { return NumCaptures
!= 0 || CapturesCXXThis
; }
2629 /// getNumCaptures - Returns the number of captured variables.
2630 /// Does not include an entry for 'this'.
2631 unsigned getNumCaptures() const { return NumCaptures
; }
2633 typedef const Capture
*capture_iterator
;
2634 typedef const Capture
*capture_const_iterator
;
2635 capture_iterator
capture_begin() { return Captures
; }
2636 capture_iterator
capture_end() { return Captures
+ NumCaptures
; }
2637 capture_const_iterator
capture_begin() const { return Captures
; }
2638 capture_const_iterator
capture_end() const { return Captures
+ NumCaptures
; }
2640 bool capturesCXXThis() const { return CapturesCXXThis
; }
2642 void setCaptures(ASTContext
&Context
,
2643 const Capture
*begin
,
2645 bool capturesCXXThis
);
2647 SourceRange
getSourceRange() const;
2649 // Implement isa/cast/dyncast/etc.
2650 static bool classof(const Decl
*D
) { return classofKind(D
->getKind()); }
2651 static bool classof(const BlockDecl
*D
) { return true; }
2652 static bool classofKind(Kind K
) { return K
== Block
; }
2653 static DeclContext
*castToDeclContext(const BlockDecl
*D
) {
2654 return static_cast<DeclContext
*>(const_cast<BlockDecl
*>(D
));
2656 static BlockDecl
*castFromDeclContext(const DeclContext
*DC
) {
2657 return static_cast<BlockDecl
*>(const_cast<DeclContext
*>(DC
));
2661 /// Insertion operator for diagnostics. This allows sending NamedDecl's
2662 /// into a diagnostic with <<.
2663 inline const DiagnosticBuilder
&operator<<(const DiagnosticBuilder
&DB
,
2665 DB
.AddTaggedVal(reinterpret_cast<intptr_t>(ND
), Diagnostic::ak_nameddecl
);
2669 template<typename decl_type
>
2670 void Redeclarable
<decl_type
>::setPreviousDeclaration(decl_type
*PrevDecl
) {
2671 // Note: This routine is implemented here because we need both NamedDecl
2672 // and Redeclarable to be defined.
2677 // Point to previous. Make sure that this is actually the most recent
2678 // redeclaration, or we can build invalid chains. If the most recent
2679 // redeclaration is invalid, it won't be PrevDecl, but we want it anyway.
2680 RedeclLink
= PreviousDeclLink(llvm::cast
<decl_type
>(
2681 PrevDecl
->getMostRecentDeclaration()));
2682 First
= PrevDecl
->getFirstDeclaration();
2683 assert(First
->RedeclLink
.NextIsLatest() && "Expected first");
2686 First
= static_cast<decl_type
*>(this);
2689 // First one will point to this one as latest.
2690 First
->RedeclLink
= LatestDeclLink(static_cast<decl_type
*>(this));
2691 if (NamedDecl
*ND
= dyn_cast
<NamedDecl
>(static_cast<decl_type
*>(this)))
2692 ND
->ClearLinkageCache();
2695 } // end namespace clang