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