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