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