Decl.h revision 6c2b6eb8d836da19007f7540709e16d5e39a1cba
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 CompatibleAlias, 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 = CompatibleAlias, 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 CompatibleAlias: 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 // Implement isa/cast/dyncast/etc. 251 static bool classof(const Decl *D) { 252 return D->getKind() >= ScopedFirst && D->getKind() <= ScopedLast; 253 } 254 static bool classof(const ScopedDecl *D) { return true; } 255 256protected: 257 void EmitInRec(llvm::Serializer& S) const; 258 void ReadInRec(llvm::Deserializer& D); 259 260 void EmitOutRec(llvm::Serializer& S) const; 261 void ReadOutRec(llvm::Deserializer& D); 262}; 263 264/// ValueDecl - Represent the declaration of a variable (in which case it is 265/// an lvalue) a function (in which case it is a function designator) or 266/// an enum constant. 267class ValueDecl : public ScopedDecl { 268 QualType DeclType; 269 270protected: 271 ValueDecl(Kind DK, SourceLocation L, IdentifierInfo *Id, QualType T, 272 ScopedDecl *PrevDecl) 273 : ScopedDecl(DK, L, Id, PrevDecl), DeclType(T) {} 274public: 275 QualType getType() const { return DeclType; } 276 void setType(QualType newType) { DeclType = newType; } 277 QualType getCanonicalType() const { return DeclType.getCanonicalType(); } 278 279 // Implement isa/cast/dyncast/etc. 280 static bool classof(const Decl *D) { 281 return D->getKind() >= ValueFirst && D->getKind() <= ValueLast; 282 } 283 static bool classof(const ValueDecl *D) { return true; } 284 285protected: 286 void EmitInRec(llvm::Serializer& S) const; 287 void ReadInRec(llvm::Deserializer& D); 288}; 289 290/// VarDecl - An instance of this class is created to represent a variable 291/// declaration or definition. 292class VarDecl : public ValueDecl { 293public: 294 enum StorageClass { 295 None, Auto, Register, Extern, Static, PrivateExtern 296 }; 297 StorageClass getStorageClass() const { return (StorageClass)SClass; } 298 299 const Expr *getInit() const { return Init; } 300 Expr *getInit() { return Init; } 301 void setInit(Expr *I) { Init = I; } 302 303 /// hasLocalStorage - Returns true if a variable with function scope 304 /// is a non-static local variable. 305 bool hasLocalStorage() const { 306 if (getStorageClass() == None) 307 return getKind() != FileVar; 308 309 // Return true for: Auto, Register. 310 // Return false for: Extern, Static, PrivateExtern. 311 312 return getStorageClass() <= Register; 313 } 314 315 /// hasGlobalStorage - Returns true for all variables that do not 316 /// have local storage. This includs all global variables as well 317 /// as static variables declared within a function. 318 bool hasGlobalStorage() const { return !hasLocalStorage(); } 319 320 // Implement isa/cast/dyncast/etc. 321 static bool classof(const Decl *D) { 322 return D->getKind() >= VarFirst && D->getKind() <= VarLast; 323 } 324 static bool classof(const VarDecl *D) { return true; } 325protected: 326 VarDecl(Kind DK, SourceLocation L, IdentifierInfo *Id, QualType T, 327 StorageClass SC, ScopedDecl *PrevDecl) 328 : ValueDecl(DK, L, Id, T, PrevDecl), Init(0) { SClass = SC; } 329private: 330 Expr *Init; 331 // FIXME: This can be packed into the bitfields in Decl. 332 unsigned SClass : 3; 333 334 friend class StmtIteratorBase; 335 336protected: 337 void EmitInRec(llvm::Serializer& S) const; 338 void ReadInRec(llvm::Deserializer& D); 339 340 void EmitOutRec(llvm::Serializer& S) const; 341 void ReadOutRec(llvm::Deserializer& D); 342 343 /// EmitImpl - Serialize this VarDecl. Called by Decl::Emit. 344 virtual void EmitImpl(llvm::Serializer& S) const; 345 346 /// ReadImpl - Deserialize this VarDecl. Called by subclasses. 347 virtual void ReadImpl(llvm::Deserializer& S); 348}; 349 350/// BlockVarDecl - Represent a local variable declaration. 351class BlockVarDecl : public VarDecl { 352public: 353 BlockVarDecl(SourceLocation L, IdentifierInfo *Id, QualType T, StorageClass S, 354 ScopedDecl *PrevDecl) 355 : VarDecl(BlockVar, L, Id, T, S, PrevDecl) {} 356 357 // Implement isa/cast/dyncast/etc. 358 static bool classof(const Decl *D) { return D->getKind() == BlockVar; } 359 static bool classof(const BlockVarDecl *D) { return true; } 360 361protected: 362 /// CreateImpl - Deserialize a BlockVarDecl. Called by Decl::Create. 363 static BlockVarDecl* CreateImpl(llvm::Deserializer& D); 364 365 friend Decl* Decl::Create(llvm::Deserializer& D); 366}; 367 368/// FileVarDecl - Represent a file scoped variable declaration. This 369/// will allow us to reason about external variable declarations and tentative 370/// definitions (C99 6.9.2p2) using our type system (without storing a 371/// pointer to the decl's scope, which is transient). 372class FileVarDecl : public VarDecl { 373public: 374 FileVarDecl(SourceLocation L, IdentifierInfo *Id, QualType T, StorageClass S, 375 ScopedDecl *PrevDecl) 376 : VarDecl(FileVar, L, Id, T, S, PrevDecl) {} 377 378 // Implement isa/cast/dyncast/etc. 379 static bool classof(const Decl *D) { return D->getKind() == FileVar; } 380 static bool classof(const FileVarDecl *D) { return true; } 381 382protected: 383 /// CreateImpl - Deserialize a FileVarDecl. Called by Decl::Create. 384 static FileVarDecl* CreateImpl(llvm::Deserializer& D); 385 386 friend Decl* Decl::Create(llvm::Deserializer& D); 387}; 388 389/// ParmVarDecl - Represent a parameter to a function. 390class ParmVarDecl : public VarDecl { 391public: 392 ParmVarDecl(SourceLocation L, IdentifierInfo *Id, QualType T, StorageClass S, 393 ScopedDecl *PrevDecl) 394 : VarDecl(ParmVar, L, Id, T, S, PrevDecl), 395 objcDeclQualifier(OBJC_TQ_None) {} 396 397 ObjCDeclQualifier getObjCDeclQualifier() const { 398 return ObjCDeclQualifier(objcDeclQualifier); 399 } 400 void setObjCDeclQualifier(ObjCDeclQualifier QTVal) 401 { objcDeclQualifier = QTVal; } 402 403 // Implement isa/cast/dyncast/etc. 404 static bool classof(const Decl *D) { return D->getKind() == ParmVar; } 405 static bool classof(const ParmVarDecl *D) { return true; } 406 407private: 408 // NOTE: VC++ treats enums as signed, avoid using the ObjCDeclQualifier enum 409 /// FIXME: Also can be paced into the bitfields in Decl. 410 /// in, inout, etc. 411 unsigned objcDeclQualifier : 6; 412 413protected: 414 /// EmitImpl - Serialize this ParmVarDecl. Called by Decl::Emit. 415 virtual void EmitImpl(llvm::Serializer& S) const; 416 417 /// CreateImpl - Deserialize a ParmVarDecl. Called by Decl::Create. 418 static ParmVarDecl* CreateImpl(llvm::Deserializer& D); 419 420 friend Decl* Decl::Create(llvm::Deserializer& D); 421}; 422 423/// FunctionDecl - An instance of this class is created to represent a function 424/// declaration or definition. 425class FunctionDecl : public ValueDecl { 426public: 427 enum StorageClass { 428 None, Extern, Static, PrivateExtern 429 }; 430 FunctionDecl(SourceLocation L, IdentifierInfo *Id, QualType T, 431 StorageClass S = None, bool isInline = false, 432 ScopedDecl *PrevDecl = 0) 433 : ValueDecl(Function, L, Id, T, PrevDecl), 434 ParamInfo(0), Body(0), DeclChain(0), SClass(S), IsInline(isInline) {} 435 virtual ~FunctionDecl(); 436 437 Stmt *getBody() const { return Body; } 438 void setBody(Stmt *B) { Body = B; } 439 440 ScopedDecl *getDeclChain() const { return DeclChain; } 441 void setDeclChain(ScopedDecl *D) { DeclChain = D; } 442 443 // Iterator access to formal parameters. 444 unsigned param_size() const { return getNumParams(); } 445 typedef ParmVarDecl **param_iterator; 446 typedef ParmVarDecl * const *param_const_iterator; 447 param_iterator param_begin() { return ParamInfo; } 448 param_iterator param_end() { return ParamInfo+param_size(); } 449 param_const_iterator param_begin() const { return ParamInfo; } 450 param_const_iterator param_end() const { return ParamInfo+param_size(); } 451 452 unsigned getNumParams() const; 453 const ParmVarDecl *getParamDecl(unsigned i) const { 454 assert(i < getNumParams() && "Illegal param #"); 455 return ParamInfo[i]; 456 } 457 ParmVarDecl *getParamDecl(unsigned i) { 458 assert(i < getNumParams() && "Illegal param #"); 459 return ParamInfo[i]; 460 } 461 void setParams(ParmVarDecl **NewParamInfo, unsigned NumParams); 462 463 QualType getResultType() const { 464 return cast<FunctionType>(getType())->getResultType(); 465 } 466 StorageClass getStorageClass() const { return StorageClass(SClass); } 467 bool isInline() const { return IsInline; } 468 469 // Implement isa/cast/dyncast/etc. 470 static bool classof(const Decl *D) { return D->getKind() == Function; } 471 static bool classof(const FunctionDecl *D) { return true; } 472 473private: 474 /// ParamInfo - new[]'d array of pointers to VarDecls for the formal 475 /// parameters of this function. This is null if a prototype or if there are 476 /// no formals. TODO: we could allocate this space immediately after the 477 /// FunctionDecl object to save an allocation like FunctionType does. 478 ParmVarDecl **ParamInfo; 479 480 Stmt *Body; // Null if a prototype. 481 482 /// DeclChain - Linked list of declarations that are defined inside this 483 /// function. 484 ScopedDecl *DeclChain; 485 486 // NOTE: VC++ treats enums as signed, avoid using the StorageClass enum 487 unsigned SClass : 2; 488 bool IsInline : 1; 489 490protected: 491 /// EmitImpl - Serialize this FunctionDecl. Called by Decl::Emit. 492 virtual void EmitImpl(llvm::Serializer& S) const; 493 494 /// CreateImpl - Deserialize a FunctionDecl. Called by Decl::Create. 495 static FunctionDecl* CreateImpl(llvm::Deserializer& D); 496 497 friend Decl* Decl::Create(llvm::Deserializer& D); 498}; 499 500 501/// FieldDecl - An instance of this class is created by Sema::ActOnField to 502/// represent a member of a struct/union/class. 503class FieldDecl : public NamedDecl { 504 QualType DeclType; 505 Expr *BitWidth; 506public: 507 FieldDecl(SourceLocation L, IdentifierInfo *Id, QualType T, 508 Expr *BW = NULL) 509 : NamedDecl(Field, L, Id), DeclType(T), BitWidth(BW) {} 510 FieldDecl(Kind DK, SourceLocation L, IdentifierInfo *Id, QualType T, 511 Expr *BW = NULL) 512 : NamedDecl(DK, L, Id), DeclType(T), BitWidth(BW) {} 513 514 QualType getType() const { return DeclType; } 515 QualType getCanonicalType() const { return DeclType.getCanonicalType(); } 516 517 bool isBitField() const { return BitWidth != NULL; } 518 Expr *getBitWidth() const { return BitWidth; } 519 // Implement isa/cast/dyncast/etc. 520 static bool classof(const Decl *D) { 521 return D->getKind() >= FieldFirst && D->getKind() <= FieldLast; 522 } 523 static bool classof(const FieldDecl *D) { return true; } 524 525protected: 526 /// EmitImpl - Serialize this FieldDecl. Called by Decl::Emit. 527 virtual void EmitImpl(llvm::Serializer& S) const; 528 529 /// CreateImpl - Deserialize a FieldDecl. Called by Decl::Create. 530 static FieldDecl* CreateImpl(llvm::Deserializer& D); 531 532 friend Decl* Decl::Create(llvm::Deserializer& D); 533}; 534 535/// EnumConstantDecl - An instance of this object exists for each enum constant 536/// that is defined. For example, in "enum X {a,b}", each of a/b are 537/// EnumConstantDecl's, X is an instance of EnumDecl, and the type of a/b is a 538/// TagType for the X EnumDecl. 539class EnumConstantDecl : public ValueDecl { 540 Expr *Init; // an integer constant expression 541 llvm::APSInt Val; // The value. 542protected: 543 EnumConstantDecl(SourceLocation L, IdentifierInfo *Id, QualType T, Expr *E, 544 const llvm::APSInt &V, ScopedDecl *PrevDecl) 545 : ValueDecl(EnumConstant, L, Id, T, PrevDecl), Init(E), Val(V) {} 546 ~EnumConstantDecl() {} 547public: 548 549 static EnumConstantDecl *Create(SourceLocation L, IdentifierInfo *Id, 550 QualType T, Expr *E, const llvm::APSInt &V, 551 ScopedDecl *PrevDecl, ASTContext &C); 552 553 const Expr *getInitExpr() const { return Init; } 554 Expr *getInitExpr() { return Init; } 555 const llvm::APSInt &getInitVal() const { return Val; } 556 557 void setInitExpr(Expr *E) { Init = E; } 558 void setInitVal(const llvm::APSInt &V) { Val = V; } 559 560 // Implement isa/cast/dyncast/etc. 561 static bool classof(const Decl *D) { return D->getKind() == EnumConstant; } 562 static bool classof(const EnumConstantDecl *D) { return true; } 563 564 friend class StmtIteratorBase; 565 566protected: 567 /// EmitImpl - Serialize this EnumConstantDecl. Called by Decl::Emit. 568 virtual void EmitImpl(llvm::Serializer& S) const; 569 570 /// CreateImpl - Deserialize a EnumConstantDecl. Called by Decl::Create. 571 static EnumConstantDecl* CreateImpl(llvm::Deserializer& D); 572 573 friend Decl* Decl::Create(llvm::Deserializer& D); 574}; 575 576 577/// TypeDecl - Represents a declaration of a type. 578/// 579class TypeDecl : public ScopedDecl { 580 /// TypeForDecl - This indicates the Type object that represents this 581 /// TypeDecl. It is a cache maintained by ASTContext::getTypedefType and 582 /// ASTContext::getTagDeclType. 583 Type *TypeForDecl; 584 friend class ASTContext; 585protected: 586 TypeDecl(Kind DK, SourceLocation L, IdentifierInfo *Id, ScopedDecl *PrevDecl) 587 : ScopedDecl(DK, L, Id, PrevDecl), TypeForDecl(0) {} 588public: 589 // Implement isa/cast/dyncast/etc. 590 static bool classof(const Decl *D) { 591 return D->getKind() >= TypeFirst && D->getKind() <= TypeLast; 592 } 593 static bool classof(const TypeDecl *D) { return true; } 594}; 595 596 597class TypedefDecl : public TypeDecl { 598 /// UnderlyingType - This is the type the typedef is set to. 599 QualType UnderlyingType; 600 TypedefDecl(SourceLocation L, IdentifierInfo *Id, QualType T, ScopedDecl *PD) 601 : TypeDecl(Typedef, L, Id, PD), UnderlyingType(T) {} 602 ~TypedefDecl() {} 603public: 604 605 static TypedefDecl *Create(SourceLocation L, IdentifierInfo *Id, QualType T, 606 ScopedDecl *PD, ASTContext &C); 607 608 QualType getUnderlyingType() const { return UnderlyingType; } 609 void setUnderlyingType(QualType newType) { UnderlyingType = newType; } 610 611 // Implement isa/cast/dyncast/etc. 612 static bool classof(const Decl *D) { return D->getKind() == Typedef; } 613 static bool classof(const TypedefDecl *D) { return true; } 614 615protected: 616 /// EmitImpl - Serialize this TypedefDecl. Called by Decl::Emit. 617 virtual void EmitImpl(llvm::Serializer& S) const; 618 619 /// CreateImpl - Deserialize a TypedefDecl. Called by Decl::Create. 620 static TypedefDecl* CreateImpl(llvm::Deserializer& D); 621 622 friend Decl* Decl::Create(llvm::Deserializer& D); 623}; 624 625 626/// TagDecl - Represents the declaration of a struct/union/class/enum. 627class TagDecl : public TypeDecl { 628 /// IsDefinition - True if this is a definition ("struct foo {};"), false if 629 /// it is a declaration ("struct foo;"). 630 bool IsDefinition : 1; 631protected: 632 TagDecl(Kind DK, SourceLocation L, IdentifierInfo *Id, ScopedDecl *PrevDecl) 633 : TypeDecl(DK, L, Id, PrevDecl) { 634 IsDefinition = false; 635 } 636public: 637 638 /// isDefinition - Return true if this decl has its body specified. 639 bool isDefinition() const { 640 return IsDefinition; 641 } 642 643 const char *getKindName() const { 644 switch (getKind()) { 645 default: assert(0 && "Unknown TagDecl!"); 646 case Struct: return "struct"; 647 case Union: return "union"; 648 case Class: return "class"; 649 case Enum: return "enum"; 650 } 651 } 652 653 // Implement isa/cast/dyncast/etc. 654 static bool classof(const Decl *D) { 655 return D->getKind() >= TagFirst && D->getKind() <= TagLast; 656 } 657 static bool classof(const TagDecl *D) { return true; } 658protected: 659 void setDefinition(bool V) { IsDefinition = V; } 660}; 661 662/// EnumDecl - Represents an enum. As an extension, we allow forward-declared 663/// enums. 664class EnumDecl : public TagDecl { 665 /// ElementList - this is a linked list of EnumConstantDecl's which are linked 666 /// together through their getNextDeclarator pointers. 667 EnumConstantDecl *ElementList; 668 669 /// IntegerType - This represent the integer type that the enum corresponds 670 /// to for code generation purposes. Note that the enumerator constants may 671 /// have a different type than this does. 672 QualType IntegerType; 673 674 EnumDecl(SourceLocation L, IdentifierInfo *Id, ScopedDecl *PrevDecl) 675 : TagDecl(Enum, L, Id, PrevDecl) { 676 ElementList = 0; 677 IntegerType = QualType(); 678 } 679 ~EnumDecl() {} 680public: 681 static EnumDecl *Create(SourceLocation L, IdentifierInfo *Id, 682 ScopedDecl *PrevDecl, ASTContext &C); 683 684 /// defineElements - When created, EnumDecl correspond to a forward declared 685 /// enum. This method is used to mark the decl as being defined, with the 686 /// specified list of enums. 687 void defineElements(EnumConstantDecl *ListHead, QualType NewType) { 688 assert(!isDefinition() && "Cannot redefine enums!"); 689 ElementList = ListHead; 690 setDefinition(true); 691 692 IntegerType = NewType; 693 } 694 695 /// getIntegerType - Return the integer type this enum decl corresponds to. 696 /// This returns a null qualtype for an enum forward definition. 697 QualType getIntegerType() const { return IntegerType; } 698 699 /// getEnumConstantList - Return the first EnumConstantDecl in the enum. 700 /// 701 EnumConstantDecl *getEnumConstantList() { return ElementList; } 702 const EnumConstantDecl *getEnumConstantList() const { return ElementList; } 703 704 static bool classof(const Decl *D) { return D->getKind() == Enum; } 705 static bool classof(const EnumDecl *D) { return true; } 706 707protected: 708 /// EmitImpl - Serialize this EnumDecl. Called by Decl::Emit. 709 virtual void EmitImpl(llvm::Serializer& S) const; 710 711 /// CreateImpl - Deserialize a EnumDecl. Called by Decl::Create. 712 static EnumDecl* CreateImpl(llvm::Deserializer& D); 713 714 friend Decl* Decl::Create(llvm::Deserializer& D); 715}; 716 717 718/// RecordDecl - Represents a struct/union/class. For example: 719/// struct X; // Forward declaration, no "body". 720/// union Y { int A, B; }; // Has body with members A and B (FieldDecls). 721/// This decl will be marked invalid if *any* members are invalid. 722/// 723class RecordDecl : public TagDecl { 724 /// HasFlexibleArrayMember - This is true if this struct ends with a flexible 725 /// array member (e.g. int X[]) or if this union contains a struct that does. 726 /// If so, this cannot be contained in arrays or other structs as a member. 727 bool HasFlexibleArrayMember : 1; 728 729 /// Members/NumMembers - This is a new[]'d array of pointers to Decls. 730 FieldDecl **Members; // Null if not defined. 731 int NumMembers; // -1 if not defined. 732 733 RecordDecl(Kind DK, SourceLocation L, IdentifierInfo *Id, 734 ScopedDecl *PrevDecl) : TagDecl(DK, L, Id, PrevDecl) { 735 HasFlexibleArrayMember = false; 736 assert(classof(static_cast<Decl*>(this)) && "Invalid Kind!"); 737 Members = 0; 738 NumMembers = -1; 739 } 740 741 ~RecordDecl() {} 742public: 743 744 static RecordDecl *Create(Kind DK, SourceLocation L, IdentifierInfo *Id, 745 ScopedDecl *PrevDecl, ASTContext &C); 746 747 bool hasFlexibleArrayMember() const { return HasFlexibleArrayMember; } 748 void setHasFlexibleArrayMember(bool V) { HasFlexibleArrayMember = V; } 749 750 /// getNumMembers - Return the number of members, or -1 if this is a forward 751 /// definition. 752 int getNumMembers() const { return NumMembers; } 753 const FieldDecl *getMember(unsigned i) const { return Members[i]; } 754 FieldDecl *getMember(unsigned i) { return Members[i]; } 755 756 /// defineBody - When created, RecordDecl's correspond to a forward declared 757 /// record. This method is used to mark the decl as being defined, with the 758 /// specified contents. 759 void defineBody(FieldDecl **Members, unsigned numMembers); 760 761 /// getMember - If the member doesn't exist, or there are no members, this 762 /// function will return 0; 763 FieldDecl *getMember(IdentifierInfo *name); 764 765 static bool classof(const Decl *D) { 766 return D->getKind() >= RecordFirst && D->getKind() <= RecordLast; 767 } 768 static bool classof(const RecordDecl *D) { return true; } 769 770protected: 771 /// EmitImpl - Serialize this RecordDecl. Called by Decl::Emit. 772 virtual void EmitImpl(llvm::Serializer& S) const; 773 774 /// CreateImpl - Deserialize a RecordDecl. Called by Decl::Create. 775 static RecordDecl* CreateImpl(Kind DK, llvm::Deserializer& D); 776 777 friend Decl* Decl::Create(llvm::Deserializer& D); 778}; 779 780class FileScopeAsmDecl : public Decl { 781 StringLiteral *AsmString; 782public: 783 FileScopeAsmDecl(SourceLocation L, StringLiteral *asmstring) 784 : Decl(FileScopeAsm, L), AsmString(asmstring) {} 785 786 const StringLiteral *getAsmString() const { return AsmString; } 787 StringLiteral *getAsmString() { return AsmString; } 788 static bool classof(const Decl *D) { 789 return D->getKind() == FileScopeAsm; 790 } 791 static bool classof(const FileScopeAsmDecl *D) { return true; } 792protected: 793 /// EmitImpl - Serialize this FileScopeAsmDecl. Called by Decl::Emit. 794 virtual void EmitImpl(llvm::Serializer& S) const; 795 796 /// CreateImpl - Deserialize a FileScopeAsmDecl. Called by Decl::Create. 797 static FileScopeAsmDecl* CreateImpl(llvm::Deserializer& D); 798 799 friend Decl* Decl::Create(llvm::Deserializer& D); 800}; 801 802/// LinkageSpecDecl - This represents a linkage specification. For example: 803/// extern "C" void foo(); 804/// 805class LinkageSpecDecl : public Decl { 806public: 807 /// LanguageIDs - Used to represent the language in a linkage 808 /// specification. The values are part of the serialization abi for 809 /// ASTs and cannot be changed without altering that abi. To help 810 /// ensure a stable abi for this, we choose the DW_LANG_ encodings 811 /// from the dwarf standard. 812 enum LanguageIDs { lang_c = /* DW_LANG_C */ 0x0002, 813 lang_cxx = /* DW_LANG_C_plus_plus */ 0x0004 }; 814private: 815 /// Language - The language for this linkage specification. 816 LanguageIDs Language; 817 /// D - This is the Decl of the linkage specification. 818 Decl *D; 819public: 820 LinkageSpecDecl(SourceLocation L, LanguageIDs lang, Decl *d) 821 : Decl(LinkageSpec, L), Language(lang), D(d) {} 822 823 LanguageIDs getLanguage() const { return Language; } 824 const Decl *getDecl() const { return D; } 825 Decl *getDecl() { return D; } 826 827 static bool classof(const Decl *D) { 828 return D->getKind() == LinkageSpec; 829 } 830 static bool classof(const LinkageSpecDecl *D) { return true; } 831 832protected: 833 void EmitInRec(llvm::Serializer& S) const; 834 void ReadInRec(llvm::Deserializer& D); 835}; 836 837} // end namespace clang 838 839#endif 840