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