Decl.h revision 1eee0e753fb390b04848846e837714ec774b7bfd
1//===--- Decl.h - Classes for representing declarations ---------*- C++ -*-===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 10// This file defines the Decl subclasses. 11// 12//===----------------------------------------------------------------------===// 13 14#ifndef LLVM_CLANG_AST_DECL_H 15#define LLVM_CLANG_AST_DECL_H 16 17#include "clang/AST/DeclBase.h" 18#include "clang/AST/DeclarationName.h" 19#include "clang/AST/ExternalASTSource.h" 20 21namespace clang { 22class Expr; 23class FunctionTemplateDecl; 24class Stmt; 25class CompoundStmt; 26class StringLiteral; 27 28/// TranslationUnitDecl - The top declaration context. 29class TranslationUnitDecl : public Decl, public DeclContext { 30 TranslationUnitDecl() 31 : Decl(TranslationUnit, 0, SourceLocation()), 32 DeclContext(TranslationUnit) {} 33public: 34 static TranslationUnitDecl *Create(ASTContext &C); 35 // Implement isa/cast/dyncast/etc. 36 static bool classof(const Decl *D) { return D->getKind() == TranslationUnit; } 37 static bool classof(const TranslationUnitDecl *D) { return true; } 38 static DeclContext *castToDeclContext(const TranslationUnitDecl *D) { 39 return static_cast<DeclContext *>(const_cast<TranslationUnitDecl*>(D)); 40 } 41 static TranslationUnitDecl *castFromDeclContext(const DeclContext *DC) { 42 return static_cast<TranslationUnitDecl *>(const_cast<DeclContext*>(DC)); 43 } 44}; 45 46/// NamedDecl - This represents a decl with a name. Many decls have names such 47/// as ObjCMethodDecl, but not @class, etc. 48class NamedDecl : public Decl { 49 /// Name - The name of this declaration, which is typically a normal 50 /// identifier but may also be a special kind of name (C++ 51 /// constructor, Objective-C selector, etc.) 52 DeclarationName Name; 53 54protected: 55 NamedDecl(Kind DK, DeclContext *DC, SourceLocation L, DeclarationName N) 56 : Decl(DK, DC, L), Name(N) { } 57 58public: 59 /// getIdentifier - Get the identifier that names this declaration, 60 /// if there is one. This will return NULL if this declaration has 61 /// no name (e.g., for an unnamed class) or if the name is a special 62 /// name (C++ constructor, Objective-C selector, etc.). 63 IdentifierInfo *getIdentifier() const { return Name.getAsIdentifierInfo(); } 64 65 /// getNameAsCString - Get the name of identifier for this declaration as a 66 /// C string (const char*). This requires that the declaration have a name 67 /// and that it be a simple identifier. 68 const char *getNameAsCString() const { 69 assert(getIdentifier() && "Name is not a simple identifier"); 70 return getIdentifier()->getName(); 71 } 72 73 /// getDeclName - Get the actual, stored name of the declaration, 74 /// which may be a special name. 75 DeclarationName getDeclName() const { return Name; } 76 77 /// \brief Set the name of this declaration. 78 void setDeclName(DeclarationName N) { Name = N; } 79 80 /// getNameAsString - Get a human-readable name for the declaration, even if 81 /// it is one of the special kinds of names (C++ constructor, Objective-C 82 /// selector, etc). Creating this name requires expensive string 83 /// manipulation, so it should be called only when performance doesn't matter. 84 /// For simple declarations, getNameAsCString() should suffice. 85 std::string getNameAsString() const { return Name.getAsString(); } 86 87 /// getQualifiedNameAsString - Returns human-readable qualified name for 88 /// declaration, like A::B::i, for i being member of namespace A::B. 89 /// If declaration is not member of context which can be named (record, 90 /// namespace), it will return same result as getNameAsString(). 91 /// Creating this name is expensive, so it should be called only when 92 /// performance doesn't matter. 93 std::string getQualifiedNameAsString() const; 94 95 /// declarationReplaces - Determine whether this declaration, if 96 /// known to be well-formed within its context, will replace the 97 /// declaration OldD if introduced into scope. A declaration will 98 /// replace another declaration if, for example, it is a 99 /// redeclaration of the same variable or function, but not if it is 100 /// a declaration of a different kind (function vs. class) or an 101 /// overloaded function. 102 bool declarationReplaces(NamedDecl *OldD) const; 103 104 /// \brief Determine whether this declaration has linkage. 105 bool hasLinkage() const; 106 107 static bool classof(const Decl *D) { 108 return D->getKind() >= NamedFirst && D->getKind() <= NamedLast; 109 } 110 static bool classof(const NamedDecl *D) { return true; } 111}; 112 113/// NamespaceDecl - Represent a C++ namespace. 114class NamespaceDecl : public NamedDecl, public DeclContext { 115 SourceLocation LBracLoc, RBracLoc; 116 117 // For extended namespace definitions: 118 // 119 // namespace A { int x; } 120 // namespace A { int y; } 121 // 122 // there will be one NamespaceDecl for each declaration. 123 // NextNamespace points to the next extended declaration. 124 // OrigNamespace points to the original namespace declaration. 125 // OrigNamespace of the first namespace decl points to itself. 126 NamespaceDecl *OrigNamespace, *NextNamespace; 127 128 NamespaceDecl(DeclContext *DC, SourceLocation L, IdentifierInfo *Id) 129 : NamedDecl(Namespace, DC, L, Id), DeclContext(Namespace) { 130 OrigNamespace = this; 131 NextNamespace = 0; 132 } 133public: 134 static NamespaceDecl *Create(ASTContext &C, DeclContext *DC, 135 SourceLocation L, IdentifierInfo *Id); 136 137 virtual void Destroy(ASTContext& C); 138 139 NamespaceDecl *getNextNamespace() { return NextNamespace; } 140 const NamespaceDecl *getNextNamespace() const { return NextNamespace; } 141 void setNextNamespace(NamespaceDecl *ND) { NextNamespace = ND; } 142 143 NamespaceDecl *getOriginalNamespace() const { 144 return OrigNamespace; 145 } 146 void setOriginalNamespace(NamespaceDecl *ND) { OrigNamespace = ND; } 147 148 SourceRange getSourceRange() const { 149 return SourceRange(LBracLoc, RBracLoc); 150 } 151 152 SourceLocation getLBracLoc() const { return LBracLoc; } 153 SourceLocation getRBracLoc() const { return RBracLoc; } 154 void setLBracLoc(SourceLocation LBrace) { LBracLoc = LBrace; } 155 void setRBracLoc(SourceLocation RBrace) { RBracLoc = RBrace; } 156 157 // Implement isa/cast/dyncast/etc. 158 static bool classof(const Decl *D) { return D->getKind() == Namespace; } 159 static bool classof(const NamespaceDecl *D) { return true; } 160 static DeclContext *castToDeclContext(const NamespaceDecl *D) { 161 return static_cast<DeclContext *>(const_cast<NamespaceDecl*>(D)); 162 } 163 static NamespaceDecl *castFromDeclContext(const DeclContext *DC) { 164 return static_cast<NamespaceDecl *>(const_cast<DeclContext*>(DC)); 165 } 166}; 167 168/// ValueDecl - Represent the declaration of a variable (in which case it is 169/// an lvalue) a function (in which case it is a function designator) or 170/// an enum constant. 171class ValueDecl : public NamedDecl { 172 QualType DeclType; 173 174protected: 175 ValueDecl(Kind DK, DeclContext *DC, SourceLocation L, 176 DeclarationName N, QualType T) 177 : NamedDecl(DK, DC, L, N), DeclType(T) {} 178public: 179 QualType getType() const { return DeclType; } 180 void setType(QualType newType) { DeclType = newType; } 181 182 // Implement isa/cast/dyncast/etc. 183 static bool classof(const Decl *D) { 184 return D->getKind() >= ValueFirst && D->getKind() <= ValueLast; 185 } 186 static bool classof(const ValueDecl *D) { return true; } 187}; 188 189/// VarDecl - An instance of this class is created to represent a variable 190/// declaration or definition. 191class VarDecl : public ValueDecl { 192public: 193 enum StorageClass { 194 None, Auto, Register, Extern, Static, PrivateExtern 195 }; 196 197 /// getStorageClassSpecifierString - Return the string used to 198 /// specify the storage class \arg SC. 199 /// 200 /// It is illegal to call this function with SC == None. 201 static const char *getStorageClassSpecifierString(StorageClass SC); 202 203private: 204 Stmt *Init; 205 // FIXME: This can be packed into the bitfields in Decl. 206 unsigned SClass : 3; 207 bool ThreadSpecified : 1; 208 bool HasCXXDirectInit : 1; 209 210 /// DeclaredInCondition - Whether this variable was declared in a 211 /// condition, e.g., if (int x = foo()) { ... }. 212 bool DeclaredInCondition : 1; 213 214 /// \brief The previous declaration of this variable. 215 VarDecl *PreviousDeclaration; 216 217 // Move to DeclGroup when it is implemented. 218 SourceLocation TypeSpecStartLoc; 219 friend class StmtIteratorBase; 220protected: 221 VarDecl(Kind DK, DeclContext *DC, SourceLocation L, IdentifierInfo *Id, 222 QualType T, StorageClass SC, SourceLocation TSSL = SourceLocation()) 223 : ValueDecl(DK, DC, L, Id, T), Init(0), 224 ThreadSpecified(false), HasCXXDirectInit(false), 225 DeclaredInCondition(false), PreviousDeclaration(0), 226 TypeSpecStartLoc(TSSL) { 227 SClass = SC; 228 } 229public: 230 static VarDecl *Create(ASTContext &C, DeclContext *DC, 231 SourceLocation L, IdentifierInfo *Id, 232 QualType T, StorageClass S, 233 SourceLocation TypeSpecStartLoc = SourceLocation()); 234 235 virtual ~VarDecl(); 236 virtual void Destroy(ASTContext& C); 237 238 StorageClass getStorageClass() const { return (StorageClass)SClass; } 239 void setStorageClass(StorageClass SC) { SClass = SC; } 240 241 SourceLocation getTypeSpecStartLoc() const { return TypeSpecStartLoc; } 242 void setTypeSpecStartLoc(SourceLocation SL) { 243 TypeSpecStartLoc = SL; 244 } 245 246 const Expr *getInit() const { return (const Expr*) Init; } 247 Expr *getInit() { return (Expr*) Init; } 248 void setInit(Expr *I) { Init = (Stmt*) I; } 249 250 /// \brief Retrieve the definition of this variable, which may come 251 /// from a previous declaration. Def will be set to the VarDecl that 252 /// contains the initializer, and the result will be that 253 /// initializer. 254 const Expr *getDefinition(const VarDecl *&Def) const; 255 256 void setThreadSpecified(bool T) { ThreadSpecified = T; } 257 bool isThreadSpecified() const { 258 return ThreadSpecified; 259 } 260 261 void setCXXDirectInitializer(bool T) { HasCXXDirectInit = T; } 262 263 /// hasCXXDirectInitializer - If true, the initializer was a direct 264 /// initializer, e.g: "int x(1);". The Init expression will be the expression 265 /// inside the parens or a "ClassType(a,b,c)" class constructor expression for 266 /// class types. Clients can distinguish between "int x(1);" and "int x=1;" 267 /// by checking hasCXXDirectInitializer. 268 /// 269 bool hasCXXDirectInitializer() const { 270 return HasCXXDirectInit; 271 } 272 273 /// isDeclaredInCondition - Whether this variable was declared as 274 /// part of a condition in an if/switch/while statement, e.g., 275 /// @code 276 /// if (int x = foo()) { ... } 277 /// @endcode 278 bool isDeclaredInCondition() const { 279 return DeclaredInCondition; 280 } 281 void setDeclaredInCondition(bool InCondition) { 282 DeclaredInCondition = InCondition; 283 } 284 285 /// getPreviousDeclaration - Return the previous declaration of this 286 /// variable. 287 const VarDecl *getPreviousDeclaration() const { return PreviousDeclaration; } 288 289 void setPreviousDeclaration(VarDecl *PrevDecl) { 290 PreviousDeclaration = PrevDecl; 291 } 292 293 /// hasLocalStorage - Returns true if a variable with function scope 294 /// is a non-static local variable. 295 bool hasLocalStorage() const { 296 if (getStorageClass() == None) 297 return !isFileVarDecl(); 298 299 // Return true for: Auto, Register. 300 // Return false for: Extern, Static, PrivateExtern. 301 302 return getStorageClass() <= Register; 303 } 304 305 /// hasExternStorage - Returns true if a variable has extern or 306 /// __private_extern__ storage. 307 bool hasExternalStorage() const { 308 return getStorageClass() == Extern || getStorageClass() == PrivateExtern; 309 } 310 311 /// hasGlobalStorage - Returns true for all variables that do not 312 /// have local storage. This includs all global variables as well 313 /// as static variables declared within a function. 314 bool hasGlobalStorage() const { return !hasLocalStorage(); } 315 316 /// isBlockVarDecl - Returns true for local variable declarations. Note that 317 /// this includes static variables inside of functions. 318 /// 319 /// void foo() { int x; static int y; extern int z; } 320 /// 321 bool isBlockVarDecl() const { 322 if (getKind() != Decl::Var) 323 return false; 324 if (const DeclContext *DC = getDeclContext()) 325 return DC->getLookupContext()->isFunctionOrMethod(); 326 return false; 327 } 328 329 /// \brief Determines whether this is a static data member. 330 /// 331 /// This will only be true in C++, and applies to, e.g., the 332 /// variable 'x' in: 333 /// \code 334 /// struct S { 335 /// static int x; 336 /// }; 337 /// \endcode 338 bool isStaticDataMember() const { 339 return getDeclContext()->isRecord(); 340 } 341 342 /// isFileVarDecl - Returns true for file scoped variable declaration. 343 bool isFileVarDecl() const { 344 if (getKind() != Decl::Var) 345 return false; 346 if (const DeclContext *Ctx = getDeclContext()) { 347 Ctx = Ctx->getLookupContext(); 348 if (isa<TranslationUnitDecl>(Ctx) || isa<NamespaceDecl>(Ctx) ) 349 return true; 350 } 351 return false; 352 } 353 354 /// \brief Determine whether this is a tentative definition of a 355 /// variable in C. 356 bool isTentativeDefinition(ASTContext &Context) const; 357 358 /// \brief Determines whether this variable is a variable with 359 /// external, C linkage. 360 bool isExternC(ASTContext &Context) const; 361 362 // Implement isa/cast/dyncast/etc. 363 static bool classof(const Decl *D) { 364 return D->getKind() >= VarFirst && D->getKind() <= VarLast; 365 } 366 static bool classof(const VarDecl *D) { return true; } 367}; 368 369class ImplicitParamDecl : public VarDecl { 370protected: 371 ImplicitParamDecl(Kind DK, DeclContext *DC, SourceLocation L, 372 IdentifierInfo *Id, QualType Tw) 373 : VarDecl(DK, DC, L, Id, Tw, VarDecl::None) {} 374public: 375 static ImplicitParamDecl *Create(ASTContext &C, DeclContext *DC, 376 SourceLocation L, IdentifierInfo *Id, 377 QualType T); 378 // Implement isa/cast/dyncast/etc. 379 static bool classof(const ImplicitParamDecl *D) { return true; } 380 static bool classof(const Decl *D) { return D->getKind() == ImplicitParam; } 381}; 382 383/// ParmVarDecl - Represent a parameter to a function. 384class ParmVarDecl : public VarDecl { 385 // NOTE: VC++ treats enums as signed, avoid using the ObjCDeclQualifier enum 386 /// FIXME: Also can be paced into the bitfields in Decl. 387 /// in, inout, etc. 388 unsigned objcDeclQualifier : 6; 389 390 /// Default argument, if any. [C++ Only] 391 Expr *DefaultArg; 392protected: 393 ParmVarDecl(Kind DK, DeclContext *DC, SourceLocation L, 394 IdentifierInfo *Id, QualType T, StorageClass S, 395 Expr *DefArg) 396 : VarDecl(DK, DC, L, Id, T, S), 397 objcDeclQualifier(OBJC_TQ_None), DefaultArg(DefArg) {} 398 399public: 400 static ParmVarDecl *Create(ASTContext &C, DeclContext *DC, 401 SourceLocation L,IdentifierInfo *Id, 402 QualType T, StorageClass S, Expr *DefArg); 403 404 ObjCDeclQualifier getObjCDeclQualifier() const { 405 return ObjCDeclQualifier(objcDeclQualifier); 406 } 407 void setObjCDeclQualifier(ObjCDeclQualifier QTVal) { 408 objcDeclQualifier = QTVal; 409 } 410 411 const Expr *getDefaultArg() const { return DefaultArg; } 412 Expr *getDefaultArg() { return DefaultArg; } 413 void setDefaultArg(Expr *defarg) { DefaultArg = defarg; } 414 415 /// hasUnparsedDefaultArg - Determines whether this parameter has a 416 /// default argument that has not yet been parsed. This will occur 417 /// during the processing of a C++ class whose member functions have 418 /// default arguments, e.g., 419 /// @code 420 /// class X { 421 /// public: 422 /// void f(int x = 17); // x has an unparsed default argument now 423 /// }; // x has a regular default argument now 424 /// @endcode 425 bool hasUnparsedDefaultArg() const { 426 return DefaultArg == reinterpret_cast<Expr *>(-1); 427 } 428 429 /// setUnparsedDefaultArg - Specify that this parameter has an 430 /// unparsed default argument. The argument will be replaced with a 431 /// real default argument via setDefaultArg when the class 432 /// definition enclosing the function declaration that owns this 433 /// default argument is completed. 434 void setUnparsedDefaultArg() { DefaultArg = reinterpret_cast<Expr *>(-1); } 435 436 QualType getOriginalType() const; 437 438 /// setOwningFunction - Sets the function declaration that owns this 439 /// ParmVarDecl. Since ParmVarDecls are often created before the 440 /// FunctionDecls that own them, this routine is required to update 441 /// the DeclContext appropriately. 442 void setOwningFunction(DeclContext *FD) { setDeclContext(FD); } 443 444 // Implement isa/cast/dyncast/etc. 445 static bool classof(const Decl *D) { 446 return (D->getKind() == ParmVar || 447 D->getKind() == OriginalParmVar); 448 } 449 static bool classof(const ParmVarDecl *D) { return true; } 450}; 451 452/// OriginalParmVarDecl - Represent a parameter to a function, when 453/// the type of the parameter has been promoted. This node represents the 454/// parameter to the function with its original type. 455/// 456class OriginalParmVarDecl : public ParmVarDecl { 457 friend class ParmVarDecl; 458protected: 459 QualType OriginalType; 460private: 461 OriginalParmVarDecl(DeclContext *DC, SourceLocation L, 462 IdentifierInfo *Id, QualType T, 463 QualType OT, StorageClass S, 464 Expr *DefArg) 465 : ParmVarDecl(OriginalParmVar, DC, L, Id, T, S, DefArg), OriginalType(OT) {} 466public: 467 static OriginalParmVarDecl *Create(ASTContext &C, DeclContext *DC, 468 SourceLocation L,IdentifierInfo *Id, 469 QualType T, QualType OT, 470 StorageClass S, Expr *DefArg); 471 472 void setOriginalType(QualType T) { OriginalType = T; } 473 474 // Implement isa/cast/dyncast/etc. 475 static bool classof(const Decl *D) { return D->getKind() == OriginalParmVar; } 476 static bool classof(const OriginalParmVarDecl *D) { return true; } 477}; 478 479/// FunctionDecl - An instance of this class is created to represent a 480/// function declaration or definition. 481/// 482/// Since a given function can be declared several times in a program, 483/// there may be several FunctionDecls that correspond to that 484/// function. Only one of those FunctionDecls will be found when 485/// traversing the list of declarations in the context of the 486/// FunctionDecl (e.g., the translation unit); this FunctionDecl 487/// contains all of the information known about the function. Other, 488/// previous declarations of the function are available via the 489/// getPreviousDeclaration() chain. 490class FunctionDecl : public ValueDecl, public DeclContext { 491public: 492 enum StorageClass { 493 None, Extern, Static, PrivateExtern 494 }; 495private: 496 /// ParamInfo - new[]'d array of pointers to VarDecls for the formal 497 /// parameters of this function. This is null if a prototype or if there are 498 /// no formals. TODO: we could allocate this space immediately after the 499 /// FunctionDecl object to save an allocation like FunctionType does. 500 ParmVarDecl **ParamInfo; 501 502 LazyDeclStmtPtr Body; 503 504 /// PreviousDeclaration - A link to the previous declaration of this 505 /// same function, NULL if this is the first declaration. For 506 /// example, in the following code, the PreviousDeclaration can be 507 /// traversed several times to see all three declarations of the 508 /// function "f", the last of which is also a definition. 509 /// 510 /// int f(int x, int y = 1); 511 /// int f(int x = 0, int y); 512 /// int f(int x, int y) { return x + y; } 513 FunctionDecl *PreviousDeclaration; 514 515 // FIXME: This can be packed into the bitfields in Decl. 516 // NOTE: VC++ treats enums as signed, avoid using the StorageClass enum 517 unsigned SClass : 2; 518 bool IsInline : 1; 519 bool C99InlineDefinition : 1; 520 bool IsVirtual : 1; 521 bool IsPure : 1; 522 bool InheritedPrototype : 1; 523 bool HasPrototype : 1; 524 bool IsDeleted : 1; 525 526 // Move to DeclGroup when it is implemented. 527 SourceLocation TypeSpecStartLoc; 528 529 /// \brief The template or declaration that this declaration 530 /// describes or was instantiated from, respectively. 531 /// 532 /// For non-templates, this value will be NULL. For function 533 /// declarations that describe a function template, this will be a 534 /// pointer to a FunctionTemplateDecl. For member functions 535 /// of class template specializations, this will be the 536 /// FunctionDecl from which the member function was instantiated. 537 llvm::PointerUnion<FunctionTemplateDecl*, FunctionDecl*> 538 TemplateOrInstantiation; 539 540protected: 541 FunctionDecl(Kind DK, DeclContext *DC, SourceLocation L, 542 DeclarationName N, QualType T, 543 StorageClass S, bool isInline, 544 SourceLocation TSSL = SourceLocation()) 545 : ValueDecl(DK, DC, L, N, T), 546 DeclContext(DK), 547 ParamInfo(0), Body(), PreviousDeclaration(0), 548 SClass(S), IsInline(isInline), C99InlineDefinition(false), 549 IsVirtual(false), IsPure(false), InheritedPrototype(false), 550 HasPrototype(true), IsDeleted(false), TypeSpecStartLoc(TSSL), 551 TemplateOrInstantiation() {} 552 553 virtual ~FunctionDecl() {} 554 virtual void Destroy(ASTContext& C); 555 556public: 557 static FunctionDecl *Create(ASTContext &C, DeclContext *DC, SourceLocation L, 558 DeclarationName N, QualType T, 559 StorageClass S = None, bool isInline = false, 560 bool hasPrototype = true, 561 SourceLocation TSStartLoc = SourceLocation()); 562 563 SourceLocation getTypeSpecStartLoc() const { return TypeSpecStartLoc; } 564 void setTypeSpecStartLoc(SourceLocation TS) { TypeSpecStartLoc = TS; } 565 566 /// getBody - Retrieve the body (definition) of the function. The 567 /// function body might be in any of the (re-)declarations of this 568 /// function. The variant that accepts a FunctionDecl pointer will 569 /// set that function declaration to the actual declaration 570 /// containing the body (if there is one). 571 Stmt *getBody(ASTContext &Context, const FunctionDecl *&Definition) const; 572 573 virtual Stmt *getBody(ASTContext &Context) const { 574 const FunctionDecl* Definition; 575 return getBody(Context, Definition); 576 } 577 578 /// \brief If the function has a body that is immediately available, 579 /// return it. 580 Stmt *getBodyIfAvailable() const; 581 582 /// isThisDeclarationADefinition - Returns whether this specific 583 /// declaration of the function is also a definition. This does not 584 /// determine whether the function has been defined (e.g., in a 585 /// previous definition); for that information, use getBody. 586 /// FIXME: Should return true if function is deleted or defaulted. However, 587 /// CodeGenModule.cpp uses it, and I don't know if this would break it. 588 bool isThisDeclarationADefinition() const { return Body; } 589 590 void setBody(Stmt *B) { Body = B; } 591 void setLazyBody(uint64_t Offset) { Body = Offset; } 592 593 /// Whether this function is virtual, either by explicit marking, or by 594 /// overriding a virtual function. Only valid on C++ member functions. 595 bool isVirtual() { return IsVirtual; } 596 void setVirtual(bool V = true) { IsVirtual = V; } 597 598 /// Whether this virtual function is pure, i.e. makes the containing class 599 /// abstract. 600 bool isPure() const { return IsPure; } 601 void setPure(bool P = true) { IsPure = P; } 602 603 /// \brief Whether this function has a prototype, either because one 604 /// was explicitly written or because it was "inherited" by merging 605 /// a declaration without a prototype with a declaration that has a 606 /// prototype. 607 bool hasPrototype() const { return HasPrototype || InheritedPrototype; } 608 void setHasPrototype(bool P) { HasPrototype = P; } 609 610 /// \brief Whether this function inherited its prototype from a 611 /// previous declaration. 612 bool inheritedPrototype() const { return InheritedPrototype; } 613 void setInheritedPrototype(bool P = true) { InheritedPrototype = P; } 614 615 /// \brief Whether this function has been deleted. 616 /// 617 /// A function that is "deleted" (via the C++0x "= delete" syntax) 618 /// acts like a normal function, except that it cannot actually be 619 /// called or have its address taken. Deleted functions are 620 /// typically used in C++ overload resolution to attract arguments 621 /// whose type or lvalue/rvalue-ness would permit the use of a 622 /// different overload that would behave incorrectly. For example, 623 /// one might use deleted functions to ban implicit conversion from 624 /// a floating-point number to an Integer type: 625 /// 626 /// @code 627 /// struct Integer { 628 /// Integer(long); // construct from a long 629 /// Integer(double) = delete; // no construction from float or double 630 /// Integer(long double) = delete; // no construction from long double 631 /// }; 632 /// @endcode 633 bool isDeleted() const { return IsDeleted; } 634 void setDeleted(bool D = true) { IsDeleted = D; } 635 636 /// \brief Determines whether this is a function "main", which is 637 /// the entry point into an executable program. 638 bool isMain() const; 639 640 /// \brief Determines whether this function is a function with 641 /// external, C linkage. 642 bool isExternC(ASTContext &Context) const; 643 644 /// \brief Determines whether this is a global function. 645 bool isGlobal() const; 646 647 /// getPreviousDeclaration - Return the previous declaration of this 648 /// function. 649 const FunctionDecl *getPreviousDeclaration() const { 650 return PreviousDeclaration; 651 } 652 653 void setPreviousDeclaration(FunctionDecl * PrevDecl) { 654 PreviousDeclaration = PrevDecl; 655 } 656 657 unsigned getBuiltinID(ASTContext &Context) const; 658 659 unsigned getNumParmVarDeclsFromType() const; 660 661 // Iterator access to formal parameters. 662 unsigned param_size() const { return getNumParams(); } 663 typedef ParmVarDecl **param_iterator; 664 typedef ParmVarDecl * const *param_const_iterator; 665 666 param_iterator param_begin() { return ParamInfo; } 667 param_iterator param_end() { return ParamInfo+param_size(); } 668 669 param_const_iterator param_begin() const { return ParamInfo; } 670 param_const_iterator param_end() const { return ParamInfo+param_size(); } 671 672 /// getNumParams - Return the number of parameters this function must have 673 /// based on its functiontype. This is the length of the PararmInfo array 674 /// after it has been created. 675 unsigned getNumParams() const; 676 677 const ParmVarDecl *getParamDecl(unsigned i) const { 678 assert(i < getNumParams() && "Illegal param #"); 679 return ParamInfo[i]; 680 } 681 ParmVarDecl *getParamDecl(unsigned i) { 682 assert(i < getNumParams() && "Illegal param #"); 683 return ParamInfo[i]; 684 } 685 void setParams(ASTContext& C, ParmVarDecl **NewParamInfo, unsigned NumParams); 686 687 /// getMinRequiredArguments - Returns the minimum number of arguments 688 /// needed to call this function. This may be fewer than the number of 689 /// function parameters, if some of the parameters have default 690 /// arguments (in C++). 691 unsigned getMinRequiredArguments() const; 692 693 QualType getResultType() const { 694 return getType()->getAsFunctionType()->getResultType(); 695 } 696 StorageClass getStorageClass() const { return StorageClass(SClass); } 697 void setStorageClass(StorageClass SC) { SClass = SC; } 698 699 bool isInline() const { return IsInline; } 700 void setInline(bool I) { IsInline = I; } 701 702 /// \brief Whether this function is an "inline definition" as 703 /// defined by C99. 704 bool isC99InlineDefinition() const { return C99InlineDefinition; } 705 void setC99InlineDefinition(bool I) { C99InlineDefinition = I; } 706 707 /// \brief Determines whether this function has a gnu_inline 708 /// attribute that affects its semantics. 709 /// 710 /// The gnu_inline attribute only introduces GNU inline semantics 711 /// when all of the inline declarations of the function are marked 712 /// gnu_inline. 713 bool hasActiveGNUInlineAttribute() const; 714 715 /// \brief Determines whether this function is a GNU "extern 716 /// inline", which is roughly the opposite of a C99 "extern inline" 717 /// function. 718 bool isExternGNUInline() const; 719 720 /// isOverloadedOperator - Whether this function declaration 721 /// represents an C++ overloaded operator, e.g., "operator+". 722 bool isOverloadedOperator() const { 723 return getOverloadedOperator() != OO_None; 724 }; 725 726 OverloadedOperatorKind getOverloadedOperator() const; 727 728 /// \brief If this function is an instantiation of a member function 729 /// of a class template specialization, retrieves the function from 730 /// which it was instantiated. 731 /// 732 /// This routine will return non-NULL for (non-templated) member 733 /// functions of class templates and for instantiations of function 734 /// templates. For example, given: 735 /// 736 /// \code 737 /// template<typename T> 738 /// struct X { 739 /// void f(T); 740 /// }; 741 /// \endcode 742 /// 743 /// The declaration for X<int>::f is a (non-templated) FunctionDecl 744 /// whose parent is the class template specialization X<int>. For 745 /// this declaration, getInstantiatedFromFunction() will return 746 /// the FunctionDecl X<T>::A. When a complete definition of 747 /// X<int>::A is required, it will be instantiated from the 748 /// declaration returned by getInstantiatedFromMemberFunction(). 749 FunctionDecl *getInstantiatedFromMemberFunction() const { 750 return TemplateOrInstantiation.dyn_cast<FunctionDecl*>(); 751 } 752 753 /// \brief Specify that this record is an instantiation of the 754 /// member function RD. 755 void setInstantiationOfMemberFunction(FunctionDecl *RD) { 756 TemplateOrInstantiation = RD; 757 } 758 759 /// \brief Retrieves the function template that is described by this 760 /// function declaration. 761 /// 762 /// Every function template is represented as a FunctionTemplateDecl 763 /// and a FunctionDecl (or something derived from FunctionDecl). The 764 /// former contains template properties (such as the template 765 /// parameter lists) while the latter contains the actual 766 /// description of the template's 767 /// contents. FunctionTemplateDecl::getTemplatedDecl() retrieves the 768 /// FunctionDecl that describes the function template, 769 /// getDescribedFunctionTemplate() retrieves the 770 /// FunctionTemplateDecl from a FunctionDecl. 771 FunctionTemplateDecl *getDescribedFunctionTemplate() const { 772 return TemplateOrInstantiation.dyn_cast<FunctionTemplateDecl*>(); 773 } 774 775 void setDescribedFunctionTemplate(FunctionTemplateDecl *Template) { 776 TemplateOrInstantiation = Template; 777 } 778 779 // Implement isa/cast/dyncast/etc. 780 static bool classof(const Decl *D) { 781 return D->getKind() >= FunctionFirst && D->getKind() <= FunctionLast; 782 } 783 static bool classof(const FunctionDecl *D) { return true; } 784 static DeclContext *castToDeclContext(const FunctionDecl *D) { 785 return static_cast<DeclContext *>(const_cast<FunctionDecl*>(D)); 786 } 787 static FunctionDecl *castFromDeclContext(const DeclContext *DC) { 788 return static_cast<FunctionDecl *>(const_cast<DeclContext*>(DC)); 789 } 790}; 791 792 793/// FieldDecl - An instance of this class is created by Sema::ActOnField to 794/// represent a member of a struct/union/class. 795class FieldDecl : public ValueDecl { 796 // FIXME: This can be packed into the bitfields in Decl. 797 bool Mutable : 1; 798 Expr *BitWidth; 799protected: 800 FieldDecl(Kind DK, DeclContext *DC, SourceLocation L, 801 IdentifierInfo *Id, QualType T, Expr *BW, bool Mutable) 802 : ValueDecl(DK, DC, L, Id, T), Mutable(Mutable), BitWidth(BW) 803 { } 804 805public: 806 static FieldDecl *Create(ASTContext &C, DeclContext *DC, SourceLocation L, 807 IdentifierInfo *Id, QualType T, Expr *BW, 808 bool Mutable); 809 810 /// isMutable - Determines whether this field is mutable (C++ only). 811 bool isMutable() const { return Mutable; } 812 813 /// \brief Set whether this field is mutable (C++ only). 814 void setMutable(bool M) { Mutable = M; } 815 816 /// isBitfield - Determines whether this field is a bitfield. 817 bool isBitField() const { return BitWidth != NULL; } 818 819 /// @brief Determines whether this is an unnamed bitfield. 820 bool isUnnamedBitfield() const { return BitWidth != NULL && !getDeclName(); } 821 822 /// isAnonymousStructOrUnion - Determines whether this field is a 823 /// representative for an anonymous struct or union. Such fields are 824 /// unnamed and are implicitly generated by the implementation to 825 /// store the data for the anonymous union or struct. 826 bool isAnonymousStructOrUnion() const; 827 828 Expr *getBitWidth() const { return BitWidth; } 829 void setBitWidth(Expr *BW) { BitWidth = BW; } 830 831 // Implement isa/cast/dyncast/etc. 832 static bool classof(const Decl *D) { 833 return D->getKind() >= FieldFirst && D->getKind() <= FieldLast; 834 } 835 static bool classof(const FieldDecl *D) { return true; } 836}; 837 838/// EnumConstantDecl - An instance of this object exists for each enum constant 839/// that is defined. For example, in "enum X {a,b}", each of a/b are 840/// EnumConstantDecl's, X is an instance of EnumDecl, and the type of a/b is a 841/// TagType for the X EnumDecl. 842class EnumConstantDecl : public ValueDecl { 843 Stmt *Init; // an integer constant expression 844 llvm::APSInt Val; // The value. 845protected: 846 EnumConstantDecl(DeclContext *DC, SourceLocation L, 847 IdentifierInfo *Id, QualType T, Expr *E, 848 const llvm::APSInt &V) 849 : ValueDecl(EnumConstant, DC, L, Id, T), Init((Stmt*)E), Val(V) {} 850 851 virtual ~EnumConstantDecl() {} 852public: 853 854 static EnumConstantDecl *Create(ASTContext &C, EnumDecl *DC, 855 SourceLocation L, IdentifierInfo *Id, 856 QualType T, Expr *E, 857 const llvm::APSInt &V); 858 859 virtual void Destroy(ASTContext& C); 860 861 const Expr *getInitExpr() const { return (const Expr*) Init; } 862 Expr *getInitExpr() { return (Expr*) Init; } 863 const llvm::APSInt &getInitVal() const { return Val; } 864 865 void setInitExpr(Expr *E) { Init = (Stmt*) E; } 866 void setInitVal(const llvm::APSInt &V) { Val = V; } 867 868 // Implement isa/cast/dyncast/etc. 869 static bool classof(const Decl *D) { return D->getKind() == EnumConstant; } 870 static bool classof(const EnumConstantDecl *D) { return true; } 871 872 friend class StmtIteratorBase; 873}; 874 875 876/// TypeDecl - Represents a declaration of a type. 877/// 878class TypeDecl : public NamedDecl { 879 /// TypeForDecl - This indicates the Type object that represents 880 /// this TypeDecl. It is a cache maintained by 881 /// ASTContext::getTypedefType, ASTContext::getTagDeclType, and 882 /// ASTContext::getTemplateTypeParmType, and TemplateTypeParmDecl. 883 mutable Type *TypeForDecl; 884 friend class ASTContext; 885 friend class DeclContext; 886 friend class TagDecl; 887 friend class TemplateTypeParmDecl; 888 friend class ClassTemplateSpecializationDecl; 889 friend class TagType; 890 891protected: 892 TypeDecl(Kind DK, DeclContext *DC, SourceLocation L, 893 IdentifierInfo *Id) 894 : NamedDecl(DK, DC, L, Id), TypeForDecl(0) {} 895 896public: 897 // Low-level accessor 898 Type *getTypeForDecl() const { return TypeForDecl; } 899 void setTypeForDecl(Type *TD) { TypeForDecl = TD; } 900 901 // Implement isa/cast/dyncast/etc. 902 static bool classof(const Decl *D) { 903 return D->getKind() >= TypeFirst && D->getKind() <= TypeLast; 904 } 905 static bool classof(const TypeDecl *D) { return true; } 906}; 907 908 909class TypedefDecl : public TypeDecl { 910 /// UnderlyingType - This is the type the typedef is set to. 911 QualType UnderlyingType; 912 TypedefDecl(DeclContext *DC, SourceLocation L, 913 IdentifierInfo *Id, QualType T) 914 : TypeDecl(Typedef, DC, L, Id), UnderlyingType(T) {} 915 916 virtual ~TypedefDecl() {} 917public: 918 919 static TypedefDecl *Create(ASTContext &C, DeclContext *DC, 920 SourceLocation L,IdentifierInfo *Id, 921 QualType T); 922 923 QualType getUnderlyingType() const { return UnderlyingType; } 924 void setUnderlyingType(QualType newType) { UnderlyingType = newType; } 925 926 // Implement isa/cast/dyncast/etc. 927 static bool classof(const Decl *D) { return D->getKind() == Typedef; } 928 static bool classof(const TypedefDecl *D) { return true; } 929}; 930 931class TypedefDecl; 932 933/// TagDecl - Represents the declaration of a struct/union/class/enum. 934class TagDecl : public TypeDecl, public DeclContext { 935public: 936 enum TagKind { 937 TK_struct, 938 TK_union, 939 TK_class, 940 TK_enum 941 }; 942 943private: 944 // FIXME: This can be packed into the bitfields in Decl. 945 /// TagDeclKind - The TagKind enum. 946 unsigned TagDeclKind : 2; 947 948 /// IsDefinition - True if this is a definition ("struct foo {};"), false if 949 /// it is a declaration ("struct foo;"). 950 bool IsDefinition : 1; 951 952 /// TypedefForAnonDecl - If a TagDecl is anonymous and part of a typedef, 953 /// this points to the TypedefDecl. Used for mangling. 954 TypedefDecl *TypedefForAnonDecl; 955 956protected: 957 TagDecl(Kind DK, TagKind TK, DeclContext *DC, SourceLocation L, 958 IdentifierInfo *Id) 959 : TypeDecl(DK, DC, L, Id), DeclContext(DK), TypedefForAnonDecl(0) { 960 assert((DK != Enum || TK == TK_enum) &&"EnumDecl not matched with TK_enum"); 961 TagDeclKind = TK; 962 IsDefinition = false; 963 } 964public: 965 966 /// isDefinition - Return true if this decl has its body specified. 967 bool isDefinition() const { 968 return IsDefinition; 969 } 970 971 /// \brief Whether this declaration declares a type that is 972 /// dependent, i.e., a type that somehow depends on template 973 /// parameters. 974 bool isDependentType() const; 975 976 /// @brief Starts the definition of this tag declaration. 977 /// 978 /// This method should be invoked at the beginning of the definition 979 /// of this tag declaration. It will set the tag type into a state 980 /// where it is in the process of being defined. 981 void startDefinition(); 982 983 /// @brief Completes the definition of this tag declaration. 984 void completeDefinition(); 985 986 /// getDefinition - Returns the TagDecl that actually defines this 987 /// struct/union/class/enum. When determining whether or not a 988 /// struct/union/class/enum is completely defined, one should use this method 989 /// as opposed to 'isDefinition'. 'isDefinition' indicates whether or not a 990 /// specific TagDecl is defining declaration, not whether or not the 991 /// struct/union/class/enum type is defined. This method returns NULL if 992 /// there is no TagDecl that defines the struct/union/class/enum. 993 TagDecl* getDefinition(ASTContext& C) const; 994 995 const char *getKindName() const { 996 switch (getTagKind()) { 997 default: assert(0 && "Unknown TagKind!"); 998 case TK_struct: return "struct"; 999 case TK_union: return "union"; 1000 case TK_class: return "class"; 1001 case TK_enum: return "enum"; 1002 } 1003 } 1004 1005 TagKind getTagKind() const { 1006 return TagKind(TagDeclKind); 1007 } 1008 1009 void setTagKind(TagKind TK) { TagDeclKind = TK; } 1010 1011 bool isStruct() const { return getTagKind() == TK_struct; } 1012 bool isClass() const { return getTagKind() == TK_class; } 1013 bool isUnion() const { return getTagKind() == TK_union; } 1014 bool isEnum() const { return getTagKind() == TK_enum; } 1015 1016 TypedefDecl *getTypedefForAnonDecl() const { return TypedefForAnonDecl; } 1017 void setTypedefForAnonDecl(TypedefDecl *TDD) { TypedefForAnonDecl = TDD; } 1018 1019 // Implement isa/cast/dyncast/etc. 1020 static bool classof(const Decl *D) { 1021 return D->getKind() >= TagFirst && D->getKind() <= TagLast; 1022 } 1023 static bool classof(const TagDecl *D) { return true; } 1024 1025 static DeclContext *castToDeclContext(const TagDecl *D) { 1026 return static_cast<DeclContext *>(const_cast<TagDecl*>(D)); 1027 } 1028 static TagDecl *castFromDeclContext(const DeclContext *DC) { 1029 return static_cast<TagDecl *>(const_cast<DeclContext*>(DC)); 1030 } 1031 1032 void setDefinition(bool V) { IsDefinition = V; } 1033}; 1034 1035/// EnumDecl - Represents an enum. As an extension, we allow forward-declared 1036/// enums. 1037class EnumDecl : public TagDecl { 1038 /// IntegerType - This represent the integer type that the enum corresponds 1039 /// to for code generation purposes. Note that the enumerator constants may 1040 /// have a different type than this does. 1041 QualType IntegerType; 1042 1043 EnumDecl(DeclContext *DC, SourceLocation L, 1044 IdentifierInfo *Id) 1045 : TagDecl(Enum, TK_enum, DC, L, Id) { 1046 IntegerType = QualType(); 1047 } 1048public: 1049 static EnumDecl *Create(ASTContext &C, DeclContext *DC, 1050 SourceLocation L, IdentifierInfo *Id, 1051 EnumDecl *PrevDecl); 1052 1053 virtual void Destroy(ASTContext& C); 1054 1055 /// completeDefinition - When created, the EnumDecl corresponds to a 1056 /// forward-declared enum. This method is used to mark the 1057 /// declaration as being defined; it's enumerators have already been 1058 /// added (via DeclContext::addDecl). NewType is the new underlying 1059 /// type of the enumeration type. 1060 void completeDefinition(ASTContext &C, QualType NewType); 1061 1062 // enumerator_iterator - Iterates through the enumerators of this 1063 // enumeration. 1064 typedef specific_decl_iterator<EnumConstantDecl> enumerator_iterator; 1065 1066 enumerator_iterator enumerator_begin(ASTContext &Context) const { 1067 return enumerator_iterator(this->decls_begin(Context)); 1068 } 1069 1070 enumerator_iterator enumerator_end(ASTContext &Context) const { 1071 return enumerator_iterator(this->decls_end(Context)); 1072 } 1073 1074 /// getIntegerType - Return the integer type this enum decl corresponds to. 1075 /// This returns a null qualtype for an enum forward definition. 1076 QualType getIntegerType() const { return IntegerType; } 1077 1078 /// \brief Set the underlying integer type. 1079 void setIntegerType(QualType T) { IntegerType = T; } 1080 1081 static bool classof(const Decl *D) { return D->getKind() == Enum; } 1082 static bool classof(const EnumDecl *D) { return true; } 1083}; 1084 1085 1086/// RecordDecl - Represents a struct/union/class. For example: 1087/// struct X; // Forward declaration, no "body". 1088/// union Y { int A, B; }; // Has body with members A and B (FieldDecls). 1089/// This decl will be marked invalid if *any* members are invalid. 1090/// 1091class RecordDecl : public TagDecl { 1092 // FIXME: This can be packed into the bitfields in Decl. 1093 /// HasFlexibleArrayMember - This is true if this struct ends with a flexible 1094 /// array member (e.g. int X[]) or if this union contains a struct that does. 1095 /// If so, this cannot be contained in arrays or other structs as a member. 1096 bool HasFlexibleArrayMember : 1; 1097 1098 /// AnonymousStructOrUnion - Whether this is the type of an 1099 /// anonymous struct or union. 1100 bool AnonymousStructOrUnion : 1; 1101 1102protected: 1103 RecordDecl(Kind DK, TagKind TK, DeclContext *DC, 1104 SourceLocation L, IdentifierInfo *Id); 1105 virtual ~RecordDecl(); 1106 1107public: 1108 static RecordDecl *Create(ASTContext &C, TagKind TK, DeclContext *DC, 1109 SourceLocation L, IdentifierInfo *Id, 1110 RecordDecl* PrevDecl = 0); 1111 1112 virtual void Destroy(ASTContext& C); 1113 1114 bool hasFlexibleArrayMember() const { return HasFlexibleArrayMember; } 1115 void setHasFlexibleArrayMember(bool V) { HasFlexibleArrayMember = V; } 1116 1117 /// isAnonymousStructOrUnion - Whether this is an anonymous struct 1118 /// or union. To be an anonymous struct or union, it must have been 1119 /// declared without a name and there must be no objects of this 1120 /// type declared, e.g., 1121 /// @code 1122 /// union { int i; float f; }; 1123 /// @endcode 1124 /// is an anonymous union but neither of the following are: 1125 /// @code 1126 /// union X { int i; float f; }; 1127 /// union { int i; float f; } obj; 1128 /// @endcode 1129 bool isAnonymousStructOrUnion() const { return AnonymousStructOrUnion; } 1130 void setAnonymousStructOrUnion(bool Anon) { 1131 AnonymousStructOrUnion = Anon; 1132 } 1133 1134 /// \brief Determines whether this declaration represents the 1135 /// injected class name. 1136 /// 1137 /// The injected class name in C++ is the name of the class that 1138 /// appears inside the class itself. For example: 1139 /// 1140 /// \code 1141 /// struct C { 1142 /// // C is implicitly declared here as a synonym for the class name. 1143 /// }; 1144 /// 1145 /// C::C c; // same as "C c;" 1146 /// \endcode 1147 bool isInjectedClassName() const; 1148 1149 /// getDefinition - Returns the RecordDecl that actually defines this 1150 /// struct/union/class. When determining whether or not a struct/union/class 1151 /// is completely defined, one should use this method as opposed to 1152 /// 'isDefinition'. 'isDefinition' indicates whether or not a specific 1153 /// RecordDecl is defining declaration, not whether or not the record 1154 /// type is defined. This method returns NULL if there is no RecordDecl 1155 /// that defines the struct/union/tag. 1156 RecordDecl* getDefinition(ASTContext& C) const { 1157 return cast_or_null<RecordDecl>(TagDecl::getDefinition(C)); 1158 } 1159 1160 // Iterator access to field members. The field iterator only visits 1161 // the non-static data members of this class, ignoring any static 1162 // data members, functions, constructors, destructors, etc. 1163 typedef specific_decl_iterator<FieldDecl> field_iterator; 1164 1165 field_iterator field_begin(ASTContext &Context) const { 1166 return field_iterator(decls_begin(Context)); 1167 } 1168 field_iterator field_end(ASTContext &Context) const { 1169 return field_iterator(decls_end(Context)); 1170 } 1171 1172 // field_empty - Whether there are any fields (non-static data 1173 // members) in this record. 1174 bool field_empty(ASTContext &Context) const { 1175 return field_begin(Context) == field_end(Context); 1176 } 1177 1178 /// completeDefinition - Notes that the definition of this type is 1179 /// now complete. 1180 void completeDefinition(ASTContext& C); 1181 1182 static bool classof(const Decl *D) { 1183 return D->getKind() >= RecordFirst && D->getKind() <= RecordLast; 1184 } 1185 static bool classof(const RecordDecl *D) { return true; } 1186}; 1187 1188class FileScopeAsmDecl : public Decl { 1189 StringLiteral *AsmString; 1190 FileScopeAsmDecl(DeclContext *DC, SourceLocation L, StringLiteral *asmstring) 1191 : Decl(FileScopeAsm, DC, L), AsmString(asmstring) {} 1192public: 1193 static FileScopeAsmDecl *Create(ASTContext &C, DeclContext *DC, 1194 SourceLocation L, StringLiteral *Str); 1195 1196 const StringLiteral *getAsmString() const { return AsmString; } 1197 StringLiteral *getAsmString() { return AsmString; } 1198 void setAsmString(StringLiteral *Asm) { AsmString = Asm; } 1199 1200 static bool classof(const Decl *D) { 1201 return D->getKind() == FileScopeAsm; 1202 } 1203 static bool classof(const FileScopeAsmDecl *D) { return true; } 1204}; 1205 1206/// BlockDecl - This represents a block literal declaration, which is like an 1207/// unnamed FunctionDecl. For example: 1208/// ^{ statement-body } or ^(int arg1, float arg2){ statement-body } 1209/// 1210class BlockDecl : public Decl, public DeclContext { 1211 /// ParamInfo - new[]'d array of pointers to ParmVarDecls for the formal 1212 /// parameters of this function. This is null if a prototype or if there are 1213 /// no formals. 1214 ParmVarDecl **ParamInfo; 1215 unsigned NumParams; 1216 1217 Stmt *Body; 1218 1219protected: 1220 BlockDecl(DeclContext *DC, SourceLocation CaretLoc) 1221 : Decl(Block, DC, CaretLoc), DeclContext(Block), 1222 ParamInfo(0), NumParams(0), Body(0) {} 1223 1224 virtual ~BlockDecl(); 1225 virtual void Destroy(ASTContext& C); 1226 1227public: 1228 static BlockDecl *Create(ASTContext &C, DeclContext *DC, SourceLocation L); 1229 1230 SourceLocation getCaretLocation() const { return getLocation(); } 1231 1232 CompoundStmt *getBody() const { return (CompoundStmt*) Body; } 1233 Stmt *getBody(ASTContext &C) const { return (Stmt*) Body; } 1234 void setBody(CompoundStmt *B) { Body = (Stmt*) B; } 1235 1236 // Iterator access to formal parameters. 1237 unsigned param_size() const { return getNumParams(); } 1238 typedef ParmVarDecl **param_iterator; 1239 typedef ParmVarDecl * const *param_const_iterator; 1240 1241 bool param_empty() const { return NumParams == 0; } 1242 param_iterator param_begin() { return ParamInfo; } 1243 param_iterator param_end() { return ParamInfo+param_size(); } 1244 1245 param_const_iterator param_begin() const { return ParamInfo; } 1246 param_const_iterator param_end() const { return ParamInfo+param_size(); } 1247 1248 unsigned getNumParams() const; 1249 const ParmVarDecl *getParamDecl(unsigned i) const { 1250 assert(i < getNumParams() && "Illegal param #"); 1251 return ParamInfo[i]; 1252 } 1253 ParmVarDecl *getParamDecl(unsigned i) { 1254 assert(i < getNumParams() && "Illegal param #"); 1255 return ParamInfo[i]; 1256 } 1257 void setParams(ASTContext& C, ParmVarDecl **NewParamInfo, unsigned NumParams); 1258 1259 // Implement isa/cast/dyncast/etc. 1260 static bool classof(const Decl *D) { return D->getKind() == Block; } 1261 static bool classof(const BlockDecl *D) { return true; } 1262 static DeclContext *castToDeclContext(const BlockDecl *D) { 1263 return static_cast<DeclContext *>(const_cast<BlockDecl*>(D)); 1264 } 1265 static BlockDecl *castFromDeclContext(const DeclContext *DC) { 1266 return static_cast<BlockDecl *>(const_cast<DeclContext*>(DC)); 1267 } 1268}; 1269 1270/// Insertion operator for diagnostics. This allows sending NamedDecl's 1271/// into a diagnostic with <<. 1272inline const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB, 1273 NamedDecl* ND) { 1274 DB.AddTaggedVal(reinterpret_cast<intptr_t>(ND), Diagnostic::ak_nameddecl); 1275 return DB; 1276} 1277 1278} // end namespace clang 1279 1280#endif 1281