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