Decl.h revision 2f1f8cb1b8f487086053579802407d64ca2b587f
1//===--- Decl.h - Classes for representing declarations ---------*- C++ -*-===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file was developed by Chris Lattner and is distributed under 6// the University of Illinois Open Source 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/Basic/SourceLocation.h" 18#include "clang/AST/Type.h" 19#include "llvm/ADT/APSInt.h" 20#include "llvm/Bitcode/Serialization.h" 21 22namespace clang { 23class Decl; 24} 25 26namespace llvm { 27template <> struct SerializeTrait<clang::Decl> { 28 static void Emit(Serializer& S, clang::Decl& D); 29 static clang::Decl* Materialize(Deserializer& D); 30}; 31} // end namespace llvm 32 33namespace clang { 34class Expr; 35class Stmt; 36class FunctionDecl; 37class AttributeList; 38class IdentifierInfo; 39 40/// Decl - This represents one declaration (or definition), e.g. a variable, 41/// typedef, function, struct, etc. 42/// 43class Decl { 44public: 45 enum Kind { 46 // This lists the concrete classes of Decl in order of the inheritance 47 // hierarchy. This allows us to do efficient classof tests based on the 48 // enums below. The commented out names are abstract class names. 49 50 // Decl 51 // NamedDecl 52 Field, 53 ObjcIvar, 54 ObjcCategory, 55 ObjcCategoryImpl, 56 ObjcImplementation, 57 ObjcProtocol, 58 // ScopedDecl 59 CompatibleAlias, 60 // TypeDecl 61 ObjcInterface, 62 Typedef, 63 // TagDecl 64 Enum, 65 // RecordDecl, 66 Struct, 67 Union, 68 Class, 69 // ValueDecl 70 EnumConstant, 71 Function, 72 // VarDecl 73 BlockVar, 74 FileVar, 75 ParmVar, 76 ObjcMethod, 77 ObjcClass, 78 ObjcForwardProtocol, 79 80 // For each non-leaf class, we now define a mapping to the first/last member 81 // of the class, to allow efficient classof. 82 NamedFirst = Field, NamedLast = ParmVar, 83 FieldFirst = Field, FieldLast = ObjcIvar, 84 ScopedFirst = CompatibleAlias, ScopedLast = ParmVar, 85 TypeFirst = ObjcInterface, TypeLast = Class, 86 TagFirst = Enum , TagLast = Class, 87 RecordFirst = Struct , RecordLast = Class, 88 ValueFirst = EnumConstant , ValueLast = ParmVar, 89 VarFirst = BlockVar , VarLast = ParmVar 90 }; 91 92 /// IdentifierNamespace - According to C99 6.2.3, there are four namespaces, 93 /// labels, tags, members and ordinary identifiers. 94 enum IdentifierNamespace { 95 IDNS_Label, 96 IDNS_Tag, 97 IDNS_Member, 98 IDNS_Ordinary 99 }; 100 101private: 102 /// Loc - The location that this decl. 103 SourceLocation Loc; 104 105 /// DeclKind - This indicates which class this is. 106 Kind DeclKind : 8; 107 108 /// InvalidDecl - This indicates a semantic error occurred. 109 unsigned int InvalidDecl : 1; 110 111protected: 112 Decl(Kind DK, SourceLocation L) : Loc(L), DeclKind(DK), InvalidDecl(0) { 113 if (Decl::CollectingStats()) addDeclKind(DK); 114 } 115 116 virtual ~Decl(); 117 118public: 119 SourceLocation getLocation() const { return Loc; } 120 void setLocation(SourceLocation L) { Loc = L; } 121 122 Kind getKind() const { return DeclKind; } 123 const char *getDeclKindName() const; 124 125 /// setInvalidDecl - Indicates the Decl had a semantic error. This 126 /// allows for graceful error recovery. 127 void setInvalidDecl() { InvalidDecl = 1; } 128 bool isInvalidDecl() const { return (bool) InvalidDecl; } 129 130 IdentifierNamespace getIdentifierNamespace() const { 131 switch (DeclKind) { 132 default: assert(0 && "Unknown decl kind!"); 133 case Typedef: 134 case Function: 135 case BlockVar: 136 case FileVar: 137 case ParmVar: 138 case EnumConstant: 139 case ObjcInterface: 140 case CompatibleAlias: 141 return IDNS_Ordinary; 142 case Struct: 143 case Union: 144 case Class: 145 case Enum: 146 return IDNS_Tag; 147 } 148 } 149 // global temp stats (until we have a per-module visitor) 150 static void addDeclKind(const Kind k); 151 static bool CollectingStats(bool enable=false); 152 static void PrintStats(); 153 154 // Deserialization of Decls. 155 template <typename DeclType> 156 static inline DeclType* DeserializeDecl(llvm::Deserializer& D) { 157 Decl* decl = llvm::SerializeTrait<Decl>::Materialize(D); 158 return cast<DeclType>(decl); 159 } 160 161 // Implement isa/cast/dyncast/etc. 162 static bool classof(const Decl *) { return true; } 163}; 164 165/// NamedDecl - This represents a decl with an identifier for a name. Many 166/// decls have names, but not ObjcMethodDecl, @class, etc. 167class NamedDecl : public Decl { 168 /// Identifier - The identifier for this declaration (e.g. the name for the 169 /// variable, the tag for a struct). 170 IdentifierInfo *Identifier; 171public: 172 NamedDecl(Kind DK, SourceLocation L, IdentifierInfo *Id) 173 : Decl(DK, L), Identifier(Id) {} 174 175 IdentifierInfo *getIdentifier() const { return Identifier; } 176 const char *getName() const; 177 178 179 static bool classof(const Decl *D) { 180 return D->getKind() >= NamedFirst && D->getKind() <= NamedLast; 181 } 182 static bool classof(const NamedDecl *D) { return true; } 183}; 184 185/// ScopedDecl - Represent lexically scoped names, used for all ValueDecl's 186/// and TypeDecl's. 187class ScopedDecl : public NamedDecl { 188 189 /// NextDeclarator - If this decl was part of a multi-declarator declaration, 190 /// such as "int X, Y, *Z;" this indicates Decl for the next declarator. 191 ScopedDecl *NextDeclarator; 192 193 /// When this decl is in scope while parsing, the Next field contains a 194 /// pointer to the shadowed decl of the same name. When the scope is popped, 195 /// Decls are relinked onto a containing decl object. 196 /// 197 ScopedDecl *Next; 198protected: 199 ScopedDecl(Kind DK, SourceLocation L, IdentifierInfo *Id,ScopedDecl *PrevDecl) 200 : NamedDecl(DK, L, Id), NextDeclarator(PrevDecl), Next(0) {} 201public: 202 ScopedDecl *getNext() const { return Next; } 203 void setNext(ScopedDecl *N) { Next = N; } 204 205 /// getNextDeclarator - If this decl was part of a multi-declarator 206 /// declaration, such as "int X, Y, *Z;" this returns the decl for the next 207 /// declarator. Otherwise it returns null. 208 ScopedDecl *getNextDeclarator() { return NextDeclarator; } 209 const ScopedDecl *getNextDeclarator() const { return NextDeclarator; } 210 void setNextDeclarator(ScopedDecl *N) { NextDeclarator = N; } 211 212 // Implement isa/cast/dyncast/etc. 213 static bool classof(const Decl *D) { 214 return D->getKind() >= ScopedFirst && D->getKind() <= ScopedLast; 215 } 216 static bool classof(const ScopedDecl *D) { return true; } 217}; 218 219/// ValueDecl - Represent the declaration of a variable (in which case it is 220/// an lvalue) a function (in which case it is a function designator) or 221/// an enum constant. 222class ValueDecl : public ScopedDecl { 223 QualType DeclType; 224protected: 225 ValueDecl(Kind DK, SourceLocation L, IdentifierInfo *Id, QualType T, 226 ScopedDecl *PrevDecl) : ScopedDecl(DK, L, Id, PrevDecl), DeclType(T) {} 227public: 228 QualType getType() const { return DeclType; } 229 void setType(QualType newType) { DeclType = newType; } 230 QualType getCanonicalType() const { return DeclType.getCanonicalType(); } 231 232 // Implement isa/cast/dyncast/etc. 233 static bool classof(const Decl *D) { 234 return D->getKind() >= ValueFirst && D->getKind() <= ValueLast; 235 } 236 static bool classof(const ValueDecl *D) { return true; } 237}; 238 239/// VarDecl - An instance of this class is created to represent a variable 240/// declaration or definition. 241class VarDecl : public ValueDecl { 242public: 243 enum StorageClass { 244 None, Extern, Static, Auto, Register 245 }; 246 StorageClass getStorageClass() const { return SClass; } 247 248 const Expr *getInit() const { return Init; } 249 Expr *getInit() { return Init; } 250 void setInit(Expr *I) { Init = I; } 251 252 // hasAutoStorage - Returns true if either the implicit or explicit 253 // storage class of a variable is "auto." In particular, variables 254 // declared within a function that lack a storage keyword are 255 // implicitly "auto", but are represented internally with a storage 256 // class of None. 257 bool hasAutoStorage() const { 258 return SClass == Auto || (SClass == None && getKind() != FileVar); 259 } 260 261 // hasStaticStorage - Returns true if either the implicit or 262 // explicit storage class of a variable is "static." In 263 // particular, variables declared within a file (outside of a 264 // function) that lack a storage keyword are implicitly "static," 265 // but are represented internally with a storage class of "None". 266 bool hasStaticStorage() const { 267 return SClass == Static || (SClass == None && getKind() == FileVar); 268 } 269 270 // hasLocalStorage - Returns true if a variable with function scope 271 // is a non-static local variable. 272 bool hasLocalStorage() const { return hasAutoStorage() || SClass == Register;} 273 274 // hasGlobalStorage - Returns true for all variables that do not 275 // have local storage. This includs all global variables as well 276 // as static variables declared within a function. 277 bool hasGlobalStorage() const { return !hasAutoStorage(); } 278 279 // Implement isa/cast/dyncast/etc. 280 static bool classof(const Decl *D) { 281 return D->getKind() >= VarFirst && D->getKind() <= VarLast; 282 } 283 static bool classof(const VarDecl *D) { return true; } 284protected: 285 VarDecl(Kind DK, SourceLocation L, IdentifierInfo *Id, QualType T, 286 StorageClass SC, ScopedDecl *PrevDecl) 287 : ValueDecl(DK, L, Id, T, PrevDecl), Init(0) { SClass = SC; } 288private: 289 StorageClass SClass; 290 Expr *Init; 291 friend class StmtIteratorBase; 292}; 293 294/// BlockVarDecl - Represent a local variable declaration. 295class BlockVarDecl : public VarDecl { 296public: 297 BlockVarDecl(SourceLocation L, IdentifierInfo *Id, QualType T, StorageClass S, 298 ScopedDecl *PrevDecl) 299 : VarDecl(BlockVar, L, Id, T, S, PrevDecl) {} 300 301 // Implement isa/cast/dyncast/etc. 302 static bool classof(const Decl *D) { return D->getKind() == BlockVar; } 303 static bool classof(const BlockVarDecl *D) { return true; } 304}; 305 306/// FileVarDecl - Represent a file scoped variable declaration. This 307/// will allow us to reason about external variable declarations and tentative 308/// definitions (C99 6.9.2p2) using our type system (without storing a 309/// pointer to the decl's scope, which is transient). 310class FileVarDecl : public VarDecl { 311public: 312 FileVarDecl(SourceLocation L, IdentifierInfo *Id, QualType T, StorageClass S, 313 ScopedDecl *PrevDecl) 314 : VarDecl(FileVar, L, Id, T, S, PrevDecl) {} 315 316 // Implement isa/cast/dyncast/etc. 317 static bool classof(const Decl *D) { return D->getKind() == FileVar; } 318 static bool classof(const FileVarDecl *D) { return true; } 319}; 320 321/// ParmVarDecl - Represent a parameter to a function. 322class ParmVarDecl : public VarDecl { 323public: 324 ParmVarDecl(SourceLocation L, IdentifierInfo *Id, QualType T, StorageClass S, 325 ScopedDecl *PrevDecl) 326 : VarDecl(ParmVar, L, Id, T, S, PrevDecl) {} 327 328 // Implement isa/cast/dyncast/etc. 329 static bool classof(const Decl *D) { return D->getKind() == ParmVar; } 330 static bool classof(const ParmVarDecl *D) { return true; } 331}; 332 333/// FunctionDecl - An instance of this class is created to represent a function 334/// declaration or definition. 335class FunctionDecl : public ValueDecl { 336public: 337 enum StorageClass { 338 None, Extern, Static 339 }; 340 FunctionDecl(SourceLocation L, IdentifierInfo *Id, QualType T, 341 StorageClass S = None, bool isInline = false, 342 ScopedDecl *PrevDecl = 0) 343 : ValueDecl(Function, L, Id, T, PrevDecl), 344 ParamInfo(0), Body(0), DeclChain(0), SClass(S), IsInline(isInline) {} 345 virtual ~FunctionDecl(); 346 347 Stmt *getBody() const { return Body; } 348 void setBody(Stmt *B) { Body = B; } 349 350 ScopedDecl *getDeclChain() const { return DeclChain; } 351 void setDeclChain(ScopedDecl *D) { DeclChain = D; } 352 353 unsigned getNumParams() const; 354 const ParmVarDecl *getParamDecl(unsigned i) const { 355 assert(i < getNumParams() && "Illegal param #"); 356 return ParamInfo[i]; 357 } 358 ParmVarDecl *getParamDecl(unsigned i) { 359 assert(i < getNumParams() && "Illegal param #"); 360 return ParamInfo[i]; 361 } 362 void setParams(ParmVarDecl **NewParamInfo, unsigned NumParams); 363 364 QualType getResultType() const { 365 return cast<FunctionType>(getType())->getResultType(); 366 } 367 StorageClass getStorageClass() const { return SClass; } 368 bool isInline() const { return IsInline; } 369 370 // Implement isa/cast/dyncast/etc. 371 static bool classof(const Decl *D) { return D->getKind() == Function; } 372 static bool classof(const FunctionDecl *D) { return true; } 373private: 374 /// ParamInfo - new[]'d array of pointers to VarDecls for the formal 375 /// parameters of this function. This is null if a prototype or if there are 376 /// no formals. TODO: we could allocate this space immediately after the 377 /// FunctionDecl object to save an allocation like FunctionType does. 378 ParmVarDecl **ParamInfo; 379 380 Stmt *Body; // Null if a prototype. 381 382 /// DeclChain - Linked list of declarations that are defined inside this 383 /// function. 384 ScopedDecl *DeclChain; 385 386 StorageClass SClass : 2; 387 bool IsInline : 1; 388}; 389 390 391/// FieldDecl - An instance of this class is created by Sema::ActOnField to 392/// represent a member of a struct/union/class. 393class FieldDecl : public NamedDecl { 394 QualType DeclType; 395public: 396 FieldDecl(SourceLocation L, IdentifierInfo *Id, QualType T) 397 : NamedDecl(Field, L, Id), DeclType(T) {} 398 FieldDecl(Kind DK, SourceLocation L, IdentifierInfo *Id, QualType T) 399 : NamedDecl(DK, L, Id), DeclType(T) {} 400 401 QualType getType() const { return DeclType; } 402 QualType getCanonicalType() const { return DeclType.getCanonicalType(); } 403 404 // Implement isa/cast/dyncast/etc. 405 static bool classof(const Decl *D) { 406 return D->getKind() >= FieldFirst && D->getKind() <= FieldLast; 407 } 408 static bool classof(const FieldDecl *D) { return true; } 409}; 410 411/// EnumConstantDecl - An instance of this object exists for each enum constant 412/// that is defined. For example, in "enum X {a,b}", each of a/b are 413/// EnumConstantDecl's, X is an instance of EnumDecl, and the type of a/b is a 414/// TagType for the X EnumDecl. 415class EnumConstantDecl : public ValueDecl { 416 Expr *Init; // an integer constant expression 417 llvm::APSInt Val; // The value. 418public: 419 EnumConstantDecl(SourceLocation L, IdentifierInfo *Id, QualType T, Expr *E, 420 const llvm::APSInt &V, ScopedDecl *PrevDecl) 421 : ValueDecl(EnumConstant, L, Id, T, PrevDecl), Init(E), Val(V) {} 422 423 const Expr *getInitExpr() const { return Init; } 424 Expr *getInitExpr() { return Init; } 425 const llvm::APSInt &getInitVal() const { return Val; } 426 427 void setInitExpr(Expr *E) { Init = E; } 428 void setInitVal(llvm::APSInt &V) { Val = V; } 429 430 // Implement isa/cast/dyncast/etc. 431 static bool classof(const Decl *D) { return D->getKind() == EnumConstant; } 432 static bool classof(const EnumConstantDecl *D) { return true; } 433}; 434 435 436/// TypeDecl - Represents a declaration of a type. 437/// 438class TypeDecl : public ScopedDecl { 439 /// TypeForDecl - This indicates the Type object that represents this 440 /// TypeDecl. It is a cache maintained by ASTContext::getTypedefType and 441 /// ASTContext::getTagDeclType. 442 Type *TypeForDecl; 443 friend class ASTContext; 444protected: 445 TypeDecl(Kind DK, SourceLocation L, IdentifierInfo *Id, ScopedDecl *PrevDecl) 446 : ScopedDecl(DK, L, Id, PrevDecl), TypeForDecl(0) {} 447public: 448 // Implement isa/cast/dyncast/etc. 449 static bool classof(const Decl *D) { 450 return D->getKind() >= TypeFirst && D->getKind() <= TypeLast; 451 } 452 static bool classof(const TypeDecl *D) { return true; } 453}; 454 455 456class TypedefDecl : public TypeDecl { 457 /// UnderlyingType - This is the type the typedef is set to. 458 QualType UnderlyingType; 459public: 460 TypedefDecl(SourceLocation L, IdentifierInfo *Id, QualType T, ScopedDecl *PD) 461 : TypeDecl(Typedef, L, Id, PD), UnderlyingType(T) {} 462 463 QualType getUnderlyingType() const { return UnderlyingType; } 464 void setUnderlyingType(QualType newType) { UnderlyingType = newType; } 465 466 // Implement isa/cast/dyncast/etc. 467 static bool classof(const Decl *D) { return D->getKind() == Typedef; } 468 static bool classof(const TypedefDecl *D) { return true; } 469}; 470 471 472/// TagDecl - Represents the declaration of a struct/union/class/enum. 473class TagDecl : public TypeDecl { 474 /// IsDefinition - True if this is a definition ("struct foo {};"), false if 475 /// it is a declaration ("struct foo;"). 476 bool IsDefinition : 1; 477protected: 478 TagDecl(Kind DK, SourceLocation L, IdentifierInfo *Id, ScopedDecl *PrevDecl) 479 : TypeDecl(DK, L, Id, PrevDecl) { 480 IsDefinition = false; 481 } 482public: 483 484 /// isDefinition - Return true if this decl has its body specified. 485 bool isDefinition() const { 486 return IsDefinition; 487 } 488 489 const char *getKindName() const { 490 switch (getKind()) { 491 default: assert(0 && "Unknown TagDecl!"); 492 case Struct: return "struct"; 493 case Union: return "union"; 494 case Class: return "class"; 495 case Enum: return "enum"; 496 } 497 } 498 499 // Implement isa/cast/dyncast/etc. 500 static bool classof(const Decl *D) { 501 return D->getKind() >= TagFirst && D->getKind() <= TagLast; 502 } 503 static bool classof(const TagDecl *D) { return true; } 504protected: 505 void setDefinition(bool V) { IsDefinition = V; } 506}; 507 508/// EnumDecl - Represents an enum. As an extension, we allow forward-declared 509/// enums. 510class EnumDecl : public TagDecl { 511 /// ElementList - this is a linked list of EnumConstantDecl's which are linked 512 /// together through their getNextDeclarator pointers. 513 EnumConstantDecl *ElementList; 514 515 /// IntegerType - This represent the integer type that the enum corresponds 516 /// to for code generation purposes. Note that the enumerator constants may 517 /// have a different type than this does. 518 QualType IntegerType; 519public: 520 EnumDecl(SourceLocation L, IdentifierInfo *Id, ScopedDecl *PrevDecl) 521 : TagDecl(Enum, L, Id, PrevDecl) { 522 ElementList = 0; 523 } 524 525 /// defineElements - When created, EnumDecl correspond to a forward declared 526 /// enum. This method is used to mark the decl as being defined, with the 527 /// specified list of enums. 528 void defineElements(EnumConstantDecl *ListHead, QualType NewType) { 529 assert(!isDefinition() && "Cannot redefine enums!"); 530 ElementList = ListHead; 531 setDefinition(true); 532 533 IntegerType = NewType; 534 } 535 536 /// getIntegerType - Return the integer type this enum decl corresponds to. 537 /// This returns a null qualtype for an enum forward definition. 538 QualType getIntegerType() const { return IntegerType; } 539 540 /// getEnumConstantList - Return the first EnumConstantDecl in the enum. 541 /// 542 EnumConstantDecl *getEnumConstantList() { return ElementList; } 543 const EnumConstantDecl *getEnumConstantList() const { return ElementList; } 544 545 static bool classof(const Decl *D) { return D->getKind() == Enum; } 546 static bool classof(const EnumDecl *D) { return true; } 547}; 548 549 550/// RecordDecl - Represents a struct/union/class. For example: 551/// struct X; // Forward declaration, no "body". 552/// union Y { int A, B; }; // Has body with members A and B (FieldDecls). 553/// 554class RecordDecl : public TagDecl { 555 /// HasFlexibleArrayMember - This is true if this struct ends with a flexible 556 /// array member (e.g. int X[]) or if this union contains a struct that does. 557 /// If so, this cannot be contained in arrays or other structs as a member. 558 bool HasFlexibleArrayMember : 1; 559 560 /// Members/NumMembers - This is a new[]'d array of pointers to Decls. 561 FieldDecl **Members; // Null if not defined. 562 int NumMembers; // -1 if not defined. 563public: 564 RecordDecl(Kind DK, SourceLocation L, IdentifierInfo *Id, ScopedDecl*PrevDecl) 565 : TagDecl(DK, L, Id, PrevDecl) { 566 HasFlexibleArrayMember = false; 567 assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!"); 568 Members = 0; 569 NumMembers = -1; 570 } 571 572 bool hasFlexibleArrayMember() const { return HasFlexibleArrayMember; } 573 void setHasFlexibleArrayMember(bool V) { HasFlexibleArrayMember = V; } 574 575 /// getNumMembers - Return the number of members, or -1 if this is a forward 576 /// definition. 577 int getNumMembers() const { return NumMembers; } 578 const FieldDecl *getMember(unsigned i) const { return Members[i]; } 579 FieldDecl *getMember(unsigned i) { return Members[i]; } 580 581 /// defineBody - When created, RecordDecl's correspond to a forward declared 582 /// record. This method is used to mark the decl as being defined, with the 583 /// specified contents. 584 void defineBody(FieldDecl **Members, unsigned numMembers); 585 586 /// getMember - If the member doesn't exist, or there are no members, this 587 /// function will return 0; 588 FieldDecl *getMember(IdentifierInfo *name); 589 590 static bool classof(const Decl *D) { 591 return D->getKind() >= RecordFirst && D->getKind() <= RecordLast; 592 } 593 static bool classof(const RecordDecl *D) { return true; } 594}; 595 596} // end namespace clang 597 598#endif 599