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