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