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