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