Decl.h revision 127102b5196ffe04bdb70fd553fe62c265ab10a9
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 814 unsigned getBuiltinID(ASTContext &Context) const; 815 816 unsigned getNumParmVarDeclsFromType() const; 817 818 // Iterator access to formal parameters. 819 unsigned param_size() const { return getNumParams(); } 820 typedef ParmVarDecl **param_iterator; 821 typedef ParmVarDecl * const *param_const_iterator; 822 823 param_iterator param_begin() { return ParamInfo; } 824 param_iterator param_end() { return ParamInfo+param_size(); } 825 826 param_const_iterator param_begin() const { return ParamInfo; } 827 param_const_iterator param_end() const { return ParamInfo+param_size(); } 828 829 /// getNumParams - Return the number of parameters this function must have 830 /// based on its functiontype. This is the length of the PararmInfo array 831 /// after it has been created. 832 unsigned getNumParams() const; 833 834 const ParmVarDecl *getParamDecl(unsigned i) const { 835 assert(i < getNumParams() && "Illegal param #"); 836 return ParamInfo[i]; 837 } 838 ParmVarDecl *getParamDecl(unsigned i) { 839 assert(i < getNumParams() && "Illegal param #"); 840 return ParamInfo[i]; 841 } 842 void setParams(ASTContext& C, ParmVarDecl **NewParamInfo, unsigned NumParams); 843 844 /// getMinRequiredArguments - Returns the minimum number of arguments 845 /// needed to call this function. This may be fewer than the number of 846 /// function parameters, if some of the parameters have default 847 /// arguments (in C++). 848 unsigned getMinRequiredArguments() const; 849 850 QualType getResultType() const { 851 return getType()->getAsFunctionType()->getResultType(); 852 } 853 StorageClass getStorageClass() const { return StorageClass(SClass); } 854 void setStorageClass(StorageClass SC) { SClass = SC; } 855 856 bool isInline() const { return IsInline; } 857 void setInline(bool I) { IsInline = I; } 858 859 /// \brief Whether this function is an "inline definition" as 860 /// defined by C99. 861 bool isC99InlineDefinition() const { return C99InlineDefinition; } 862 void setC99InlineDefinition(bool I) { C99InlineDefinition = I; } 863 864 /// \brief Determines whether this function has a gnu_inline 865 /// attribute that affects its semantics. 866 /// 867 /// The gnu_inline attribute only introduces GNU inline semantics 868 /// when all of the inline declarations of the function are marked 869 /// gnu_inline. 870 bool hasActiveGNUInlineAttribute(ASTContext &Context) const; 871 872 /// \brief Determines whether this function is a GNU "extern 873 /// inline", which is roughly the opposite of a C99 "extern inline" 874 /// function. 875 bool isExternGNUInline(ASTContext &Context) const; 876 877 /// isOverloadedOperator - Whether this function declaration 878 /// represents an C++ overloaded operator, e.g., "operator+". 879 bool isOverloadedOperator() const { 880 return getOverloadedOperator() != OO_None; 881 }; 882 883 OverloadedOperatorKind getOverloadedOperator() const; 884 885 /// \brief If this function is an instantiation of a member function 886 /// of a class template specialization, retrieves the function from 887 /// which it was instantiated. 888 /// 889 /// This routine will return non-NULL for (non-templated) member 890 /// functions of class templates and for instantiations of function 891 /// templates. For example, given: 892 /// 893 /// \code 894 /// template<typename T> 895 /// struct X { 896 /// void f(T); 897 /// }; 898 /// \endcode 899 /// 900 /// The declaration for X<int>::f is a (non-templated) FunctionDecl 901 /// whose parent is the class template specialization X<int>. For 902 /// this declaration, getInstantiatedFromFunction() will return 903 /// the FunctionDecl X<T>::A. When a complete definition of 904 /// X<int>::A is required, it will be instantiated from the 905 /// declaration returned by getInstantiatedFromMemberFunction(). 906 FunctionDecl *getInstantiatedFromMemberFunction() const { 907 return TemplateOrSpecialization.dyn_cast<FunctionDecl*>(); 908 } 909 910 /// \brief Specify that this record is an instantiation of the 911 /// member function RD. 912 void setInstantiationOfMemberFunction(FunctionDecl *RD) { 913 TemplateOrSpecialization = RD; 914 } 915 916 /// \brief Retrieves the function template that is described by this 917 /// function declaration. 918 /// 919 /// Every function template is represented as a FunctionTemplateDecl 920 /// and a FunctionDecl (or something derived from FunctionDecl). The 921 /// former contains template properties (such as the template 922 /// parameter lists) while the latter contains the actual 923 /// description of the template's 924 /// contents. FunctionTemplateDecl::getTemplatedDecl() retrieves the 925 /// FunctionDecl that describes the function template, 926 /// getDescribedFunctionTemplate() retrieves the 927 /// FunctionTemplateDecl from a FunctionDecl. 928 FunctionTemplateDecl *getDescribedFunctionTemplate() const { 929 return TemplateOrSpecialization.dyn_cast<FunctionTemplateDecl*>(); 930 } 931 932 void setDescribedFunctionTemplate(FunctionTemplateDecl *Template) { 933 TemplateOrSpecialization = Template; 934 } 935 936 /// \brief Retrieve the primary template that this function template 937 /// specialization either specializes or was instantiated from. 938 /// 939 /// If this function declaration is not a function template specialization, 940 /// returns NULL. 941 FunctionTemplateDecl *getPrimaryTemplate() const; 942 943 /// \brief Retrieve the template arguments used to produce this function 944 /// template specialization from the primary template. 945 /// 946 /// If this function declaration is not a function template specialization, 947 /// returns NULL. 948 const TemplateArgumentList *getTemplateSpecializationArgs() const; 949 950 /// \brief Specify that this function declaration is actually a function 951 /// template specialization. 952 /// 953 /// \param Context the AST context in which this function resides. 954 /// 955 /// \param Template the function template that this function template 956 /// specialization specializes. 957 /// 958 /// \param TemplateArgs the template arguments that produced this 959 /// function template specialization from the template. 960 void setFunctionTemplateSpecialization(ASTContext &Context, 961 FunctionTemplateDecl *Template, 962 const TemplateArgumentList *TemplateArgs, 963 void *InsertPos); 964 965 // Implement isa/cast/dyncast/etc. 966 static bool classof(const Decl *D) { 967 return D->getKind() >= FunctionFirst && D->getKind() <= FunctionLast; 968 } 969 static bool classof(const FunctionDecl *D) { return true; } 970 static DeclContext *castToDeclContext(const FunctionDecl *D) { 971 return static_cast<DeclContext *>(const_cast<FunctionDecl*>(D)); 972 } 973 static FunctionDecl *castFromDeclContext(const DeclContext *DC) { 974 return static_cast<FunctionDecl *>(const_cast<DeclContext*>(DC)); 975 } 976}; 977 978 979/// FieldDecl - An instance of this class is created by Sema::ActOnField to 980/// represent a member of a struct/union/class. 981class FieldDecl : public ValueDecl { 982 // FIXME: This can be packed into the bitfields in Decl. 983 bool Mutable : 1; 984 Expr *BitWidth; 985protected: 986 FieldDecl(Kind DK, DeclContext *DC, SourceLocation L, 987 IdentifierInfo *Id, QualType T, Expr *BW, bool Mutable) 988 : ValueDecl(DK, DC, L, Id, T), Mutable(Mutable), BitWidth(BW) 989 { } 990 991public: 992 static FieldDecl *Create(ASTContext &C, DeclContext *DC, SourceLocation L, 993 IdentifierInfo *Id, QualType T, Expr *BW, 994 bool Mutable); 995 996 /// isMutable - Determines whether this field is mutable (C++ only). 997 bool isMutable() const { return Mutable; } 998 999 /// \brief Set whether this field is mutable (C++ only). 1000 void setMutable(bool M) { Mutable = M; } 1001 1002 /// isBitfield - Determines whether this field is a bitfield. 1003 bool isBitField() const { return BitWidth != NULL; } 1004 1005 /// @brief Determines whether this is an unnamed bitfield. 1006 bool isUnnamedBitfield() const { return BitWidth != NULL && !getDeclName(); } 1007 1008 /// isAnonymousStructOrUnion - Determines whether this field is a 1009 /// representative for an anonymous struct or union. Such fields are 1010 /// unnamed and are implicitly generated by the implementation to 1011 /// store the data for the anonymous union or struct. 1012 bool isAnonymousStructOrUnion() const; 1013 1014 Expr *getBitWidth() const { return BitWidth; } 1015 void setBitWidth(Expr *BW) { BitWidth = BW; } 1016 1017 // Implement isa/cast/dyncast/etc. 1018 static bool classof(const Decl *D) { 1019 return D->getKind() >= FieldFirst && D->getKind() <= FieldLast; 1020 } 1021 static bool classof(const FieldDecl *D) { return true; } 1022}; 1023 1024/// EnumConstantDecl - An instance of this object exists for each enum constant 1025/// that is defined. For example, in "enum X {a,b}", each of a/b are 1026/// EnumConstantDecl's, X is an instance of EnumDecl, and the type of a/b is a 1027/// TagType for the X EnumDecl. 1028class EnumConstantDecl : public ValueDecl { 1029 Stmt *Init; // an integer constant expression 1030 llvm::APSInt Val; // The value. 1031protected: 1032 EnumConstantDecl(DeclContext *DC, SourceLocation L, 1033 IdentifierInfo *Id, QualType T, Expr *E, 1034 const llvm::APSInt &V) 1035 : ValueDecl(EnumConstant, DC, L, Id, T), Init((Stmt*)E), Val(V) {} 1036 1037 virtual ~EnumConstantDecl() {} 1038public: 1039 1040 static EnumConstantDecl *Create(ASTContext &C, EnumDecl *DC, 1041 SourceLocation L, IdentifierInfo *Id, 1042 QualType T, Expr *E, 1043 const llvm::APSInt &V); 1044 1045 virtual void Destroy(ASTContext& C); 1046 1047 const Expr *getInitExpr() const { return (const Expr*) Init; } 1048 Expr *getInitExpr() { return (Expr*) Init; } 1049 const llvm::APSInt &getInitVal() const { return Val; } 1050 1051 void setInitExpr(Expr *E) { Init = (Stmt*) E; } 1052 void setInitVal(const llvm::APSInt &V) { Val = V; } 1053 1054 // Implement isa/cast/dyncast/etc. 1055 static bool classof(const Decl *D) { return D->getKind() == EnumConstant; } 1056 static bool classof(const EnumConstantDecl *D) { return true; } 1057 1058 friend class StmtIteratorBase; 1059}; 1060 1061 1062/// TypeDecl - Represents a declaration of a type. 1063/// 1064class TypeDecl : public NamedDecl { 1065 /// TypeForDecl - This indicates the Type object that represents 1066 /// this TypeDecl. It is a cache maintained by 1067 /// ASTContext::getTypedefType, ASTContext::getTagDeclType, and 1068 /// ASTContext::getTemplateTypeParmType, and TemplateTypeParmDecl. 1069 mutable Type *TypeForDecl; 1070 friend class ASTContext; 1071 friend class DeclContext; 1072 friend class TagDecl; 1073 friend class TemplateTypeParmDecl; 1074 friend class ClassTemplateSpecializationDecl; 1075 friend class TagType; 1076 1077protected: 1078 TypeDecl(Kind DK, DeclContext *DC, SourceLocation L, 1079 IdentifierInfo *Id) 1080 : NamedDecl(DK, DC, L, Id), TypeForDecl(0) {} 1081 1082public: 1083 // Low-level accessor 1084 Type *getTypeForDecl() const { return TypeForDecl; } 1085 void setTypeForDecl(Type *TD) { TypeForDecl = TD; } 1086 1087 // Implement isa/cast/dyncast/etc. 1088 static bool classof(const Decl *D) { 1089 return D->getKind() >= TypeFirst && D->getKind() <= TypeLast; 1090 } 1091 static bool classof(const TypeDecl *D) { return true; } 1092}; 1093 1094 1095class TypedefDecl : public TypeDecl { 1096 /// UnderlyingType - This is the type the typedef is set to. 1097 QualType UnderlyingType; 1098 TypedefDecl(DeclContext *DC, SourceLocation L, 1099 IdentifierInfo *Id, QualType T) 1100 : TypeDecl(Typedef, DC, L, Id), UnderlyingType(T) {} 1101 1102 virtual ~TypedefDecl() {} 1103public: 1104 1105 static TypedefDecl *Create(ASTContext &C, DeclContext *DC, 1106 SourceLocation L,IdentifierInfo *Id, 1107 QualType T); 1108 1109 QualType getUnderlyingType() const { return UnderlyingType; } 1110 void setUnderlyingType(QualType newType) { UnderlyingType = newType; } 1111 1112 // Implement isa/cast/dyncast/etc. 1113 static bool classof(const Decl *D) { return D->getKind() == Typedef; } 1114 static bool classof(const TypedefDecl *D) { return true; } 1115}; 1116 1117class TypedefDecl; 1118 1119/// TagDecl - Represents the declaration of a struct/union/class/enum. 1120class TagDecl : public TypeDecl, public DeclContext { 1121public: 1122 enum TagKind { 1123 TK_struct, 1124 TK_union, 1125 TK_class, 1126 TK_enum 1127 }; 1128 1129private: 1130 // FIXME: This can be packed into the bitfields in Decl. 1131 /// TagDeclKind - The TagKind enum. 1132 unsigned TagDeclKind : 2; 1133 1134 /// IsDefinition - True if this is a definition ("struct foo {};"), false if 1135 /// it is a declaration ("struct foo;"). 1136 bool IsDefinition : 1; 1137 1138 /// TypedefForAnonDecl - If a TagDecl is anonymous and part of a typedef, 1139 /// this points to the TypedefDecl. Used for mangling. 1140 TypedefDecl *TypedefForAnonDecl; 1141 1142protected: 1143 TagDecl(Kind DK, TagKind TK, DeclContext *DC, SourceLocation L, 1144 IdentifierInfo *Id) 1145 : TypeDecl(DK, DC, L, Id), DeclContext(DK), TypedefForAnonDecl(0) { 1146 assert((DK != Enum || TK == TK_enum) &&"EnumDecl not matched with TK_enum"); 1147 TagDeclKind = TK; 1148 IsDefinition = false; 1149 } 1150public: 1151 1152 /// isDefinition - Return true if this decl has its body specified. 1153 bool isDefinition() const { 1154 return IsDefinition; 1155 } 1156 1157 /// \brief Whether this declaration declares a type that is 1158 /// dependent, i.e., a type that somehow depends on template 1159 /// parameters. 1160 bool isDependentType() const { return isDependentContext(); } 1161 1162 /// @brief Starts the definition of this tag declaration. 1163 /// 1164 /// This method should be invoked at the beginning of the definition 1165 /// of this tag declaration. It will set the tag type into a state 1166 /// where it is in the process of being defined. 1167 void startDefinition(); 1168 1169 /// @brief Completes the definition of this tag declaration. 1170 void completeDefinition(); 1171 1172 /// getDefinition - Returns the TagDecl that actually defines this 1173 /// struct/union/class/enum. When determining whether or not a 1174 /// struct/union/class/enum is completely defined, one should use this method 1175 /// as opposed to 'isDefinition'. 'isDefinition' indicates whether or not a 1176 /// specific TagDecl is defining declaration, not whether or not the 1177 /// struct/union/class/enum type is defined. This method returns NULL if 1178 /// there is no TagDecl that defines the struct/union/class/enum. 1179 TagDecl* getDefinition(ASTContext& C) const; 1180 1181 const char *getKindName() const { 1182 switch (getTagKind()) { 1183 default: assert(0 && "Unknown TagKind!"); 1184 case TK_struct: return "struct"; 1185 case TK_union: return "union"; 1186 case TK_class: return "class"; 1187 case TK_enum: return "enum"; 1188 } 1189 } 1190 1191 TagKind getTagKind() const { 1192 return TagKind(TagDeclKind); 1193 } 1194 1195 void setTagKind(TagKind TK) { TagDeclKind = TK; } 1196 1197 bool isStruct() const { return getTagKind() == TK_struct; } 1198 bool isClass() const { return getTagKind() == TK_class; } 1199 bool isUnion() const { return getTagKind() == TK_union; } 1200 bool isEnum() const { return getTagKind() == TK_enum; } 1201 1202 TypedefDecl *getTypedefForAnonDecl() const { return TypedefForAnonDecl; } 1203 void setTypedefForAnonDecl(TypedefDecl *TDD) { TypedefForAnonDecl = TDD; } 1204 1205 // Implement isa/cast/dyncast/etc. 1206 static bool classof(const Decl *D) { 1207 return D->getKind() >= TagFirst && D->getKind() <= TagLast; 1208 } 1209 static bool classof(const TagDecl *D) { return true; } 1210 1211 static DeclContext *castToDeclContext(const TagDecl *D) { 1212 return static_cast<DeclContext *>(const_cast<TagDecl*>(D)); 1213 } 1214 static TagDecl *castFromDeclContext(const DeclContext *DC) { 1215 return static_cast<TagDecl *>(const_cast<DeclContext*>(DC)); 1216 } 1217 1218 void setDefinition(bool V) { IsDefinition = V; } 1219}; 1220 1221/// EnumDecl - Represents an enum. As an extension, we allow forward-declared 1222/// enums. 1223class EnumDecl : public TagDecl { 1224 /// IntegerType - This represent the integer type that the enum corresponds 1225 /// to for code generation purposes. Note that the enumerator constants may 1226 /// have a different type than this does. 1227 QualType IntegerType; 1228 1229 /// \brief If the enumeration was instantiated from an enumeration 1230 /// within a class or function template, this pointer refers to the 1231 /// enumeration declared within the template. 1232 EnumDecl *InstantiatedFrom; 1233 1234 EnumDecl(DeclContext *DC, SourceLocation L, 1235 IdentifierInfo *Id) 1236 : TagDecl(Enum, TK_enum, DC, L, Id), InstantiatedFrom(0) { 1237 IntegerType = QualType(); 1238 } 1239public: 1240 static EnumDecl *Create(ASTContext &C, DeclContext *DC, 1241 SourceLocation L, IdentifierInfo *Id, 1242 EnumDecl *PrevDecl); 1243 1244 virtual void Destroy(ASTContext& C); 1245 1246 /// completeDefinition - When created, the EnumDecl corresponds to a 1247 /// forward-declared enum. This method is used to mark the 1248 /// declaration as being defined; it's enumerators have already been 1249 /// added (via DeclContext::addDecl). NewType is the new underlying 1250 /// type of the enumeration type. 1251 void completeDefinition(ASTContext &C, QualType NewType); 1252 1253 // enumerator_iterator - Iterates through the enumerators of this 1254 // enumeration. 1255 typedef specific_decl_iterator<EnumConstantDecl> enumerator_iterator; 1256 1257 enumerator_iterator enumerator_begin(ASTContext &Context) const { 1258 return enumerator_iterator(this->decls_begin(Context)); 1259 } 1260 1261 enumerator_iterator enumerator_end(ASTContext &Context) const { 1262 return enumerator_iterator(this->decls_end(Context)); 1263 } 1264 1265 /// getIntegerType - Return the integer type this enum decl corresponds to. 1266 /// This returns a null qualtype for an enum forward definition. 1267 QualType getIntegerType() const { return IntegerType; } 1268 1269 /// \brief Set the underlying integer type. 1270 void setIntegerType(QualType T) { IntegerType = T; } 1271 1272 /// \brief Returns the enumeration (declared within the template) 1273 /// from which this enumeration type was instantiated, or NULL if 1274 /// this enumeration was not instantiated from any template. 1275 EnumDecl *getInstantiatedFromMemberEnum() const { 1276 return InstantiatedFrom; 1277 } 1278 1279 void setInstantiationOfMemberEnum(EnumDecl *IF) { InstantiatedFrom = IF; } 1280 1281 static bool classof(const Decl *D) { return D->getKind() == Enum; } 1282 static bool classof(const EnumDecl *D) { return true; } 1283}; 1284 1285 1286/// RecordDecl - Represents a struct/union/class. For example: 1287/// struct X; // Forward declaration, no "body". 1288/// union Y { int A, B; }; // Has body with members A and B (FieldDecls). 1289/// This decl will be marked invalid if *any* members are invalid. 1290/// 1291class RecordDecl : public TagDecl { 1292 // FIXME: This can be packed into the bitfields in Decl. 1293 /// HasFlexibleArrayMember - This is true if this struct ends with a flexible 1294 /// array member (e.g. int X[]) or if this union contains a struct that does. 1295 /// If so, this cannot be contained in arrays or other structs as a member. 1296 bool HasFlexibleArrayMember : 1; 1297 1298 /// AnonymousStructOrUnion - Whether this is the type of an 1299 /// anonymous struct or union. 1300 bool AnonymousStructOrUnion : 1; 1301 1302protected: 1303 RecordDecl(Kind DK, TagKind TK, DeclContext *DC, 1304 SourceLocation L, IdentifierInfo *Id); 1305 virtual ~RecordDecl(); 1306 1307public: 1308 static RecordDecl *Create(ASTContext &C, TagKind TK, DeclContext *DC, 1309 SourceLocation L, IdentifierInfo *Id, 1310 RecordDecl* PrevDecl = 0); 1311 1312 virtual void Destroy(ASTContext& C); 1313 1314 bool hasFlexibleArrayMember() const { return HasFlexibleArrayMember; } 1315 void setHasFlexibleArrayMember(bool V) { HasFlexibleArrayMember = V; } 1316 1317 /// isAnonymousStructOrUnion - Whether this is an anonymous struct 1318 /// or union. To be an anonymous struct or union, it must have been 1319 /// declared without a name and there must be no objects of this 1320 /// type declared, e.g., 1321 /// @code 1322 /// union { int i; float f; }; 1323 /// @endcode 1324 /// is an anonymous union but neither of the following are: 1325 /// @code 1326 /// union X { int i; float f; }; 1327 /// union { int i; float f; } obj; 1328 /// @endcode 1329 bool isAnonymousStructOrUnion() const { return AnonymousStructOrUnion; } 1330 void setAnonymousStructOrUnion(bool Anon) { 1331 AnonymousStructOrUnion = Anon; 1332 } 1333 1334 /// \brief Determines whether this declaration represents the 1335 /// injected class name. 1336 /// 1337 /// The injected class name in C++ is the name of the class that 1338 /// appears inside the class itself. For example: 1339 /// 1340 /// \code 1341 /// struct C { 1342 /// // C is implicitly declared here as a synonym for the class name. 1343 /// }; 1344 /// 1345 /// C::C c; // same as "C c;" 1346 /// \endcode 1347 bool isInjectedClassName() const; 1348 1349 /// getDefinition - Returns the RecordDecl that actually defines this 1350 /// struct/union/class. When determining whether or not a struct/union/class 1351 /// is completely defined, one should use this method as opposed to 1352 /// 'isDefinition'. 'isDefinition' indicates whether or not a specific 1353 /// RecordDecl is defining declaration, not whether or not the record 1354 /// type is defined. This method returns NULL if there is no RecordDecl 1355 /// that defines the struct/union/tag. 1356 RecordDecl* getDefinition(ASTContext& C) const { 1357 return cast_or_null<RecordDecl>(TagDecl::getDefinition(C)); 1358 } 1359 1360 // Iterator access to field members. The field iterator only visits 1361 // the non-static data members of this class, ignoring any static 1362 // data members, functions, constructors, destructors, etc. 1363 typedef specific_decl_iterator<FieldDecl> field_iterator; 1364 1365 field_iterator field_begin(ASTContext &Context) const { 1366 return field_iterator(decls_begin(Context)); 1367 } 1368 field_iterator field_end(ASTContext &Context) const { 1369 return field_iterator(decls_end(Context)); 1370 } 1371 1372 // field_empty - Whether there are any fields (non-static data 1373 // members) in this record. 1374 bool field_empty(ASTContext &Context) const { 1375 return field_begin(Context) == field_end(Context); 1376 } 1377 1378 /// completeDefinition - Notes that the definition of this type is 1379 /// now complete. 1380 void completeDefinition(ASTContext& C); 1381 1382 static bool classof(const Decl *D) { 1383 return D->getKind() >= RecordFirst && D->getKind() <= RecordLast; 1384 } 1385 static bool classof(const RecordDecl *D) { return true; } 1386}; 1387 1388class FileScopeAsmDecl : public Decl { 1389 StringLiteral *AsmString; 1390 FileScopeAsmDecl(DeclContext *DC, SourceLocation L, StringLiteral *asmstring) 1391 : Decl(FileScopeAsm, DC, L), AsmString(asmstring) {} 1392public: 1393 static FileScopeAsmDecl *Create(ASTContext &C, DeclContext *DC, 1394 SourceLocation L, StringLiteral *Str); 1395 1396 const StringLiteral *getAsmString() const { return AsmString; } 1397 StringLiteral *getAsmString() { return AsmString; } 1398 void setAsmString(StringLiteral *Asm) { AsmString = Asm; } 1399 1400 static bool classof(const Decl *D) { 1401 return D->getKind() == FileScopeAsm; 1402 } 1403 static bool classof(const FileScopeAsmDecl *D) { return true; } 1404}; 1405 1406/// BlockDecl - This represents a block literal declaration, which is like an 1407/// unnamed FunctionDecl. For example: 1408/// ^{ statement-body } or ^(int arg1, float arg2){ statement-body } 1409/// 1410class BlockDecl : public Decl, public DeclContext { 1411 // FIXME: This can be packed into the bitfields in Decl. 1412 bool isVariadic : 1; 1413 /// ParamInfo - new[]'d array of pointers to ParmVarDecls for the formal 1414 /// parameters of this function. This is null if a prototype or if there are 1415 /// no formals. 1416 ParmVarDecl **ParamInfo; 1417 unsigned NumParams; 1418 1419 Stmt *Body; 1420 1421protected: 1422 BlockDecl(DeclContext *DC, SourceLocation CaretLoc) 1423 : Decl(Block, DC, CaretLoc), DeclContext(Block), 1424 isVariadic(false), ParamInfo(0), NumParams(0), Body(0) {} 1425 1426 virtual ~BlockDecl(); 1427 virtual void Destroy(ASTContext& C); 1428 1429public: 1430 static BlockDecl *Create(ASTContext &C, DeclContext *DC, SourceLocation L); 1431 1432 SourceLocation getCaretLocation() const { return getLocation(); } 1433 1434 bool IsVariadic() const { return isVariadic; } 1435 void setIsVariadic(bool value) { isVariadic = value; } 1436 1437 CompoundStmt *getBody() const { return (CompoundStmt*) Body; } 1438 Stmt *getBody(ASTContext &C) const { return (Stmt*) Body; } 1439 void setBody(CompoundStmt *B) { Body = (Stmt*) B; } 1440 1441 // Iterator access to formal parameters. 1442 unsigned param_size() const { return getNumParams(); } 1443 typedef ParmVarDecl **param_iterator; 1444 typedef ParmVarDecl * const *param_const_iterator; 1445 1446 bool param_empty() const { return NumParams == 0; } 1447 param_iterator param_begin() { return ParamInfo; } 1448 param_iterator param_end() { return ParamInfo+param_size(); } 1449 1450 param_const_iterator param_begin() const { return ParamInfo; } 1451 param_const_iterator param_end() const { return ParamInfo+param_size(); } 1452 1453 unsigned getNumParams() const; 1454 const ParmVarDecl *getParamDecl(unsigned i) const { 1455 assert(i < getNumParams() && "Illegal param #"); 1456 return ParamInfo[i]; 1457 } 1458 ParmVarDecl *getParamDecl(unsigned i) { 1459 assert(i < getNumParams() && "Illegal param #"); 1460 return ParamInfo[i]; 1461 } 1462 void setParams(ASTContext& C, ParmVarDecl **NewParamInfo, unsigned NumParams); 1463 1464 // Implement isa/cast/dyncast/etc. 1465 static bool classof(const Decl *D) { return D->getKind() == Block; } 1466 static bool classof(const BlockDecl *D) { return true; } 1467 static DeclContext *castToDeclContext(const BlockDecl *D) { 1468 return static_cast<DeclContext *>(const_cast<BlockDecl*>(D)); 1469 } 1470 static BlockDecl *castFromDeclContext(const DeclContext *DC) { 1471 return static_cast<BlockDecl *>(const_cast<DeclContext*>(DC)); 1472 } 1473}; 1474 1475/// Insertion operator for diagnostics. This allows sending NamedDecl's 1476/// into a diagnostic with <<. 1477inline const DiagnosticBuilder &operator<<(const DiagnosticBuilder &DB, 1478 NamedDecl* ND) { 1479 DB.AddTaggedVal(reinterpret_cast<intptr_t>(ND), Diagnostic::ak_nameddecl); 1480 return DB; 1481} 1482 1483} // end namespace clang 1484 1485#endif 1486