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