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