Decl.h revision 72c3f314d92d65c050ee1c07b7753623c044d6c7
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/Basic/IdentifierTable.h" 18#include "clang/Basic/OperatorKinds.h" 19#include "clang/AST/DeclarationName.h" 20#include "clang/AST/DeclBase.h" 21#include "clang/Parse/AccessSpecifier.h" 22#include "llvm/ADT/SmallVector.h" 23 24namespace clang { 25class Expr; 26class Stmt; 27class CompoundStmt; 28class StringLiteral; 29 30/// TranslationUnitDecl - The top declaration context. 31/// FIXME: The TranslationUnit class should probably be modified to serve as 32/// the top decl context. It would have ownership of the top decls so that the 33/// AST is self-contained and easily de/serializable. 34class TranslationUnitDecl : public Decl, public DeclContext { 35 TranslationUnitDecl() 36 : Decl(TranslationUnit, SourceLocation()), 37 DeclContext(TranslationUnit) {} 38public: 39 static TranslationUnitDecl *Create(ASTContext &C); 40 // Implement isa/cast/dyncast/etc. 41 static bool classof(const Decl *D) { return D->getKind() == TranslationUnit; } 42 static bool classof(const TranslationUnitDecl *D) { return true; } 43 static DeclContext *castToDeclContext(const TranslationUnitDecl *D) { 44 return static_cast<DeclContext *>(const_cast<TranslationUnitDecl*>(D)); 45 } 46 static TranslationUnitDecl *castFromDeclContext(const DeclContext *DC) { 47 return static_cast<TranslationUnitDecl *>(const_cast<DeclContext*>(DC)); 48 } 49 50protected: 51 /// EmitImpl - Serialize this TranslationUnitDecl. Called by Decl::Emit. 52 virtual void EmitImpl(llvm::Serializer& S) const; 53 54 /// CreateImpl - Deserialize a TranslationUnitDecl. Called by Decl::Create. 55 static TranslationUnitDecl* CreateImpl(llvm::Deserializer& D, ASTContext& C); 56 57 friend Decl* Decl::Create(llvm::Deserializer& D, ASTContext& C); 58}; 59 60/// NamedDecl - This represents a decl with a name. Many decls have names such 61/// as ObjCMethodDecl, but not @class, etc. 62class NamedDecl : public Decl { 63 /// Name - The name of this declaration, which is typically a normal 64 /// identifier but may also be a special kind of name (C++ 65 /// constructor, Objective-C selector, etc.) 66 DeclarationName Name; 67 68protected: 69 NamedDecl(Kind DK, SourceLocation L, DeclarationName N) 70 : Decl(DK, L), Name(N) {} 71 72public: 73 NamedDecl(Kind DK, SourceLocation L, IdentifierInfo *Id) 74 : Decl(DK, L), Name(Id) {} 75 76 /// getIdentifier - Get the identifier that names this declaration, 77 /// if there is one. This will return NULL if this declaration has 78 /// no name (e.g., for an unnamed class) or if the name is a special 79 /// name (C++ constructor, Objective-C selector, etc.). 80 IdentifierInfo *getIdentifier() const { return Name.getAsIdentifierInfo(); } 81 82 /// getNameAsCString - Get the name of identifier for this declaration as a 83 /// C string (const char*). This requires that the declaration have a name 84 /// and that it be a simple identifier. 85 const char *getNameAsCString() const { 86 assert(getIdentifier() && "Name is not a simple identifier"); 87 return getIdentifier()->getName(); 88 } 89 90 /// getDeclName - Get the actual, stored name of the declaration, 91 /// which may be a special name. 92 DeclarationName getDeclName() const { return Name; } 93 94 /// getNameAsString - Get a human-readable name for the declaration, even if 95 /// it is one of the special kinds of names (C++ constructor, Objective-C 96 /// selector, etc). Creating this name requires expensive string 97 /// manipulation, so it should be called only when performance doesn't matter. 98 /// For simple declarations, getNameAsCString() should suffice. 99 std::string getNameAsString() const { return Name.getAsString(); } 100 101 static bool classof(const Decl *D) { 102 return D->getKind() >= NamedFirst && D->getKind() <= NamedLast; 103 } 104 static bool classof(const NamedDecl *D) { return true; } 105 106protected: 107 void EmitInRec(llvm::Serializer& S) const; 108 void ReadInRec(llvm::Deserializer& D, ASTContext& C); 109}; 110 111/// ScopedDecl - Represent lexically scoped names, used for all ValueDecl's 112/// and TypeDecl's. 113class ScopedDecl : public NamedDecl { 114 /// NextDeclarator - If this decl was part of a multi-declarator declaration, 115 /// such as "int X, Y, *Z;" this indicates Decl for the next declarator. 116 ScopedDecl *NextDeclarator; 117 118 /// When this decl is in scope while parsing, the Next field contains a 119 /// pointer to the shadowed decl of the same name. When the scope is popped, 120 /// Decls are relinked onto a containing decl object. 121 /// 122 ScopedDecl *Next; 123 124 /// DeclCtx - Holds either a DeclContext* or a MultipleDC*. 125 /// For declarations that don't contain C++ scope specifiers, it contains 126 /// the DeclContext where the ScopedDecl was declared. 127 /// For declarations with C++ scope specifiers, it contains a MultipleDC* 128 /// with the context where it semantically belongs (SemanticDC) and the 129 /// context where it was lexically declared (LexicalDC). 130 /// e.g.: 131 /// 132 /// namespace A { 133 /// void f(); // SemanticDC == LexicalDC == 'namespace A' 134 /// } 135 /// void A::f(); // SemanticDC == namespace 'A' 136 /// // LexicalDC == global namespace 137 uintptr_t DeclCtx; 138 139 struct MultipleDC { 140 DeclContext *SemanticDC; 141 DeclContext *LexicalDC; 142 }; 143 144 inline bool isInSemaDC() const { return (DeclCtx & 0x1) == 0; } 145 inline bool isOutOfSemaDC() const { return (DeclCtx & 0x1) != 0; } 146 inline MultipleDC *getMultipleDC() const { 147 return reinterpret_cast<MultipleDC*>(DeclCtx & ~0x1); 148 } 149 150protected: 151 ScopedDecl(Kind DK, DeclContext *DC, SourceLocation L, 152 DeclarationName N, ScopedDecl *PrevDecl) 153 : NamedDecl(DK, L, N), NextDeclarator(PrevDecl), Next(0), 154 DeclCtx(reinterpret_cast<uintptr_t>(DC)) {} 155 156 virtual ~ScopedDecl(); 157 158public: 159 const DeclContext *getDeclContext() const { 160 if (isInSemaDC()) 161 return reinterpret_cast<DeclContext*>(DeclCtx); 162 return getMultipleDC()->SemanticDC; 163 } 164 DeclContext *getDeclContext() { 165 return const_cast<DeclContext*>( 166 const_cast<const ScopedDecl*>(this)->getDeclContext()); 167 } 168 169 /// getLexicalDeclContext - The declaration context where this ScopedDecl was 170 /// lexically declared (LexicalDC). May be different from 171 /// getDeclContext() (SemanticDC). 172 /// e.g.: 173 /// 174 /// namespace A { 175 /// void f(); // SemanticDC == LexicalDC == 'namespace A' 176 /// } 177 /// void A::f(); // SemanticDC == namespace 'A' 178 /// // LexicalDC == global namespace 179 const DeclContext *getLexicalDeclContext() const { 180 if (isInSemaDC()) 181 return reinterpret_cast<DeclContext*>(DeclCtx); 182 return getMultipleDC()->LexicalDC; 183 } 184 DeclContext *getLexicalDeclContext() { 185 return const_cast<DeclContext*>( 186 const_cast<const ScopedDecl*>(this)->getLexicalDeclContext()); 187 } 188 189 void setLexicalDeclContext(DeclContext *DC); 190 191 ScopedDecl *getNext() const { return Next; } 192 void setNext(ScopedDecl *N) { Next = N; } 193 194 /// getNextDeclarator - If this decl was part of a multi-declarator 195 /// declaration, such as "int X, Y, *Z;" this returns the decl for the next 196 /// declarator. Otherwise it returns null. 197 ScopedDecl *getNextDeclarator() { return NextDeclarator; } 198 const ScopedDecl *getNextDeclarator() const { return NextDeclarator; } 199 void setNextDeclarator(ScopedDecl *N) { NextDeclarator = N; } 200 201 // isDefinedOutsideFunctionOrMethod - This predicate returns true if this 202 // scoped decl is defined outside the current function or method. This is 203 // roughly global variables and functions, but also handles enums (which could 204 // be defined inside or outside a function etc). 205 bool isDefinedOutsideFunctionOrMethod() const { 206 if (getDeclContext()) 207 return !getDeclContext()->isFunctionOrMethod(); 208 else 209 return true; 210 } 211 212 // Implement isa/cast/dyncast/etc. 213 static bool classof(const Decl *D) { 214 return D->getKind() >= ScopedFirst && D->getKind() <= ScopedLast; 215 } 216 static bool classof(const ScopedDecl *D) { return true; } 217 218protected: 219 void EmitInRec(llvm::Serializer& S) const; 220 void ReadInRec(llvm::Deserializer& D, ASTContext& C); 221 222 void EmitOutRec(llvm::Serializer& S) const; 223 void ReadOutRec(llvm::Deserializer& D, ASTContext& C); 224 225 friend void Decl::Destroy(ASTContext& C); 226}; 227 228/// NamespaceDecl - Represent a C++ namespace. 229class NamespaceDecl : public ScopedDecl, public DeclContext { 230 SourceLocation LBracLoc, RBracLoc; 231 232 // For extended namespace definitions: 233 // 234 // namespace A { int x; } 235 // namespace A { int y; } 236 // 237 // there will be one NamespaceDecl for each declaration. 238 // NextDeclarator points to the next extended declaration. 239 // OrigNamespace points to the original namespace declaration. 240 // OrigNamespace of the first namespace decl points to itself. 241 242 NamespaceDecl *OrigNamespace; 243 244 NamespaceDecl(DeclContext *DC, SourceLocation L, IdentifierInfo *Id) 245 : ScopedDecl(Namespace, DC, L, Id, 0), DeclContext(Namespace) { 246 OrigNamespace = this; 247 } 248public: 249 static NamespaceDecl *Create(ASTContext &C, DeclContext *DC, 250 SourceLocation L, IdentifierInfo *Id); 251 252 virtual void Destroy(ASTContext& C); 253 254 NamespaceDecl *getNextNamespace() { 255 return cast_or_null<NamespaceDecl>(getNextDeclarator()); 256 } 257 const NamespaceDecl *getNextNamespace() const { 258 return cast_or_null<NamespaceDecl>(getNextDeclarator()); 259 } 260 void setNextNamespace(NamespaceDecl *ND) { setNextDeclarator(ND); } 261 262 NamespaceDecl *getOriginalNamespace() const { 263 return OrigNamespace; 264 } 265 void setOriginalNamespace(NamespaceDecl *ND) { OrigNamespace = ND; } 266 267 SourceRange getSourceRange() const { 268 return SourceRange(LBracLoc, RBracLoc); 269 } 270 271 SourceLocation getLBracLoc() const { return LBracLoc; } 272 SourceLocation getRBracLoc() const { return RBracLoc; } 273 void setLBracLoc(SourceLocation LBrace) { LBracLoc = LBrace; } 274 void setRBracLoc(SourceLocation RBrace) { RBracLoc = RBrace; } 275 276 // Implement isa/cast/dyncast/etc. 277 static bool classof(const Decl *D) { return D->getKind() == Namespace; } 278 static bool classof(const NamespaceDecl *D) { return true; } 279 static DeclContext *castToDeclContext(const NamespaceDecl *D) { 280 return static_cast<DeclContext *>(const_cast<NamespaceDecl*>(D)); 281 } 282 static NamespaceDecl *castFromDeclContext(const DeclContext *DC) { 283 return static_cast<NamespaceDecl *>(const_cast<DeclContext*>(DC)); 284 } 285 286protected: 287 /// EmitImpl - Serialize this NamespaceDecl. Called by Decl::Emit. 288 virtual void EmitImpl(llvm::Serializer& S) const; 289 290 /// CreateImpl - Deserialize a NamespaceDecl. Called by Decl::Create. 291 static NamespaceDecl* CreateImpl(llvm::Deserializer& D, ASTContext& C); 292 293 friend Decl* Decl::Create(llvm::Deserializer& D, ASTContext& C); 294}; 295 296/// ValueDecl - Represent the declaration of a variable (in which case it is 297/// an lvalue) a function (in which case it is a function designator) or 298/// an enum constant. 299class ValueDecl : public ScopedDecl { 300 QualType DeclType; 301 302protected: 303 ValueDecl(Kind DK, DeclContext *DC, SourceLocation L, 304 DeclarationName N, QualType T, ScopedDecl *PrevDecl) 305 : ScopedDecl(DK, DC, L, N, PrevDecl), DeclType(T) {} 306public: 307 QualType getType() const { return DeclType; } 308 void setType(QualType newType) { DeclType = newType; } 309 310 // Implement isa/cast/dyncast/etc. 311 static bool classof(const Decl *D) { 312 return D->getKind() >= ValueFirst && D->getKind() <= ValueLast; 313 } 314 static bool classof(const ValueDecl *D) { return true; } 315 316protected: 317 void EmitInRec(llvm::Serializer& S) const; 318 void ReadInRec(llvm::Deserializer& D, ASTContext& C); 319}; 320 321/// VarDecl - An instance of this class is created to represent a variable 322/// declaration or definition. 323class VarDecl : public ValueDecl { 324public: 325 enum StorageClass { 326 None, Auto, Register, Extern, Static, PrivateExtern 327 }; 328private: 329 Stmt *Init; 330 // FIXME: This can be packed into the bitfields in Decl. 331 unsigned SClass : 3; 332 bool ThreadSpecified : 1; 333 bool HasCXXDirectInit : 1; 334 335 // Move to DeclGroup when it is implemented. 336 SourceLocation TypeSpecStartLoc; 337 friend class StmtIteratorBase; 338protected: 339 VarDecl(Kind DK, DeclContext *DC, SourceLocation L, IdentifierInfo *Id, 340 QualType T, StorageClass SC, ScopedDecl *PrevDecl, 341 SourceLocation TSSL = SourceLocation()) 342 : ValueDecl(DK, DC, L, Id, T, PrevDecl), Init(0), 343 ThreadSpecified(false), HasCXXDirectInit(false), 344 TypeSpecStartLoc(TSSL) { SClass = SC; } 345public: 346 static VarDecl *Create(ASTContext &C, DeclContext *DC, 347 SourceLocation L, IdentifierInfo *Id, 348 QualType T, StorageClass S, ScopedDecl *PrevDecl, 349 SourceLocation TypeSpecStartLoc = SourceLocation()); 350 351 StorageClass getStorageClass() const { return (StorageClass)SClass; } 352 353 SourceLocation getTypeSpecStartLoc() const { return TypeSpecStartLoc; } 354 355 const Expr *getInit() const { return (const Expr*) Init; } 356 Expr *getInit() { return (Expr*) Init; } 357 void setInit(Expr *I) { Init = (Stmt*) I; } 358 359 void setThreadSpecified(bool T) { ThreadSpecified = T; } 360 bool isThreadSpecified() const { 361 return ThreadSpecified; 362 } 363 364 void setCXXDirectInitializer(bool T) { HasCXXDirectInit = T; } 365 366 /// hasCXXDirectInitializer - If true, the initializer was a direct 367 /// initializer, e.g: "int x(1);". The Init expression will be the expression 368 /// inside the parens or a "ClassType(a,b,c)" class constructor expression for 369 /// class types. Clients can distinguish between "int x(1);" and "int x=1;" 370 /// by checking hasCXXDirectInitializer. 371 /// 372 bool hasCXXDirectInitializer() const { 373 return HasCXXDirectInit; 374 } 375 376 /// hasLocalStorage - Returns true if a variable with function scope 377 /// is a non-static local variable. 378 bool hasLocalStorage() const { 379 if (getStorageClass() == None) 380 return !isFileVarDecl(); 381 382 // Return true for: Auto, Register. 383 // Return false for: Extern, Static, PrivateExtern. 384 385 return getStorageClass() <= Register; 386 } 387 388 /// hasGlobalStorage - Returns true for all variables that do not 389 /// have local storage. This includs all global variables as well 390 /// as static variables declared within a function. 391 bool hasGlobalStorage() const { return !hasLocalStorage(); } 392 393 /// isBlockVarDecl - Returns true for local variable declarations. Note that 394 /// this includes static variables inside of functions. 395 /// 396 /// void foo() { int x; static int y; extern int z; } 397 /// 398 bool isBlockVarDecl() const { 399 if (getKind() != Decl::Var) 400 return false; 401 if (const DeclContext *DC = getDeclContext()) 402 return DC->isFunctionOrMethod(); 403 return false; 404 } 405 406 /// isFileVarDecl - Returns true for file scoped variable declaration. 407 bool isFileVarDecl() const { 408 if (getKind() != Decl::Var) 409 return false; 410 if (isa<TranslationUnitDecl>(getDeclContext()) || 411 isa<NamespaceDecl>(getDeclContext()) ) 412 return true; 413 return false; 414 } 415 416 // Implement isa/cast/dyncast/etc. 417 static bool classof(const Decl *D) { 418 return D->getKind() >= VarFirst && D->getKind() <= VarLast; 419 } 420 static bool classof(const VarDecl *D) { return true; } 421 422protected: 423 void EmitInRec(llvm::Serializer& S) const; 424 void ReadInRec(llvm::Deserializer& D, ASTContext& C); 425 426 void EmitOutRec(llvm::Serializer& S) const; 427 void ReadOutRec(llvm::Deserializer& D, ASTContext& C); 428 429 /// EmitImpl - Serialize this VarDecl. Called by Decl::Emit. 430 virtual void EmitImpl(llvm::Serializer& S) const; 431 432 /// ReadImpl - Deserialize this VarDecl. Called by subclasses. 433 virtual void ReadImpl(llvm::Deserializer& D, ASTContext& C); 434 435 /// CreateImpl - Deserialize a VarDecl. Called by Decl::Create. 436 static VarDecl* CreateImpl(llvm::Deserializer& D, ASTContext& C); 437}; 438 439class ImplicitParamDecl : public VarDecl { 440protected: 441 ImplicitParamDecl(Kind DK, DeclContext *DC, SourceLocation L, 442 IdentifierInfo *Id, QualType T, ScopedDecl *PrevDecl) 443 : VarDecl(DK, DC, L, Id, T, VarDecl::None, PrevDecl) {} 444public: 445 static ImplicitParamDecl *Create(ASTContext &C, DeclContext *DC, 446 SourceLocation L, IdentifierInfo *Id, 447 QualType T, ScopedDecl *PrevDecl); 448 // Implement isa/cast/dyncast/etc. 449 static bool classof(const ImplicitParamDecl *D) { return true; } 450 static bool classof(const Decl *D) { return D->getKind() == ImplicitParam; } 451}; 452 453/// ParmVarDecl - Represent a parameter to a function. 454class ParmVarDecl : public VarDecl { 455 // NOTE: VC++ treats enums as signed, avoid using the ObjCDeclQualifier enum 456 /// FIXME: Also can be paced into the bitfields in Decl. 457 /// in, inout, etc. 458 unsigned objcDeclQualifier : 6; 459 460 /// Default argument, if any. [C++ Only] 461 Expr *DefaultArg; 462 463 ParmVarDecl(DeclContext *DC, SourceLocation L, 464 IdentifierInfo *Id, QualType T, StorageClass S, 465 Expr *DefArg, ScopedDecl *PrevDecl) 466 : VarDecl(ParmVar, DC, L, Id, T, S, PrevDecl), 467 objcDeclQualifier(OBJC_TQ_None), DefaultArg(DefArg) {} 468 469public: 470 static ParmVarDecl *Create(ASTContext &C, DeclContext *DC, 471 SourceLocation L,IdentifierInfo *Id, 472 QualType T, StorageClass S, Expr *DefArg, 473 ScopedDecl *PrevDecl); 474 475 ObjCDeclQualifier getObjCDeclQualifier() const { 476 return ObjCDeclQualifier(objcDeclQualifier); 477 } 478 void setObjCDeclQualifier(ObjCDeclQualifier QTVal) 479 { objcDeclQualifier = QTVal; } 480 481 const Expr *getDefaultArg() const { return DefaultArg; } 482 Expr *getDefaultArg() { return DefaultArg; } 483 void setDefaultArg(Expr *defarg) { DefaultArg = defarg; } 484 485 // Implement isa/cast/dyncast/etc. 486 static bool classof(const Decl *D) { return D->getKind() == ParmVar; } 487 static bool classof(const ParmVarDecl *D) { return true; } 488 489protected: 490 /// EmitImpl - Serialize this ParmVarDecl. Called by Decl::Emit. 491 virtual void EmitImpl(llvm::Serializer& S) const; 492 493 /// CreateImpl - Deserialize a ParmVarDecl. Called by Decl::Create. 494 static ParmVarDecl* CreateImpl(llvm::Deserializer& D, ASTContext& C); 495 496 friend Decl* Decl::Create(llvm::Deserializer& D, ASTContext& C); 497}; 498 499/// FunctionDecl - An instance of this class is created to represent a 500/// function declaration or definition. 501/// 502/// Since a given function can be declared several times in a program, 503/// there may be several FunctionDecls that correspond to that 504/// function. Only one of those FunctionDecls will be found when 505/// traversing the list of declarations in the context of the 506/// FunctionDecl (e.g., the translation unit); this FunctionDecl 507/// contains all of the information known about the function. Other, 508/// previous declarations of the function are available via the 509/// getPreviousDeclaration() chain. 510class FunctionDecl : public ValueDecl, public DeclContext { 511public: 512 enum StorageClass { 513 None, Extern, Static, PrivateExtern 514 }; 515private: 516 /// ParamInfo - new[]'d array of pointers to VarDecls for the formal 517 /// parameters of this function. This is null if a prototype or if there are 518 /// no formals. TODO: we could allocate this space immediately after the 519 /// FunctionDecl object to save an allocation like FunctionType does. 520 ParmVarDecl **ParamInfo; 521 522 Stmt *Body; // Null if a prototype. 523 524 /// PreviousDeclaration - A link to the previous declaration of this 525 /// same function, NULL if this is the first declaration. For 526 /// example, in the following code, the PreviousDeclaration can be 527 /// traversed several times to see all three declarations of the 528 /// function "f", the last of which is also a definition. 529 /// 530 /// int f(int x, int y = 1); 531 /// int f(int x = 0, int y); 532 /// int f(int x, int y) { return x + y; } 533 FunctionDecl *PreviousDeclaration; 534 535 // NOTE: VC++ treats enums as signed, avoid using the StorageClass enum 536 unsigned SClass : 2; 537 bool IsInline : 1; 538 bool IsImplicit : 1; 539 540 // Move to DeclGroup when it is implemented. 541 SourceLocation TypeSpecStartLoc; 542protected: 543 FunctionDecl(Kind DK, DeclContext *DC, SourceLocation L, 544 DeclarationName N, QualType T, 545 StorageClass S, bool isInline, ScopedDecl *PrevDecl, 546 SourceLocation TSSL = SourceLocation()) 547 : ValueDecl(DK, DC, L, N, T, PrevDecl), 548 DeclContext(DK), 549 ParamInfo(0), Body(0), PreviousDeclaration(0), 550 SClass(S), IsInline(isInline), IsImplicit(0), TypeSpecStartLoc(TSSL) {} 551 552 virtual ~FunctionDecl(); 553 virtual void Destroy(ASTContext& C); 554 555public: 556 static FunctionDecl *Create(ASTContext &C, DeclContext *DC, SourceLocation L, 557 DeclarationName N, QualType T, 558 StorageClass S = None, bool isInline = false, 559 ScopedDecl *PrevDecl = 0, 560 SourceLocation TSStartLoc = SourceLocation()); 561 562 SourceLocation getTypeSpecStartLoc() const { return TypeSpecStartLoc; } 563 564 /// getBody - Retrieve the body (definition) of the function. The 565 /// function body might be in any of the (re-)declarations of this 566 /// function. The variant that accepts a FunctionDecl pointer will 567 /// set that function declaration to the actual declaration 568 /// containing the body (if there is one). 569 Stmt *getBody(const FunctionDecl *&Definition) const; 570 571 virtual Stmt *getBody() const { 572 const FunctionDecl* Definition; 573 return getBody(Definition); 574 } 575 576 /// isThisDeclarationADefinition - Returns whether this specific 577 /// declaration of the function is also a definition. This does not 578 /// determine whether the function has been defined (e.g., in a 579 /// previous definition); for that information, use getBody. 580 bool isThisDeclarationADefinition() const { return Body != 0; } 581 582 void setBody(Stmt *B) { Body = B; } 583 584 bool isImplicit() { return IsImplicit; } 585 void setImplicit() { IsImplicit = true; } 586 587 /// getPreviousDeclaration - Return the previous declaration of this 588 /// function. 589 const FunctionDecl *getPreviousDeclaration() const { 590 return PreviousDeclaration; 591 } 592 593 void setPreviousDeclaration(FunctionDecl * PrevDecl) { 594 PreviousDeclaration = PrevDecl; 595 } 596 597 // Iterator access to formal parameters. 598 unsigned param_size() const { return getNumParams(); } 599 typedef ParmVarDecl **param_iterator; 600 typedef ParmVarDecl * const *param_const_iterator; 601 602 param_iterator param_begin() { return ParamInfo; } 603 param_iterator param_end() { return ParamInfo+param_size(); } 604 605 param_const_iterator param_begin() const { return ParamInfo; } 606 param_const_iterator param_end() const { return ParamInfo+param_size(); } 607 608 unsigned getNumParams() const; 609 const ParmVarDecl *getParamDecl(unsigned i) const { 610 assert(i < getNumParams() && "Illegal param #"); 611 return ParamInfo[i]; 612 } 613 ParmVarDecl *getParamDecl(unsigned i) { 614 assert(i < getNumParams() && "Illegal param #"); 615 return ParamInfo[i]; 616 } 617 void setParams(ParmVarDecl **NewParamInfo, unsigned NumParams); 618 619 /// getMinRequiredArguments - Returns the minimum number of arguments 620 /// needed to call this function. This may be fewer than the number of 621 /// function parameters, if some of the parameters have default 622 /// arguments (in C++). 623 unsigned getMinRequiredArguments() const; 624 625 QualType getResultType() const { 626 return getType()->getAsFunctionType()->getResultType(); 627 } 628 StorageClass getStorageClass() const { return StorageClass(SClass); } 629 bool isInline() const { return IsInline; } 630 631 /// isOverloadedOperator - Whether this function declaration 632 /// represents an C++ overloaded operator, e.g., "operator+". 633 bool isOverloadedOperator() const { 634 return getOverloadedOperator() != OO_None; 635 }; 636 637 OverloadedOperatorKind getOverloadedOperator() const; 638 639 // Implement isa/cast/dyncast/etc. 640 static bool classof(const Decl *D) { 641 return D->getKind() >= FunctionFirst && D->getKind() <= FunctionLast; 642 } 643 static bool classof(const FunctionDecl *D) { return true; } 644 static DeclContext *castToDeclContext(const FunctionDecl *D) { 645 return static_cast<DeclContext *>(const_cast<FunctionDecl*>(D)); 646 } 647 static FunctionDecl *castFromDeclContext(const DeclContext *DC) { 648 return static_cast<FunctionDecl *>(const_cast<DeclContext*>(DC)); 649 } 650 651protected: 652 /// EmitImpl - Serialize this FunctionDecl. Called by Decl::Emit. 653 virtual void EmitImpl(llvm::Serializer& S) const; 654 655 /// CreateImpl - Deserialize a FunctionDecl. Called by Decl::Create. 656 static FunctionDecl* CreateImpl(llvm::Deserializer& D, ASTContext& C); 657 658 friend Decl* Decl::Create(llvm::Deserializer& D, ASTContext& C); 659 friend class CXXRecordDecl; 660}; 661 662 663/// FieldDecl - An instance of this class is created by Sema::ActOnField to 664/// represent a member of a struct/union/class. 665class FieldDecl : public NamedDecl { 666 QualType DeclType; 667 Expr *BitWidth; 668protected: 669 FieldDecl(Kind DK, SourceLocation L, IdentifierInfo *Id, QualType T, 670 Expr *BW = NULL) 671 : NamedDecl(DK, L, Id), DeclType(T), BitWidth(BW) {} 672 FieldDecl(SourceLocation L, IdentifierInfo *Id, QualType T, Expr *BW) 673 : NamedDecl(Field, L, Id), DeclType(T), BitWidth(BW) {} 674public: 675 static FieldDecl *Create(ASTContext &C, SourceLocation L, IdentifierInfo *Id, 676 QualType T, Expr *BW = NULL); 677 678 QualType getType() const { return DeclType; } 679 680 bool isBitField() const { return BitWidth != NULL; } 681 Expr *getBitWidth() const { return BitWidth; } 682 // Implement isa/cast/dyncast/etc. 683 static bool classof(const Decl *D) { 684 return D->getKind() >= FieldFirst && D->getKind() <= FieldLast; 685 } 686 static bool classof(const FieldDecl *D) { return true; } 687 688protected: 689 /// EmitImpl - Serialize this FieldDecl. Called by Decl::Emit. 690 virtual void EmitImpl(llvm::Serializer& S) const; 691 692 /// CreateImpl - Deserialize a FieldDecl. Called by Decl::Create. 693 static FieldDecl* CreateImpl(llvm::Deserializer& D, ASTContext& C); 694 695 friend Decl* Decl::Create(llvm::Deserializer& D, ASTContext& C); 696}; 697 698/// EnumConstantDecl - An instance of this object exists for each enum constant 699/// that is defined. For example, in "enum X {a,b}", each of a/b are 700/// EnumConstantDecl's, X is an instance of EnumDecl, and the type of a/b is a 701/// TagType for the X EnumDecl. 702class EnumConstantDecl : public ValueDecl { 703 Stmt *Init; // an integer constant expression 704 llvm::APSInt Val; // The value. 705protected: 706 EnumConstantDecl(DeclContext *DC, SourceLocation L, 707 IdentifierInfo *Id, QualType T, Expr *E, 708 const llvm::APSInt &V, ScopedDecl *PrevDecl) 709 : ValueDecl(EnumConstant, DC, L, Id, T, PrevDecl), Init((Stmt*)E), Val(V) {} 710 711 virtual ~EnumConstantDecl() {} 712public: 713 714 static EnumConstantDecl *Create(ASTContext &C, EnumDecl *DC, 715 SourceLocation L, IdentifierInfo *Id, 716 QualType T, Expr *E, 717 const llvm::APSInt &V, ScopedDecl *PrevDecl); 718 719 virtual void Destroy(ASTContext& C); 720 721 const Expr *getInitExpr() const { return (const Expr*) Init; } 722 Expr *getInitExpr() { return (Expr*) Init; } 723 const llvm::APSInt &getInitVal() const { return Val; } 724 725 void setInitExpr(Expr *E) { Init = (Stmt*) E; } 726 void setInitVal(const llvm::APSInt &V) { Val = V; } 727 728 // Implement isa/cast/dyncast/etc. 729 static bool classof(const Decl *D) { return D->getKind() == EnumConstant; } 730 static bool classof(const EnumConstantDecl *D) { return true; } 731 732 friend class StmtIteratorBase; 733 734protected: 735 /// EmitImpl - Serialize this EnumConstantDecl. Called by Decl::Emit. 736 virtual void EmitImpl(llvm::Serializer& S) const; 737 738 /// CreateImpl - Deserialize a EnumConstantDecl. Called by Decl::Create. 739 static EnumConstantDecl* CreateImpl(llvm::Deserializer& D, ASTContext& C); 740 741 friend Decl* Decl::Create(llvm::Deserializer& D, ASTContext& C); 742}; 743 744 745/// TypeDecl - Represents a declaration of a type. 746/// 747class TypeDecl : public ScopedDecl { 748 /// TypeForDecl - This indicates the Type object that represents this 749 /// TypeDecl. It is a cache maintained by ASTContext::getTypedefType, 750 /// ASTContext::getTagDeclType, and ASTContext::getTemplateTypeParmType. 751 Type *TypeForDecl; 752 friend class ASTContext; 753protected: 754 TypeDecl(Kind DK, DeclContext *DC, SourceLocation L, 755 IdentifierInfo *Id, ScopedDecl *PrevDecl) 756 : ScopedDecl(DK, DC, L, Id, PrevDecl), TypeForDecl(0) {} 757public: 758 void setAccess(AccessSpecifier AS) { Access = AS; } 759 AccessSpecifier getAccess() const { return AccessSpecifier(Access); } 760 761 // Implement isa/cast/dyncast/etc. 762 static bool classof(const Decl *D) { 763 return D->getKind() >= TypeFirst && D->getKind() <= TypeLast; 764 } 765 static bool classof(const TypeDecl *D) { return true; } 766}; 767 768 769class TypedefDecl : public TypeDecl { 770 /// UnderlyingType - This is the type the typedef is set to. 771 QualType UnderlyingType; 772 TypedefDecl(DeclContext *DC, SourceLocation L, 773 IdentifierInfo *Id, QualType T, ScopedDecl *PD) 774 : TypeDecl(Typedef, DC, L, Id, PD), UnderlyingType(T) {} 775 776 virtual ~TypedefDecl() {} 777public: 778 779 static TypedefDecl *Create(ASTContext &C, DeclContext *DC, 780 SourceLocation L,IdentifierInfo *Id, 781 QualType T, ScopedDecl *PD); 782 783 QualType getUnderlyingType() const { return UnderlyingType; } 784 void setUnderlyingType(QualType newType) { UnderlyingType = newType; } 785 786 // Implement isa/cast/dyncast/etc. 787 static bool classof(const Decl *D) { return D->getKind() == Typedef; } 788 static bool classof(const TypedefDecl *D) { return true; } 789 790protected: 791 /// EmitImpl - Serialize this TypedefDecl. Called by Decl::Emit. 792 virtual void EmitImpl(llvm::Serializer& S) const; 793 794 /// CreateImpl - Deserialize a TypedefDecl. Called by Decl::Create. 795 static TypedefDecl* CreateImpl(llvm::Deserializer& D, ASTContext& C); 796 797 friend Decl* Decl::Create(llvm::Deserializer& D, ASTContext& C); 798}; 799 800 801/// TagDecl - Represents the declaration of a struct/union/class/enum. 802class TagDecl : public TypeDecl { 803public: 804 enum TagKind { 805 TK_struct, 806 TK_union, 807 TK_class, 808 TK_enum 809 }; 810 811private: 812 /// TagDeclKind - The TagKind enum. 813 unsigned TagDeclKind : 2; 814 815 /// IsDefinition - True if this is a definition ("struct foo {};"), false if 816 /// it is a declaration ("struct foo;"). 817 bool IsDefinition : 1; 818protected: 819 TagDecl(Kind DK, TagKind TK, DeclContext *DC, SourceLocation L, 820 IdentifierInfo *Id, ScopedDecl *PrevDecl) 821 : TypeDecl(DK, DC, L, Id, PrevDecl) { 822 assert((DK != Enum || TK == TK_enum) &&"EnumDecl not matched with TK_enum"); 823 TagDeclKind = TK; 824 IsDefinition = false; 825 } 826public: 827 828 /// isDefinition - Return true if this decl has its body specified. 829 bool isDefinition() const { 830 return IsDefinition; 831 } 832 833 /// getDefinition - Returns the TagDecl that actually defines this 834 /// struct/union/class/enum. When determining whether or not a 835 /// struct/union/class/enum is completely defined, one should use this method 836 /// as opposed to 'isDefinition'. 'isDefinition' indicates whether or not a 837 /// specific TagDecl is defining declaration, not whether or not the 838 /// struct/union/class/enum type is defined. This method returns NULL if 839 /// there is no TagDecl that defines the struct/union/class/enum. 840 TagDecl* getDefinition(ASTContext& C) const; 841 842 const char *getKindName() const { 843 switch (getTagKind()) { 844 default: assert(0 && "Unknown TagKind!"); 845 case TK_struct: return "struct"; 846 case TK_union: return "union"; 847 case TK_class: return "class"; 848 case TK_enum: return "enum"; 849 } 850 } 851 852 TagKind getTagKind() const { 853 return TagKind(TagDeclKind); 854 } 855 856 bool isStruct() const { return getTagKind() == TK_struct; } 857 bool isClass() const { return getTagKind() == TK_class; } 858 bool isUnion() const { return getTagKind() == TK_union; } 859 bool isEnum() const { return getTagKind() == TK_enum; } 860 861 // Implement isa/cast/dyncast/etc. 862 static bool classof(const Decl *D) { 863 return D->getKind() >= TagFirst && D->getKind() <= TagLast; 864 } 865 static bool classof(const TagDecl *D) { return true; } 866protected: 867 void setDefinition(bool V) { IsDefinition = V; } 868}; 869 870/// EnumDecl - Represents an enum. As an extension, we allow forward-declared 871/// enums. 872class EnumDecl : public TagDecl, public DeclContext { 873 // EnumDecl's DeclChain points to a linked list of EnumConstantDecl's which 874 // are linked together through their getNextDeclarator pointers. 875 876 /// IntegerType - This represent the integer type that the enum corresponds 877 /// to for code generation purposes. Note that the enumerator constants may 878 /// have a different type than this does. 879 QualType IntegerType; 880 881 EnumDecl(DeclContext *DC, SourceLocation L, 882 IdentifierInfo *Id, ScopedDecl *PrevDecl) 883 : TagDecl(Enum, TK_enum, DC, L, Id, PrevDecl), DeclContext(Enum) { 884 IntegerType = QualType(); 885 } 886public: 887 static EnumDecl *Create(ASTContext &C, DeclContext *DC, 888 SourceLocation L, IdentifierInfo *Id, 889 ScopedDecl *PrevDecl); 890 891 virtual void Destroy(ASTContext& C); 892 893 /// defineElements - When created, EnumDecl correspond to a forward declared 894 /// enum. This method is used to mark the decl as being defined, with the 895 /// specified list of enums. 896 void defineElements(EnumConstantDecl *ListHead, QualType NewType) { 897 assert(!isDefinition() && "Cannot redefine enums!"); 898 setDeclChain(ListHead); 899 setDefinition(true); 900 901 IntegerType = NewType; 902 } 903 904 /// getIntegerType - Return the integer type this enum decl corresponds to. 905 /// This returns a null qualtype for an enum forward definition. 906 QualType getIntegerType() const { return IntegerType; } 907 908 /// getEnumConstantList - Return the first EnumConstantDecl in the enum. 909 /// 910 EnumConstantDecl *getEnumConstantList() { 911 return cast_or_null<EnumConstantDecl>(getDeclChain()); 912 } 913 const EnumConstantDecl *getEnumConstantList() const { 914 return cast_or_null<const EnumConstantDecl>(getDeclChain()); 915 } 916 917 static bool classof(const Decl *D) { return D->getKind() == Enum; } 918 static bool classof(const EnumDecl *D) { return true; } 919 static DeclContext *castToDeclContext(const EnumDecl *D) { 920 return static_cast<DeclContext *>(const_cast<EnumDecl*>(D)); 921 } 922 static EnumDecl *castFromDeclContext(const DeclContext *DC) { 923 return static_cast<EnumDecl *>(const_cast<DeclContext*>(DC)); 924 } 925 926protected: 927 /// EmitImpl - Serialize this EnumDecl. Called by Decl::Emit. 928 virtual void EmitImpl(llvm::Serializer& S) const; 929 930 /// CreateImpl - Deserialize a EnumDecl. Called by Decl::Create. 931 static EnumDecl* CreateImpl(llvm::Deserializer& D, ASTContext& C); 932 933 friend Decl* Decl::Create(llvm::Deserializer& D, ASTContext& C); 934}; 935 936 937/// RecordDecl - Represents a struct/union/class. For example: 938/// struct X; // Forward declaration, no "body". 939/// union Y { int A, B; }; // Has body with members A and B (FieldDecls). 940/// This decl will be marked invalid if *any* members are invalid. 941/// 942class RecordDecl : public TagDecl { 943 /// HasFlexibleArrayMember - This is true if this struct ends with a flexible 944 /// array member (e.g. int X[]) or if this union contains a struct that does. 945 /// If so, this cannot be contained in arrays or other structs as a member. 946 bool HasFlexibleArrayMember : 1; 947 948 /// Members/NumMembers - This is a new[]'d array of pointers to Decls. 949 FieldDecl **Members; // Null if not defined. 950 int NumMembers; // -1 if not defined. 951 952protected: 953 RecordDecl(Kind DK, TagKind TK, DeclContext *DC, 954 SourceLocation L, IdentifierInfo *Id); 955 virtual ~RecordDecl(); 956 957public: 958 static RecordDecl *Create(ASTContext &C, TagKind TK, DeclContext *DC, 959 SourceLocation L, IdentifierInfo *Id, 960 RecordDecl* PrevDecl = 0); 961 962 virtual void Destroy(ASTContext& C); 963 964 bool hasFlexibleArrayMember() const { return HasFlexibleArrayMember; } 965 void setHasFlexibleArrayMember(bool V) { HasFlexibleArrayMember = V; } 966 967 /// getDefinition - Returns the RecordDecl that actually defines this 968 /// struct/union/class. When determining whether or not a struct/union/class 969 /// is completely defined, one should use this method as opposed to 970 /// 'isDefinition'. 'isDefinition' indicates whether or not a specific 971 /// RecordDecl is defining declaration, not whether or not the record 972 /// type is defined. This method returns NULL if there is no RecordDecl 973 /// that defines the struct/union/tag. 974 RecordDecl* getDefinition(ASTContext& C) const { 975 return cast_or_null<RecordDecl>(TagDecl::getDefinition(C)); 976 } 977 978 /// getNumMembers - Return the number of members, or -1 if this is a forward 979 /// definition. 980 int getNumMembers() const { return NumMembers; } 981 const FieldDecl *getMember(unsigned i) const { return Members[i]; } 982 FieldDecl *getMember(unsigned i) { return Members[i]; } 983 984 // Iterator access to field members. 985 typedef FieldDecl **field_iterator; 986 typedef FieldDecl * const *field_const_iterator; 987 988 field_iterator field_begin() { 989 assert(isDefinition() && "Not a definition!"); 990 return Members; 991 } 992 field_iterator field_end() { 993 assert(isDefinition() && "Not a definition!"); 994 return Members + getNumMembers(); 995 } 996 997 field_const_iterator field_begin() const { 998 assert(isDefinition() && "Not a definition!"); 999 return Members; 1000 } 1001 field_const_iterator field_end() const { 1002 assert(isDefinition() && "Not a definition!"); 1003 return Members + getNumMembers(); 1004 } 1005 1006 /// defineBody - When created, RecordDecl's correspond to a forward declared 1007 /// record. This method is used to mark the decl as being defined, with the 1008 /// specified contents. 1009 void defineBody(ASTContext& C, FieldDecl **Members, unsigned numMembers); 1010 1011 /// getMember - If the member doesn't exist, or there are no members, this 1012 /// function will return 0; 1013 FieldDecl *getMember(IdentifierInfo *name); 1014 1015 static bool classof(const Decl *D) { 1016 return D->getKind() >= RecordFirst && D->getKind() <= RecordLast; 1017 } 1018 static bool classof(const RecordDecl *D) { return true; } 1019 1020protected: 1021 /// EmitImpl - Serialize this RecordDecl. Called by Decl::Emit. 1022 virtual void EmitImpl(llvm::Serializer& S) const; 1023 1024 /// CreateImpl - Deserialize a RecordDecl. Called by Decl::Create. 1025 static RecordDecl* CreateImpl(llvm::Deserializer& D, ASTContext& C); 1026 1027 friend Decl* Decl::Create(llvm::Deserializer& D, ASTContext& C); 1028}; 1029 1030class FileScopeAsmDecl : public Decl { 1031 StringLiteral *AsmString; 1032 FileScopeAsmDecl(SourceLocation L, StringLiteral *asmstring) 1033 : Decl(FileScopeAsm, L), AsmString(asmstring) {} 1034public: 1035 static FileScopeAsmDecl *Create(ASTContext &C, SourceLocation L, 1036 StringLiteral *Str); 1037 1038 const StringLiteral *getAsmString() const { return AsmString; } 1039 StringLiteral *getAsmString() { return AsmString; } 1040 static bool classof(const Decl *D) { 1041 return D->getKind() == FileScopeAsm; 1042 } 1043 static bool classof(const FileScopeAsmDecl *D) { return true; } 1044protected: 1045 /// EmitImpl - Serialize this FileScopeAsmDecl. Called by Decl::Emit. 1046 virtual void EmitImpl(llvm::Serializer& S) const; 1047 1048 /// CreateImpl - Deserialize a FileScopeAsmDecl. Called by Decl::Create. 1049 static FileScopeAsmDecl* CreateImpl(llvm::Deserializer& D, ASTContext& C); 1050 1051 friend Decl* Decl::Create(llvm::Deserializer& D, ASTContext& C); 1052}; 1053 1054/// BlockDecl - This represents a block literal declaration, which is like an 1055/// unnamed FunctionDecl. For example: 1056/// ^{ statement-body } or ^(int arg1, float arg2){ statement-body } 1057/// 1058class BlockDecl : public Decl, public DeclContext { 1059 llvm::SmallVector<ParmVarDecl*, 8> Args; 1060 Stmt *Body; 1061 1062 // Since BlockDecl's aren't named/scoped, we need to store the context. 1063 DeclContext *ParentContext; 1064protected: 1065 BlockDecl(DeclContext *DC, SourceLocation CaretLoc) 1066 : Decl(Block, CaretLoc), DeclContext(Block), Body(0), ParentContext(DC) {} 1067 1068 virtual ~BlockDecl(); 1069 virtual void Destroy(ASTContext& C); 1070 1071public: 1072 static BlockDecl *Create(ASTContext &C, DeclContext *DC, SourceLocation L); 1073 1074 SourceLocation getCaretLocation() const { return getLocation(); } 1075 1076 Stmt *getBody() const { return Body; } 1077 void setBody(Stmt *B) { Body = B; } 1078 1079 void setArgs(ParmVarDecl **args, unsigned numargs) { 1080 Args.clear(); 1081 Args.insert(Args.begin(), args, args+numargs); 1082 } 1083 const DeclContext *getParentContext() const { return ParentContext; } 1084 DeclContext *getParentContext() { return ParentContext; } 1085 1086 /// arg_iterator - Iterate over the ParmVarDecl's for this block. 1087 typedef llvm::SmallVector<ParmVarDecl*, 8>::const_iterator param_iterator; 1088 bool param_empty() const { return Args.empty(); } 1089 param_iterator param_begin() const { return Args.begin(); } 1090 param_iterator param_end() const { return Args.end(); } 1091 1092 // Implement isa/cast/dyncast/etc. 1093 static bool classof(const Decl *D) { return D->getKind() == Block; } 1094 static bool classof(const BlockDecl *D) { return true; } 1095 static DeclContext *castToDeclContext(const BlockDecl *D) { 1096 return static_cast<DeclContext *>(const_cast<BlockDecl*>(D)); 1097 } 1098 static BlockDecl *castFromDeclContext(const DeclContext *DC) { 1099 return static_cast<BlockDecl *>(const_cast<DeclContext*>(DC)); 1100 } 1101 1102protected: 1103 /// EmitImpl - Serialize this BlockDecl. Called by Decl::Emit. 1104 virtual void EmitImpl(llvm::Serializer& S) const; 1105 1106 /// CreateImpl - Deserialize a BlockDecl. Called by Decl::Create. 1107 static BlockDecl* CreateImpl(llvm::Deserializer& D, ASTContext& C); 1108 1109 friend Decl* Decl::Create(llvm::Deserializer& D, ASTContext& C); 1110}; 1111 1112} // end namespace clang 1113 1114#endif 1115