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