Decl.h revision 077bf5e2f48acfa9e7d69429b6e4ba86ea14896d
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 subclasses. 11// 12//===----------------------------------------------------------------------===// 13 14#ifndef LLVM_CLANG_AST_DECL_H 15#define LLVM_CLANG_AST_DECL_H 16 17#include "clang/Basic/IdentifierTable.h" 18#include "clang/Basic/OperatorKinds.h" 19#include "clang/AST/DeclarationName.h" 20#include "clang/AST/DeclBase.h" 21#include "clang/Parse/AccessSpecifier.h" 22#include "llvm/ADT/SmallVector.h" 23 24namespace clang { 25class Expr; 26class Stmt; 27class CompoundStmt; 28class StringLiteral; 29 30/// TranslationUnitDecl - The top declaration context. 31/// FIXME: The TranslationUnit class should probably be modified to serve as 32/// the top decl context. It would have ownership of the top decls so that the 33/// AST is self-contained and easily de/serializable. 34class TranslationUnitDecl : public Decl, public DeclContext { 35 TranslationUnitDecl() 36 : Decl(TranslationUnit, SourceLocation()), 37 DeclContext(TranslationUnit) {} 38public: 39 static TranslationUnitDecl *Create(ASTContext &C); 40 // Implement isa/cast/dyncast/etc. 41 static bool classof(const Decl *D) { return D->getKind() == TranslationUnit; } 42 static bool classof(const TranslationUnitDecl *D) { return true; } 43 static DeclContext *castToDeclContext(const TranslationUnitDecl *D) { 44 return static_cast<DeclContext *>(const_cast<TranslationUnitDecl*>(D)); 45 } 46 static TranslationUnitDecl *castFromDeclContext(const DeclContext *DC) { 47 return static_cast<TranslationUnitDecl *>(const_cast<DeclContext*>(DC)); 48 } 49 50protected: 51 /// EmitImpl - Serialize this TranslationUnitDecl. Called by Decl::Emit. 52 virtual void EmitImpl(llvm::Serializer& S) const; 53 54 /// CreateImpl - Deserialize a TranslationUnitDecl. Called by Decl::Create. 55 static TranslationUnitDecl* CreateImpl(llvm::Deserializer& D, ASTContext& C); 56 57 friend Decl* Decl::Create(llvm::Deserializer& D, ASTContext& C); 58}; 59 60/// NamedDecl - This represents a decl with a name. Many 61/// decls have names, but not ObjCMethodDecl, @class, etc. 62class NamedDecl : public Decl { 63 /// Name - The name of this declaration, which is typically a normal 64 /// identifier but may also be a special kind of name (C++ 65 /// constructor, Objective-C selector, etc.) 66 DeclarationName Name; 67 68protected: 69 NamedDecl(Kind DK, SourceLocation L, DeclarationName N) 70 : Decl(DK, L), Name(N) {} 71 72public: 73 NamedDecl(Kind DK, SourceLocation L, IdentifierInfo *Id) 74 : Decl(DK, L), Name(Id) {} 75 76 /// getIdentifier - Get the identifier that names this declaration, 77 /// if there is one. This will return NULL if this declaration has 78 /// no name (e.g., for an unnamed class) or if the name is a special 79 /// name (C++ constructor, Objective-C selector, etc.). 80 IdentifierInfo *getIdentifier() const { return Name.getAsIdentifierInfo(); } 81 82 /// getIdentifierName - Get the name of identifier for this 83 /// declaration as a string. If the declaration has no name, or if 84 /// the name is a special name (C++ constructor, Objective-C 85 /// selector, etc.), returns NULL. 86 const char *getIdentifierName() const { 87 if (IdentifierInfo *II = getIdentifier()) 88 return II->getName(); 89 else 90 return 0; 91 } 92 93 /// getDeclName - Get the actual, stored name of the declaration, 94 /// which may be a special name. 95 DeclarationName getDeclName() const { return Name; } 96 97 /// getName - Get a human-readable name for the declaration, even if 98 /// it is one of the special kinds of names (C++ constructor, 99 /// Objective-C selector, etc.). Creating this name requires some 100 /// expensive string manipulation, so it should be called only when 101 /// absolutely critical. For simple declarations, @c 102 /// getIdentifierName() should suffice. 103 std::string getName() const { return Name.getAsString(); } 104 std::string getNameAsString() const { return Name.getAsString(); } 105 106 static bool classof(const Decl *D) { 107 return D->getKind() >= NamedFirst && D->getKind() <= NamedLast; 108 } 109 static bool classof(const NamedDecl *D) { return true; } 110 111protected: 112 void EmitInRec(llvm::Serializer& S) const; 113 void ReadInRec(llvm::Deserializer& D, ASTContext& C); 114}; 115 116/// ScopedDecl - Represent lexically scoped names, used for all ValueDecl's 117/// and TypeDecl's. 118class ScopedDecl : public NamedDecl { 119 /// NextDeclarator - If this decl was part of a multi-declarator declaration, 120 /// such as "int X, Y, *Z;" this indicates Decl for the next declarator. 121 ScopedDecl *NextDeclarator; 122 123 /// When this decl is in scope while parsing, the Next field contains a 124 /// pointer to the shadowed decl of the same name. When the scope is popped, 125 /// Decls are relinked onto a containing decl object. 126 /// 127 ScopedDecl *Next; 128 129 /// DeclCtx - Holds either a DeclContext* or a MultipleDC*. 130 /// For declarations that don't contain C++ scope specifiers, it contains 131 /// the DeclContext where the ScopedDecl was declared. 132 /// For declarations with C++ scope specifiers, it contains a MultipleDC* 133 /// with the context where it semantically belongs (SemanticDC) and the 134 /// context where it was lexically declared (LexicalDC). 135 /// e.g.: 136 /// 137 /// namespace A { 138 /// void f(); // SemanticDC == LexicalDC == 'namespace A' 139 /// } 140 /// void A::f(); // SemanticDC == namespace 'A' 141 /// // LexicalDC == global namespace 142 uintptr_t DeclCtx; 143 144 struct MultipleDC { 145 DeclContext *SemanticDC; 146 DeclContext *LexicalDC; 147 }; 148 149 inline bool isInSemaDC() const { return (DeclCtx & 0x1) == 0; } 150 inline bool isOutOfSemaDC() const { return (DeclCtx & 0x1) != 0; } 151 inline MultipleDC *getMultipleDC() const { 152 return reinterpret_cast<MultipleDC*>(DeclCtx & ~0x1); 153 } 154 155protected: 156 ScopedDecl(Kind DK, DeclContext *DC, SourceLocation L, 157 DeclarationName N, ScopedDecl *PrevDecl) 158 : NamedDecl(DK, L, N), NextDeclarator(PrevDecl), Next(0), 159 DeclCtx(reinterpret_cast<uintptr_t>(DC)) {} 160 161 virtual ~ScopedDecl(); 162 163public: 164 const DeclContext *getDeclContext() const { 165 if (isInSemaDC()) 166 return reinterpret_cast<DeclContext*>(DeclCtx); 167 return getMultipleDC()->SemanticDC; 168 } 169 DeclContext *getDeclContext() { 170 return const_cast<DeclContext*>( 171 const_cast<const ScopedDecl*>(this)->getDeclContext()); 172 } 173 174 /// getLexicalDeclContext - The declaration context where this ScopedDecl was 175 /// lexically declared (LexicalDC). May be different from 176 /// getDeclContext() (SemanticDC). 177 /// e.g.: 178 /// 179 /// namespace A { 180 /// void f(); // SemanticDC == LexicalDC == 'namespace A' 181 /// } 182 /// void A::f(); // SemanticDC == namespace 'A' 183 /// // LexicalDC == global namespace 184 const DeclContext *getLexicalDeclContext() const { 185 if (isInSemaDC()) 186 return reinterpret_cast<DeclContext*>(DeclCtx); 187 return getMultipleDC()->LexicalDC; 188 } 189 DeclContext *getLexicalDeclContext() { 190 return const_cast<DeclContext*>( 191 const_cast<const ScopedDecl*>(this)->getLexicalDeclContext()); 192 } 193 194 void setLexicalDeclContext(DeclContext *DC); 195 196 ScopedDecl *getNext() const { return Next; } 197 void setNext(ScopedDecl *N) { Next = N; } 198 199 /// getNextDeclarator - If this decl was part of a multi-declarator 200 /// declaration, such as "int X, Y, *Z;" this returns the decl for the next 201 /// declarator. Otherwise it returns null. 202 ScopedDecl *getNextDeclarator() { return NextDeclarator; } 203 const ScopedDecl *getNextDeclarator() const { return NextDeclarator; } 204 void setNextDeclarator(ScopedDecl *N) { NextDeclarator = N; } 205 206 // isDefinedOutsideFunctionOrMethod - This predicate returns true if this 207 // scoped decl is defined outside the current function or method. This is 208 // roughly global variables and functions, but also handles enums (which could 209 // be defined inside or outside a function etc). 210 bool isDefinedOutsideFunctionOrMethod() const { 211 if (getDeclContext()) 212 return !getDeclContext()->isFunctionOrMethod(); 213 else 214 return true; 215 } 216 217 // Implement isa/cast/dyncast/etc. 218 static bool classof(const Decl *D) { 219 return D->getKind() >= ScopedFirst && D->getKind() <= ScopedLast; 220 } 221 static bool classof(const ScopedDecl *D) { return true; } 222 223protected: 224 void EmitInRec(llvm::Serializer& S) const; 225 void ReadInRec(llvm::Deserializer& D, ASTContext& C); 226 227 void EmitOutRec(llvm::Serializer& S) const; 228 void ReadOutRec(llvm::Deserializer& D, ASTContext& C); 229 230 friend void Decl::Destroy(ASTContext& C); 231}; 232 233/// NamespaceDecl - Represent a C++ namespace. 234class NamespaceDecl : public ScopedDecl, public DeclContext { 235 SourceLocation LBracLoc, RBracLoc; 236 237 // For extended namespace definitions: 238 // 239 // namespace A { int x; } 240 // namespace A { int y; } 241 // 242 // there will be one NamespaceDecl for each declaration. 243 // NextDeclarator points to the next extended declaration. 244 // OrigNamespace points to the original namespace declaration. 245 // OrigNamespace of the first namespace decl points to itself. 246 247 NamespaceDecl *OrigNamespace; 248 249 NamespaceDecl(DeclContext *DC, SourceLocation L, IdentifierInfo *Id) 250 : ScopedDecl(Namespace, DC, L, Id, 0), DeclContext(Namespace) { 251 OrigNamespace = this; 252 } 253public: 254 static NamespaceDecl *Create(ASTContext &C, DeclContext *DC, 255 SourceLocation L, IdentifierInfo *Id); 256 257 virtual void Destroy(ASTContext& C); 258 259 NamespaceDecl *getNextNamespace() { 260 return cast_or_null<NamespaceDecl>(getNextDeclarator()); 261 } 262 const NamespaceDecl *getNextNamespace() const { 263 return cast_or_null<NamespaceDecl>(getNextDeclarator()); 264 } 265 void setNextNamespace(NamespaceDecl *ND) { setNextDeclarator(ND); } 266 267 NamespaceDecl *getOriginalNamespace() const { 268 return OrigNamespace; 269 } 270 void setOriginalNamespace(NamespaceDecl *ND) { OrigNamespace = ND; } 271 272 SourceRange getSourceRange() const { 273 return SourceRange(LBracLoc, RBracLoc); 274 } 275 276 SourceLocation getLBracLoc() const { return LBracLoc; } 277 SourceLocation getRBracLoc() const { return RBracLoc; } 278 void setLBracLoc(SourceLocation LBrace) { LBracLoc = LBrace; } 279 void setRBracLoc(SourceLocation RBrace) { RBracLoc = RBrace; } 280 281 // Implement isa/cast/dyncast/etc. 282 static bool classof(const Decl *D) { return D->getKind() == Namespace; } 283 static bool classof(const NamespaceDecl *D) { return true; } 284 static DeclContext *castToDeclContext(const NamespaceDecl *D) { 285 return static_cast<DeclContext *>(const_cast<NamespaceDecl*>(D)); 286 } 287 static NamespaceDecl *castFromDeclContext(const DeclContext *DC) { 288 return static_cast<NamespaceDecl *>(const_cast<DeclContext*>(DC)); 289 } 290 291protected: 292 /// EmitImpl - Serialize this NamespaceDecl. Called by Decl::Emit. 293 virtual void EmitImpl(llvm::Serializer& S) const; 294 295 /// CreateImpl - Deserialize a NamespaceDecl. Called by Decl::Create. 296 static NamespaceDecl* CreateImpl(llvm::Deserializer& D, ASTContext& C); 297 298 friend Decl* Decl::Create(llvm::Deserializer& D, ASTContext& C); 299}; 300 301/// ValueDecl - Represent the declaration of a variable (in which case it is 302/// an lvalue) a function (in which case it is a function designator) or 303/// an enum constant. 304class ValueDecl : public ScopedDecl { 305 QualType DeclType; 306 307protected: 308 ValueDecl(Kind DK, DeclContext *DC, SourceLocation L, 309 DeclarationName N, QualType T, ScopedDecl *PrevDecl) 310 : ScopedDecl(DK, DC, L, N, PrevDecl), DeclType(T) {} 311public: 312 QualType getType() const { return DeclType; } 313 void setType(QualType newType) { DeclType = newType; } 314 315 // Implement isa/cast/dyncast/etc. 316 static bool classof(const Decl *D) { 317 return D->getKind() >= ValueFirst && D->getKind() <= ValueLast; 318 } 319 static bool classof(const ValueDecl *D) { return true; } 320 321protected: 322 void EmitInRec(llvm::Serializer& S) const; 323 void ReadInRec(llvm::Deserializer& D, ASTContext& C); 324}; 325 326/// VarDecl - An instance of this class is created to represent a variable 327/// declaration or definition. 328class VarDecl : public ValueDecl { 329public: 330 enum StorageClass { 331 None, Auto, Register, Extern, Static, PrivateExtern 332 }; 333private: 334 Stmt *Init; 335 // FIXME: This can be packed into the bitfields in Decl. 336 unsigned SClass : 3; 337 bool ThreadSpecified : 1; 338 bool HasCXXDirectInit : 1; 339 340 // Move to DeclGroup when it is implemented. 341 SourceLocation TypeSpecStartLoc; 342 friend class StmtIteratorBase; 343protected: 344 VarDecl(Kind DK, DeclContext *DC, SourceLocation L, IdentifierInfo *Id, 345 QualType T, StorageClass SC, ScopedDecl *PrevDecl, 346 SourceLocation TSSL = SourceLocation()) 347 : ValueDecl(DK, DC, L, Id, T, PrevDecl), Init(0), 348 ThreadSpecified(false), HasCXXDirectInit(false), 349 TypeSpecStartLoc(TSSL) { SClass = SC; } 350public: 351 static VarDecl *Create(ASTContext &C, DeclContext *DC, 352 SourceLocation L, IdentifierInfo *Id, 353 QualType T, StorageClass S, ScopedDecl *PrevDecl, 354 SourceLocation TypeSpecStartLoc = SourceLocation()); 355 356 StorageClass getStorageClass() const { return (StorageClass)SClass; } 357 358 SourceLocation getTypeSpecStartLoc() const { return TypeSpecStartLoc; } 359 360 const Expr *getInit() const { return (const Expr*) Init; } 361 Expr *getInit() { return (Expr*) Init; } 362 void setInit(Expr *I) { Init = (Stmt*) I; } 363 364 void setThreadSpecified(bool T) { ThreadSpecified = T; } 365 bool isThreadSpecified() const { 366 return ThreadSpecified; 367 } 368 369 void setCXXDirectInitializer(bool T) { HasCXXDirectInit = T; } 370 371 /// hasCXXDirectInitializer - If true, the initializer was a direct 372 /// initializer, e.g: "int x(1);". The Init expression will be the expression 373 /// inside the parens or a "ClassType(a,b,c)" class constructor expression for 374 /// class types. Clients can distinguish between "int x(1);" and "int x=1;" 375 /// by checking hasCXXDirectInitializer. 376 /// 377 bool hasCXXDirectInitializer() const { 378 return HasCXXDirectInit; 379 } 380 381 /// hasLocalStorage - Returns true if a variable with function scope 382 /// is a non-static local variable. 383 bool hasLocalStorage() const { 384 if (getStorageClass() == None) 385 return !isFileVarDecl(); 386 387 // Return true for: Auto, Register. 388 // Return false for: Extern, Static, PrivateExtern. 389 390 return getStorageClass() <= Register; 391 } 392 393 /// hasGlobalStorage - Returns true for all variables that do not 394 /// have local storage. This includs all global variables as well 395 /// as static variables declared within a function. 396 bool hasGlobalStorage() const { return !hasLocalStorage(); } 397 398 /// isBlockVarDecl - Returns true for local variable declarations. Note that 399 /// this includes static variables inside of functions. 400 /// 401 /// void foo() { int x; static int y; extern int z; } 402 /// 403 bool isBlockVarDecl() const { 404 if (getKind() != Decl::Var) 405 return false; 406 if (const DeclContext *DC = getDeclContext()) 407 return DC->isFunctionOrMethod(); 408 return false; 409 } 410 411 /// isFileVarDecl - Returns true for file scoped variable declaration. 412 bool isFileVarDecl() const { 413 if (getKind() != Decl::Var) 414 return false; 415 if (isa<TranslationUnitDecl>(getDeclContext()) || 416 isa<NamespaceDecl>(getDeclContext()) ) 417 return true; 418 return false; 419 } 420 421 // Implement isa/cast/dyncast/etc. 422 static bool classof(const Decl *D) { 423 return D->getKind() >= VarFirst && D->getKind() <= VarLast; 424 } 425 static bool classof(const VarDecl *D) { return true; } 426 427protected: 428 void EmitInRec(llvm::Serializer& S) const; 429 void ReadInRec(llvm::Deserializer& D, ASTContext& C); 430 431 void EmitOutRec(llvm::Serializer& S) const; 432 void ReadOutRec(llvm::Deserializer& D, ASTContext& C); 433 434 /// EmitImpl - Serialize this VarDecl. Called by Decl::Emit. 435 virtual void EmitImpl(llvm::Serializer& S) const; 436 437 /// ReadImpl - Deserialize this VarDecl. Called by subclasses. 438 virtual void ReadImpl(llvm::Deserializer& D, ASTContext& C); 439 440 /// CreateImpl - Deserialize a VarDecl. Called by Decl::Create. 441 static VarDecl* CreateImpl(llvm::Deserializer& D, ASTContext& C); 442}; 443 444class ImplicitParamDecl : public VarDecl { 445protected: 446 ImplicitParamDecl(Kind DK, DeclContext *DC, SourceLocation L, 447 IdentifierInfo *Id, QualType T, ScopedDecl *PrevDecl) 448 : VarDecl(DK, DC, L, Id, T, VarDecl::None, PrevDecl) {} 449public: 450 static ImplicitParamDecl *Create(ASTContext &C, DeclContext *DC, 451 SourceLocation L, IdentifierInfo *Id, 452 QualType T, ScopedDecl *PrevDecl); 453 // Implement isa/cast/dyncast/etc. 454 static bool classof(const ImplicitParamDecl *D) { return true; } 455 static bool classof(const Decl *D) { return D->getKind() == ImplicitParam; } 456}; 457 458/// ParmVarDecl - Represent a parameter to a function. 459class ParmVarDecl : public VarDecl { 460 // NOTE: VC++ treats enums as signed, avoid using the ObjCDeclQualifier enum 461 /// FIXME: Also can be paced into the bitfields in Decl. 462 /// in, inout, etc. 463 unsigned objcDeclQualifier : 6; 464 465 /// Default argument, if any. [C++ Only] 466 Expr *DefaultArg; 467 468 ParmVarDecl(DeclContext *DC, SourceLocation L, 469 IdentifierInfo *Id, QualType T, StorageClass S, 470 Expr *DefArg, ScopedDecl *PrevDecl) 471 : VarDecl(ParmVar, DC, L, Id, T, S, PrevDecl), 472 objcDeclQualifier(OBJC_TQ_None), DefaultArg(DefArg) {} 473 474public: 475 static ParmVarDecl *Create(ASTContext &C, DeclContext *DC, 476 SourceLocation L,IdentifierInfo *Id, 477 QualType T, StorageClass S, Expr *DefArg, 478 ScopedDecl *PrevDecl); 479 480 ObjCDeclQualifier getObjCDeclQualifier() const { 481 return ObjCDeclQualifier(objcDeclQualifier); 482 } 483 void setObjCDeclQualifier(ObjCDeclQualifier QTVal) 484 { objcDeclQualifier = QTVal; } 485 486 const Expr *getDefaultArg() const { return DefaultArg; } 487 Expr *getDefaultArg() { return DefaultArg; } 488 void setDefaultArg(Expr *defarg) { DefaultArg = defarg; } 489 490 // Implement isa/cast/dyncast/etc. 491 static bool classof(const Decl *D) { return D->getKind() == ParmVar; } 492 static bool classof(const ParmVarDecl *D) { return true; } 493 494protected: 495 /// EmitImpl - Serialize this ParmVarDecl. Called by Decl::Emit. 496 virtual void EmitImpl(llvm::Serializer& S) const; 497 498 /// CreateImpl - Deserialize a ParmVarDecl. Called by Decl::Create. 499 static ParmVarDecl* CreateImpl(llvm::Deserializer& D, ASTContext& C); 500 501 friend Decl* Decl::Create(llvm::Deserializer& D, ASTContext& C); 502}; 503 504/// FunctionDecl - An instance of this class is created to represent a 505/// function declaration or definition. 506/// 507/// Since a given function can be declared several times in a program, 508/// there may be several FunctionDecls that correspond to that 509/// function. Only one of those FunctionDecls will be found when 510/// traversing the list of declarations in the context of the 511/// FunctionDecl (e.g., the translation unit); this FunctionDecl 512/// contains all of the information known about the function. Other, 513/// previous declarations of the function are available via the 514/// getPreviousDeclaration() chain. 515class FunctionDecl : public ValueDecl, public DeclContext { 516public: 517 enum StorageClass { 518 None, Extern, Static, PrivateExtern 519 }; 520private: 521 /// ParamInfo - new[]'d array of pointers to VarDecls for the formal 522 /// parameters of this function. This is null if a prototype or if there are 523 /// no formals. TODO: we could allocate this space immediately after the 524 /// FunctionDecl object to save an allocation like FunctionType does. 525 ParmVarDecl **ParamInfo; 526 527 Stmt *Body; // Null if a prototype. 528 529 /// PreviousDeclaration - A link to the previous declaration of this 530 /// same function, NULL if this is the first declaration. For 531 /// example, in the following code, the PreviousDeclaration can be 532 /// traversed several times to see all three declarations of the 533 /// function "f", the last of which is also a definition. 534 /// 535 /// int f(int x, int y = 1); 536 /// int f(int x = 0, int y); 537 /// int f(int x, int y) { return x + y; } 538 FunctionDecl *PreviousDeclaration; 539 540 // NOTE: VC++ treats enums as signed, avoid using the StorageClass enum 541 unsigned SClass : 2; 542 bool IsInline : 1; 543 bool IsImplicit : 1; 544 545 // Move to DeclGroup when it is implemented. 546 SourceLocation TypeSpecStartLoc; 547protected: 548 FunctionDecl(Kind DK, DeclContext *DC, SourceLocation L, 549 DeclarationName N, QualType T, 550 StorageClass S, bool isInline, ScopedDecl *PrevDecl, 551 SourceLocation TSSL = SourceLocation()) 552 : ValueDecl(DK, DC, L, N, T, PrevDecl), 553 DeclContext(DK), 554 ParamInfo(0), Body(0), PreviousDeclaration(0), 555 SClass(S), IsInline(isInline), IsImplicit(0), TypeSpecStartLoc(TSSL) {} 556 557 virtual ~FunctionDecl(); 558 virtual void Destroy(ASTContext& C); 559 560public: 561 static FunctionDecl *Create(ASTContext &C, DeclContext *DC, SourceLocation L, 562 DeclarationName N, QualType T, 563 StorageClass S = None, bool isInline = false, 564 ScopedDecl *PrevDecl = 0, 565 SourceLocation TSStartLoc = SourceLocation()); 566 567 SourceLocation getTypeSpecStartLoc() const { return TypeSpecStartLoc; } 568 569 /// getBody - Retrieve the body (definition) of the function. The 570 /// function body might be in any of the (re-)declarations of this 571 /// function. The variant that accepts a FunctionDecl pointer will 572 /// set that function declaration to the actual declaration 573 /// containing the body (if there is one). 574 Stmt *getBody(const FunctionDecl *&Definition) const; 575 576 virtual Stmt *getBody() const { 577 const FunctionDecl* Definition; 578 return getBody(Definition); 579 } 580 581 /// isThisDeclarationADefinition - Returns whether this specific 582 /// declaration of the function is also a definition. This does not 583 /// determine whether the function has been defined (e.g., in a 584 /// previous definition); for that information, use getBody. 585 bool isThisDeclarationADefinition() const { return Body != 0; } 586 587 void setBody(Stmt *B) { Body = B; } 588 589 bool isImplicit() { return IsImplicit; } 590 void setImplicit() { IsImplicit = true; } 591 592 /// getPreviousDeclaration - Return the previous declaration of this 593 /// function. 594 const FunctionDecl *getPreviousDeclaration() const { 595 return PreviousDeclaration; 596 } 597 598 void setPreviousDeclaration(FunctionDecl * PrevDecl) { 599 PreviousDeclaration = PrevDecl; 600 } 601 602 // Iterator access to formal parameters. 603 unsigned param_size() const { return getNumParams(); } 604 typedef ParmVarDecl **param_iterator; 605 typedef ParmVarDecl * const *param_const_iterator; 606 607 param_iterator param_begin() { return ParamInfo; } 608 param_iterator param_end() { return ParamInfo+param_size(); } 609 610 param_const_iterator param_begin() const { return ParamInfo; } 611 param_const_iterator param_end() const { return ParamInfo+param_size(); } 612 613 unsigned getNumParams() const; 614 const ParmVarDecl *getParamDecl(unsigned i) const { 615 assert(i < getNumParams() && "Illegal param #"); 616 return ParamInfo[i]; 617 } 618 ParmVarDecl *getParamDecl(unsigned i) { 619 assert(i < getNumParams() && "Illegal param #"); 620 return ParamInfo[i]; 621 } 622 void setParams(ParmVarDecl **NewParamInfo, unsigned NumParams); 623 624 /// getMinRequiredArguments - Returns the minimum number of arguments 625 /// needed to call this function. This may be fewer than the number of 626 /// function parameters, if some of the parameters have default 627 /// arguments (in C++). 628 unsigned getMinRequiredArguments() const; 629 630 QualType getResultType() const { 631 return getType()->getAsFunctionType()->getResultType(); 632 } 633 StorageClass getStorageClass() const { return StorageClass(SClass); } 634 bool isInline() const { return IsInline; } 635 636 /// isOverloadedOperator - Whether this function declaration 637 /// represents an C++ overloaded operator, e.g., "operator+". 638 bool isOverloadedOperator() const { 639 return getOverloadedOperator() != OO_None; 640 }; 641 642 OverloadedOperatorKind getOverloadedOperator() const; 643 644 // Implement isa/cast/dyncast/etc. 645 static bool classof(const Decl *D) { 646 return D->getKind() >= FunctionFirst && D->getKind() <= FunctionLast; 647 } 648 static bool classof(const FunctionDecl *D) { return true; } 649 static DeclContext *castToDeclContext(const FunctionDecl *D) { 650 return static_cast<DeclContext *>(const_cast<FunctionDecl*>(D)); 651 } 652 static FunctionDecl *castFromDeclContext(const DeclContext *DC) { 653 return static_cast<FunctionDecl *>(const_cast<DeclContext*>(DC)); 654 } 655 656protected: 657 /// EmitImpl - Serialize this FunctionDecl. Called by Decl::Emit. 658 virtual void EmitImpl(llvm::Serializer& S) const; 659 660 /// CreateImpl - Deserialize a FunctionDecl. Called by Decl::Create. 661 static FunctionDecl* CreateImpl(llvm::Deserializer& D, ASTContext& C); 662 663 friend Decl* Decl::Create(llvm::Deserializer& D, ASTContext& C); 664 friend class CXXRecordDecl; 665}; 666 667 668/// FieldDecl - An instance of this class is created by Sema::ActOnField to 669/// represent a member of a struct/union/class. 670class FieldDecl : public NamedDecl { 671 QualType DeclType; 672 Expr *BitWidth; 673protected: 674 FieldDecl(Kind DK, SourceLocation L, IdentifierInfo *Id, QualType T, 675 Expr *BW = NULL) 676 : NamedDecl(DK, L, Id), DeclType(T), BitWidth(BW) {} 677 FieldDecl(SourceLocation L, IdentifierInfo *Id, QualType T, Expr *BW) 678 : NamedDecl(Field, L, Id), DeclType(T), BitWidth(BW) {} 679public: 680 static FieldDecl *Create(ASTContext &C, SourceLocation L, IdentifierInfo *Id, 681 QualType T, Expr *BW = NULL); 682 683 QualType getType() const { return DeclType; } 684 685 bool isBitField() const { return BitWidth != NULL; } 686 Expr *getBitWidth() const { return BitWidth; } 687 // Implement isa/cast/dyncast/etc. 688 static bool classof(const Decl *D) { 689 return D->getKind() >= FieldFirst && D->getKind() <= FieldLast; 690 } 691 static bool classof(const FieldDecl *D) { return true; } 692 693protected: 694 /// EmitImpl - Serialize this FieldDecl. Called by Decl::Emit. 695 virtual void EmitImpl(llvm::Serializer& S) const; 696 697 /// CreateImpl - Deserialize a FieldDecl. Called by Decl::Create. 698 static FieldDecl* CreateImpl(llvm::Deserializer& D, ASTContext& C); 699 700 friend Decl* Decl::Create(llvm::Deserializer& D, ASTContext& C); 701}; 702 703/// EnumConstantDecl - An instance of this object exists for each enum constant 704/// that is defined. For example, in "enum X {a,b}", each of a/b are 705/// EnumConstantDecl's, X is an instance of EnumDecl, and the type of a/b is a 706/// TagType for the X EnumDecl. 707class EnumConstantDecl : public ValueDecl { 708 Stmt *Init; // an integer constant expression 709 llvm::APSInt Val; // The value. 710protected: 711 EnumConstantDecl(DeclContext *DC, SourceLocation L, 712 IdentifierInfo *Id, QualType T, Expr *E, 713 const llvm::APSInt &V, ScopedDecl *PrevDecl) 714 : ValueDecl(EnumConstant, DC, L, Id, T, PrevDecl), Init((Stmt*)E), Val(V) {} 715 716 virtual ~EnumConstantDecl() {} 717public: 718 719 static EnumConstantDecl *Create(ASTContext &C, EnumDecl *DC, 720 SourceLocation L, IdentifierInfo *Id, 721 QualType T, Expr *E, 722 const llvm::APSInt &V, ScopedDecl *PrevDecl); 723 724 virtual void Destroy(ASTContext& C); 725 726 const Expr *getInitExpr() const { return (const Expr*) Init; } 727 Expr *getInitExpr() { return (Expr*) Init; } 728 const llvm::APSInt &getInitVal() const { return Val; } 729 730 void setInitExpr(Expr *E) { Init = (Stmt*) E; } 731 void setInitVal(const llvm::APSInt &V) { Val = V; } 732 733 // Implement isa/cast/dyncast/etc. 734 static bool classof(const Decl *D) { return D->getKind() == EnumConstant; } 735 static bool classof(const EnumConstantDecl *D) { return true; } 736 737 friend class StmtIteratorBase; 738 739protected: 740 /// EmitImpl - Serialize this EnumConstantDecl. Called by Decl::Emit. 741 virtual void EmitImpl(llvm::Serializer& S) const; 742 743 /// CreateImpl - Deserialize a EnumConstantDecl. Called by Decl::Create. 744 static EnumConstantDecl* CreateImpl(llvm::Deserializer& D, ASTContext& C); 745 746 friend Decl* Decl::Create(llvm::Deserializer& D, ASTContext& C); 747}; 748 749 750/// TypeDecl - Represents a declaration of a type. 751/// 752class TypeDecl : public ScopedDecl { 753 /// TypeForDecl - This indicates the Type object that represents this 754 /// TypeDecl. It is a cache maintained by ASTContext::getTypedefType and 755 /// ASTContext::getTagDeclType. 756 Type *TypeForDecl; 757 friend class ASTContext; 758protected: 759 TypeDecl(Kind DK, DeclContext *DC, SourceLocation L, 760 IdentifierInfo *Id, ScopedDecl *PrevDecl) 761 : ScopedDecl(DK, DC, L, Id, PrevDecl), TypeForDecl(0) {} 762public: 763 void setAccess(AccessSpecifier AS) { Access = AS; } 764 AccessSpecifier getAccess() const { return AccessSpecifier(Access); } 765 766 // Implement isa/cast/dyncast/etc. 767 static bool classof(const Decl *D) { 768 return D->getKind() >= TypeFirst && D->getKind() <= TypeLast; 769 } 770 static bool classof(const TypeDecl *D) { return true; } 771}; 772 773 774class TypedefDecl : public TypeDecl { 775 /// UnderlyingType - This is the type the typedef is set to. 776 QualType UnderlyingType; 777 TypedefDecl(DeclContext *DC, SourceLocation L, 778 IdentifierInfo *Id, QualType T, ScopedDecl *PD) 779 : TypeDecl(Typedef, DC, L, Id, PD), UnderlyingType(T) {} 780 781 virtual ~TypedefDecl() {} 782public: 783 784 static TypedefDecl *Create(ASTContext &C, DeclContext *DC, 785 SourceLocation L,IdentifierInfo *Id, 786 QualType T, ScopedDecl *PD); 787 788 QualType getUnderlyingType() const { return UnderlyingType; } 789 void setUnderlyingType(QualType newType) { UnderlyingType = newType; } 790 791 // Implement isa/cast/dyncast/etc. 792 static bool classof(const Decl *D) { return D->getKind() == Typedef; } 793 static bool classof(const TypedefDecl *D) { return true; } 794 795protected: 796 /// EmitImpl - Serialize this TypedefDecl. Called by Decl::Emit. 797 virtual void EmitImpl(llvm::Serializer& S) const; 798 799 /// CreateImpl - Deserialize a TypedefDecl. Called by Decl::Create. 800 static TypedefDecl* CreateImpl(llvm::Deserializer& D, ASTContext& C); 801 802 friend Decl* Decl::Create(llvm::Deserializer& D, ASTContext& C); 803 804}; 805 806 807/// TagDecl - Represents the declaration of a struct/union/class/enum. 808class TagDecl : public TypeDecl { 809public: 810 enum TagKind { 811 TK_struct, 812 TK_union, 813 TK_class, 814 TK_enum 815 }; 816 817private: 818 /// TagDeclKind - The TagKind enum. 819 unsigned TagDeclKind : 2; 820 821 /// IsDefinition - True if this is a definition ("struct foo {};"), false if 822 /// it is a declaration ("struct foo;"). 823 bool IsDefinition : 1; 824protected: 825 TagDecl(Kind DK, TagKind TK, DeclContext *DC, SourceLocation L, 826 IdentifierInfo *Id, ScopedDecl *PrevDecl) 827 : TypeDecl(DK, DC, L, Id, PrevDecl) { 828 assert((DK != Enum || TK == TK_enum) &&"EnumDecl not matched with TK_enum"); 829 TagDeclKind = TK; 830 IsDefinition = false; 831 } 832public: 833 834 /// isDefinition - Return true if this decl has its body specified. 835 bool isDefinition() const { 836 return IsDefinition; 837 } 838 839 /// getDefinition - Returns the TagDecl that actually defines this 840 /// struct/union/class/enum. When determining whether or not a 841 /// struct/union/class/enum is completely defined, one should use this method 842 /// as opposed to 'isDefinition'. 'isDefinition' indicates whether or not a 843 /// specific TagDecl is defining declaration, not whether or not the 844 /// struct/union/class/enum type is defined. This method returns NULL if 845 /// there is no TagDecl that defines the struct/union/class/enum. 846 TagDecl* getDefinition(ASTContext& C) const; 847 848 const char *getKindName() const { 849 switch (getTagKind()) { 850 default: assert(0 && "Unknown TagKind!"); 851 case TK_struct: return "struct"; 852 case TK_union: return "union"; 853 case TK_class: return "class"; 854 case TK_enum: return "enum"; 855 } 856 } 857 858 TagKind getTagKind() const { 859 return TagKind(TagDeclKind); 860 } 861 862 bool isStruct() const { return getTagKind() == TK_struct; } 863 bool isClass() const { return getTagKind() == TK_class; } 864 bool isUnion() const { return getTagKind() == TK_union; } 865 bool isEnum() const { return getTagKind() == TK_enum; } 866 867 // Implement isa/cast/dyncast/etc. 868 static bool classof(const Decl *D) { 869 return D->getKind() >= TagFirst && D->getKind() <= TagLast; 870 } 871 static bool classof(const TagDecl *D) { return true; } 872protected: 873 void setDefinition(bool V) { IsDefinition = V; } 874}; 875 876/// EnumDecl - Represents an enum. As an extension, we allow forward-declared 877/// enums. 878class EnumDecl : public TagDecl, public DeclContext { 879 // EnumDecl's DeclChain points to a linked list of EnumConstantDecl's which 880 // are linked together through their getNextDeclarator pointers. 881 882 /// IntegerType - This represent the integer type that the enum corresponds 883 /// to for code generation purposes. Note that the enumerator constants may 884 /// have a different type than this does. 885 QualType IntegerType; 886 887 EnumDecl(DeclContext *DC, SourceLocation L, 888 IdentifierInfo *Id, ScopedDecl *PrevDecl) 889 : TagDecl(Enum, TK_enum, DC, L, Id, PrevDecl), DeclContext(Enum) { 890 IntegerType = QualType(); 891 } 892public: 893 static EnumDecl *Create(ASTContext &C, DeclContext *DC, 894 SourceLocation L, IdentifierInfo *Id, 895 ScopedDecl *PrevDecl); 896 897 virtual void Destroy(ASTContext& C); 898 899 /// defineElements - When created, EnumDecl correspond to a forward declared 900 /// enum. This method is used to mark the decl as being defined, with the 901 /// specified list of enums. 902 void defineElements(EnumConstantDecl *ListHead, QualType NewType) { 903 assert(!isDefinition() && "Cannot redefine enums!"); 904 setDeclChain(ListHead); 905 setDefinition(true); 906 907 IntegerType = NewType; 908 } 909 910 /// getIntegerType - Return the integer type this enum decl corresponds to. 911 /// This returns a null qualtype for an enum forward definition. 912 QualType getIntegerType() const { return IntegerType; } 913 914 /// getEnumConstantList - Return the first EnumConstantDecl in the enum. 915 /// 916 EnumConstantDecl *getEnumConstantList() { 917 return cast_or_null<EnumConstantDecl>(getDeclChain()); 918 } 919 const EnumConstantDecl *getEnumConstantList() const { 920 return cast_or_null<const EnumConstantDecl>(getDeclChain()); 921 } 922 923 static bool classof(const Decl *D) { return D->getKind() == Enum; } 924 static bool classof(const EnumDecl *D) { return true; } 925 static DeclContext *castToDeclContext(const EnumDecl *D) { 926 return static_cast<DeclContext *>(const_cast<EnumDecl*>(D)); 927 } 928 static EnumDecl *castFromDeclContext(const DeclContext *DC) { 929 return static_cast<EnumDecl *>(const_cast<DeclContext*>(DC)); 930 } 931 932protected: 933 /// EmitImpl - Serialize this EnumDecl. Called by Decl::Emit. 934 virtual void EmitImpl(llvm::Serializer& S) const; 935 936 /// CreateImpl - Deserialize a EnumDecl. Called by Decl::Create. 937 static EnumDecl* CreateImpl(llvm::Deserializer& D, ASTContext& C); 938 939 friend Decl* Decl::Create(llvm::Deserializer& D, ASTContext& C); 940}; 941 942 943/// RecordDecl - Represents a struct/union/class. For example: 944/// struct X; // Forward declaration, no "body". 945/// union Y { int A, B; }; // Has body with members A and B (FieldDecls). 946/// This decl will be marked invalid if *any* members are invalid. 947/// 948class RecordDecl : public TagDecl { 949 /// HasFlexibleArrayMember - This is true if this struct ends with a flexible 950 /// array member (e.g. int X[]) or if this union contains a struct that does. 951 /// If so, this cannot be contained in arrays or other structs as a member. 952 bool HasFlexibleArrayMember : 1; 953 954 /// Members/NumMembers - This is a new[]'d array of pointers to Decls. 955 FieldDecl **Members; // Null if not defined. 956 int NumMembers; // -1 if not defined. 957 958protected: 959 RecordDecl(Kind DK, TagKind TK, DeclContext *DC, 960 SourceLocation L, IdentifierInfo *Id); 961 virtual ~RecordDecl(); 962 963public: 964 static RecordDecl *Create(ASTContext &C, TagKind TK, DeclContext *DC, 965 SourceLocation L, IdentifierInfo *Id, 966 RecordDecl* PrevDecl = 0); 967 968 virtual void Destroy(ASTContext& C); 969 970 bool hasFlexibleArrayMember() const { return HasFlexibleArrayMember; } 971 void setHasFlexibleArrayMember(bool V) { HasFlexibleArrayMember = V; } 972 973 /// getDefinition - Returns the RecordDecl that actually defines this 974 /// struct/union/class. When determining whether or not a struct/union/class 975 /// is completely defined, one should use this method as opposed to 976 /// 'isDefinition'. 'isDefinition' indicates whether or not a specific 977 /// RecordDecl is defining declaration, not whether or not the record 978 /// type is defined. This method returns NULL if there is no RecordDecl 979 /// that defines the struct/union/tag. 980 RecordDecl* getDefinition(ASTContext& C) const { 981 return cast_or_null<RecordDecl>(TagDecl::getDefinition(C)); 982 } 983 984 /// getNumMembers - Return the number of members, or -1 if this is a forward 985 /// definition. 986 int getNumMembers() const { return NumMembers; } 987 const FieldDecl *getMember(unsigned i) const { return Members[i]; } 988 FieldDecl *getMember(unsigned i) { return Members[i]; } 989 990 // Iterator access to field members. 991 typedef FieldDecl **field_iterator; 992 typedef FieldDecl * const *field_const_iterator; 993 994 field_iterator field_begin() { 995 assert(isDefinition() && "Not a definition!"); 996 return Members; 997 } 998 field_iterator field_end() { 999 assert(isDefinition() && "Not a definition!"); 1000 return Members + getNumMembers(); 1001 } 1002 1003 field_const_iterator field_begin() const { 1004 assert(isDefinition() && "Not a definition!"); 1005 return Members; 1006 } 1007 field_const_iterator field_end() const { 1008 assert(isDefinition() && "Not a definition!"); 1009 return Members + getNumMembers(); 1010 } 1011 1012 /// defineBody - When created, RecordDecl's correspond to a forward declared 1013 /// record. This method is used to mark the decl as being defined, with the 1014 /// specified contents. 1015 void defineBody(ASTContext& C, FieldDecl **Members, unsigned numMembers); 1016 1017 /// getMember - If the member doesn't exist, or there are no members, this 1018 /// function will return 0; 1019 FieldDecl *getMember(IdentifierInfo *name); 1020 1021 static bool classof(const Decl *D) { 1022 return D->getKind() >= RecordFirst && D->getKind() <= RecordLast; 1023 } 1024 static bool classof(const RecordDecl *D) { return true; } 1025 1026protected: 1027 /// EmitImpl - Serialize this RecordDecl. Called by Decl::Emit. 1028 virtual void EmitImpl(llvm::Serializer& S) const; 1029 1030 /// CreateImpl - Deserialize a RecordDecl. Called by Decl::Create. 1031 static RecordDecl* CreateImpl(llvm::Deserializer& D, ASTContext& C); 1032 1033 friend Decl* Decl::Create(llvm::Deserializer& D, ASTContext& C); 1034}; 1035 1036class FileScopeAsmDecl : public Decl { 1037 StringLiteral *AsmString; 1038 FileScopeAsmDecl(SourceLocation L, StringLiteral *asmstring) 1039 : Decl(FileScopeAsm, L), AsmString(asmstring) {} 1040public: 1041 static FileScopeAsmDecl *Create(ASTContext &C, SourceLocation L, 1042 StringLiteral *Str); 1043 1044 const StringLiteral *getAsmString() const { return AsmString; } 1045 StringLiteral *getAsmString() { return AsmString; } 1046 static bool classof(const Decl *D) { 1047 return D->getKind() == FileScopeAsm; 1048 } 1049 static bool classof(const FileScopeAsmDecl *D) { return true; } 1050protected: 1051 /// EmitImpl - Serialize this FileScopeAsmDecl. Called by Decl::Emit. 1052 virtual void EmitImpl(llvm::Serializer& S) const; 1053 1054 /// CreateImpl - Deserialize a FileScopeAsmDecl. Called by Decl::Create. 1055 static FileScopeAsmDecl* CreateImpl(llvm::Deserializer& D, ASTContext& C); 1056 1057 friend Decl* Decl::Create(llvm::Deserializer& D, ASTContext& C); 1058}; 1059 1060/// BlockDecl - This represents a block literal declaration, which is like an 1061/// unnamed FunctionDecl. For example: 1062/// ^{ statement-body } or ^(int arg1, float arg2){ statement-body } 1063/// 1064class BlockDecl : public Decl, public DeclContext { 1065 llvm::SmallVector<ParmVarDecl*, 8> Args; 1066 Stmt *Body; 1067 1068 // Since BlockDecl's aren't named/scoped, we need to store the context. 1069 DeclContext *ParentContext; 1070protected: 1071 BlockDecl(DeclContext *DC, SourceLocation CaretLoc) 1072 : Decl(Block, CaretLoc), DeclContext(Block), Body(0), ParentContext(DC) {} 1073 1074 virtual ~BlockDecl(); 1075 virtual void Destroy(ASTContext& C); 1076 1077public: 1078 static BlockDecl *Create(ASTContext &C, DeclContext *DC, SourceLocation L); 1079 1080 SourceLocation getCaretLocation() const { return getLocation(); } 1081 1082 Stmt *getBody() const { return Body; } 1083 void setBody(Stmt *B) { Body = B; } 1084 1085 void setArgs(ParmVarDecl **args, unsigned numargs) { 1086 Args.clear(); 1087 Args.insert(Args.begin(), args, args+numargs); 1088 } 1089 const DeclContext *getParentContext() const { return ParentContext; } 1090 DeclContext *getParentContext() { return ParentContext; } 1091 1092 /// arg_iterator - Iterate over the ParmVarDecl's for this block. 1093 typedef llvm::SmallVector<ParmVarDecl*, 8>::const_iterator param_iterator; 1094 bool param_empty() const { return Args.empty(); } 1095 param_iterator param_begin() const { return Args.begin(); } 1096 param_iterator param_end() const { return Args.end(); } 1097 1098 // Implement isa/cast/dyncast/etc. 1099 static bool classof(const Decl *D) { return D->getKind() == Block; } 1100 static bool classof(const BlockDecl *D) { return true; } 1101 static DeclContext *castToDeclContext(const BlockDecl *D) { 1102 return static_cast<DeclContext *>(const_cast<BlockDecl*>(D)); 1103 } 1104 static BlockDecl *castFromDeclContext(const DeclContext *DC) { 1105 return static_cast<BlockDecl *>(const_cast<DeclContext*>(DC)); 1106 } 1107 1108protected: 1109 /// EmitImpl - Serialize this BlockDecl. Called by Decl::Emit. 1110 virtual void EmitImpl(llvm::Serializer& S) const; 1111 1112 /// CreateImpl - Deserialize a BlockDecl. Called by Decl::Create. 1113 static BlockDecl* CreateImpl(llvm::Deserializer& D, ASTContext& C); 1114 1115 friend Decl* Decl::Create(llvm::Deserializer& D, ASTContext& C); 1116}; 1117 1118} // end namespace clang 1119 1120#endif 1121