Attr.h revision 0db29ece81d360dcefbe912339c34abe5917f6a9
1//===--- Attr.h - Classes for representing expressions ----------*- 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 Attr interface and subclasses. 11// 12//===----------------------------------------------------------------------===// 13 14#ifndef LLVM_CLANG_AST_ATTR_H 15#define LLVM_CLANG_AST_ATTR_H 16 17#include <cassert> 18#include <cstring> 19#include <string> 20#include <algorithm> 21 22namespace clang { 23 24/// Attr - This represents one attribute. 25class Attr { 26public: 27 enum Kind { 28 Alias, 29 Aligned, 30 AlwaysInline, 31 Annotate, 32 AsmLabel, // Represent GCC asm label extension. 33 Constructor, 34 Deprecated, 35 Destructor, 36 DLLImport, 37 DLLExport, 38 FastCall, 39 Format, 40 IBOutletKind, // Clang-specific. Use "Kind" suffix to not conflict with 41 NonNull, 42 NoReturn, 43 NoThrow, 44 ObjCGC, 45 ObjCNSObject, 46 ObjCException, 47 Overloadable, // Clang-specific 48 Packed, 49 Section, 50 StdCall, 51 TransparentUnion, 52 Unavailable, 53 Unused, 54 Used, 55 Visibility, 56 WarnUnusedResult, 57 Weak, 58 Blocks, 59 Const, 60 Pure, 61 Cleanup, 62 Nodebug 63 }; 64 65private: 66 Attr *Next; 67 Kind AttrKind; 68 bool Inherited : 1; 69 70protected: 71 Attr(Kind AK) : Next(0), AttrKind(AK), Inherited(false) {} 72public: 73 virtual ~Attr() { 74 delete Next; 75 } 76 77 /// \brief Whether this attribute should be merged to new 78 /// declarations. 79 virtual bool isMerged() const { return true; } 80 81 Kind getKind() const { return AttrKind; } 82 83 Attr *getNext() { return Next; } 84 const Attr *getNext() const { return Next; } 85 void setNext(Attr *next) { Next = next; } 86 87 bool isInherited() const { return Inherited; } 88 void setInherited(bool value) { Inherited = value; } 89 90 void addAttr(Attr *attr) { 91 assert((attr != 0) && "addAttr(): attr is null"); 92 93 // FIXME: This doesn't preserve the order in any way. 94 attr->Next = Next; 95 Next = attr; 96 } 97 98 // Implement isa/cast/dyncast/etc. 99 static bool classof(const Attr *) { return true; } 100}; 101 102class PackedAttr : public Attr { 103 unsigned Alignment; 104 105public: 106 PackedAttr(unsigned alignment) : Attr(Packed), Alignment(alignment) {} 107 108 /// getAlignment - The specified alignment in bits. 109 unsigned getAlignment() const { return Alignment; } 110 111 // Implement isa/cast/dyncast/etc. 112 static bool classof(const Attr *A) { 113 return A->getKind() == Packed; 114 } 115 static bool classof(const PackedAttr *A) { return true; } 116}; 117 118class AlignedAttr : public Attr { 119 unsigned Alignment; 120public: 121 AlignedAttr(unsigned alignment) : Attr(Aligned), Alignment(alignment) {} 122 123 /// getAlignment - The specified alignment in bits. 124 unsigned getAlignment() const { return Alignment; } 125 126 // Implement isa/cast/dyncast/etc. 127 static bool classof(const Attr *A) { 128 return A->getKind() == Aligned; 129 } 130 static bool classof(const AlignedAttr *A) { return true; } 131}; 132 133class AnnotateAttr : public Attr { 134 std::string Annotation; 135public: 136 AnnotateAttr(const std::string &ann) : Attr(Annotate), Annotation(ann) {} 137 138 const std::string& getAnnotation() const { return Annotation; } 139 140 // Implement isa/cast/dyncast/etc. 141 static bool classof(const Attr *A) { 142 return A->getKind() == Annotate; 143 } 144 static bool classof(const AnnotateAttr *A) { return true; } 145}; 146 147class AsmLabelAttr : public Attr { 148 std::string Label; 149public: 150 AsmLabelAttr(const std::string &L) : Attr(AsmLabel), Label(L) {} 151 152 const std::string& getLabel() const { return Label; } 153 154 // Implement isa/cast/dyncast/etc. 155 static bool classof(const Attr *A) { 156 return A->getKind() == AsmLabel; 157 } 158 static bool classof(const AsmLabelAttr *A) { return true; } 159}; 160 161class AlwaysInlineAttr : public Attr { 162public: 163 AlwaysInlineAttr() : Attr(AlwaysInline) {} 164 165 // Implement isa/cast/dyncast/etc. 166 167 static bool classof(const Attr *A) { return A->getKind() == AlwaysInline; } 168 static bool classof(const AlwaysInlineAttr *A) { return true; } 169}; 170 171class AliasAttr : public Attr { 172 std::string Aliasee; 173public: 174 AliasAttr(const std::string &aliasee) : Attr(Alias), Aliasee(aliasee) {} 175 176 const std::string& getAliasee() const { return Aliasee; } 177 178 // Implement isa/cast/dyncast/etc. 179 180 static bool classof(const Attr *A) { return A->getKind() == Alias; } 181 static bool classof(const AliasAttr *A) { return true; } 182}; 183 184class ConstructorAttr : public Attr { 185 int priority; 186public: 187 ConstructorAttr(int p) : Attr(Constructor), priority(p) {} 188 189 int getPriority() const { return priority; } 190 191 // Implement isa/cast/dyncast/etc. 192 static bool classof(const Attr *A) { return A->getKind() == Constructor; } 193 static bool classof(const ConstructorAttr *A) { return true; } 194}; 195 196class DestructorAttr : public Attr { 197 int priority; 198public: 199 DestructorAttr(int p) : Attr(Destructor), priority(p) {} 200 201 int getPriority() const { return priority; } 202 203 // Implement isa/cast/dyncast/etc. 204 static bool classof(const Attr *A) { return A->getKind() == Destructor; } 205 static bool classof(const DestructorAttr *A) { return true; } 206}; 207 208class IBOutletAttr : public Attr { 209public: 210 IBOutletAttr() : Attr(IBOutletKind) {} 211 212 // Implement isa/cast/dyncast/etc. 213 static bool classof(const Attr *A) { 214 return A->getKind() == IBOutletKind; 215 } 216 static bool classof(const IBOutletAttr *A) { return true; } 217}; 218 219class NoReturnAttr : public Attr { 220public: 221 NoReturnAttr() : Attr(NoReturn) {} 222 223 // Implement isa/cast/dyncast/etc. 224 static bool classof(const Attr *A) { return A->getKind() == NoReturn; } 225 static bool classof(const NoReturnAttr *A) { return true; } 226}; 227 228class DeprecatedAttr : public Attr { 229public: 230 DeprecatedAttr() : Attr(Deprecated) {} 231 232 // Implement isa/cast/dyncast/etc. 233 static bool classof(const Attr *A) { return A->getKind() == Deprecated; } 234 static bool classof(const DeprecatedAttr *A) { return true; } 235}; 236 237class SectionAttr : public Attr { 238 std::string Name; 239public: 240 SectionAttr(const std::string &N) : Attr(Section), Name(N) {} 241 242 const std::string& getName() const { return Name; } 243 244 // Implement isa/cast/dyncast/etc. 245 static bool classof(const Attr *A) { 246 return A->getKind() == Section; 247 } 248 static bool classof(const SectionAttr *A) { return true; } 249}; 250 251class UnavailableAttr : public Attr { 252public: 253 UnavailableAttr() : Attr(Unavailable) {} 254 255 // Implement isa/cast/dyncast/etc. 256 257 static bool classof(const Attr *A) { return A->getKind() == Unavailable; } 258 static bool classof(const UnavailableAttr *A) { return true; } 259}; 260 261class UnusedAttr : public Attr { 262public: 263 UnusedAttr() : Attr(Unused) {} 264 265 // Implement isa/cast/dyncast/etc. 266 static bool classof(const Attr *A) { return A->getKind() == Unused; } 267 static bool classof(const UnusedAttr *A) { return true; } 268}; 269 270class UsedAttr : public Attr { 271public: 272 UsedAttr() : Attr(Used) {} 273 274 // Implement isa/cast/dyncast/etc. 275 static bool classof(const Attr *A) { return A->getKind() == Used; } 276 static bool classof(const UsedAttr *A) { return true; } 277}; 278 279class WeakAttr : public Attr { 280public: 281 WeakAttr() : Attr(Weak) {} 282 283 // Implement isa/cast/dyncast/etc. 284 285 static bool classof(const Attr *A) { return A->getKind() == Weak; } 286 static bool classof(const WeakAttr *A) { return true; } 287}; 288 289class NoThrowAttr : public Attr { 290public: 291 NoThrowAttr() : Attr(NoThrow) {} 292 293 // Implement isa/cast/dyncast/etc. 294 static bool classof(const Attr *A) { return A->getKind() == NoThrow; } 295 static bool classof(const NoThrowAttr *A) { return true; } 296}; 297 298class ConstAttr : public Attr { 299public: 300 ConstAttr() : Attr(Const) {} 301 302 // Implement isa/cast/dyncast/etc. 303 static bool classof(const Attr *A) { return A->getKind() == Const; } 304 static bool classof(const ConstAttr *A) { return true; } 305}; 306 307class PureAttr : public Attr { 308public: 309 PureAttr() : Attr(Pure) {} 310 311 // Implement isa/cast/dyncast/etc. 312 static bool classof(const Attr *A) { return A->getKind() == Pure; } 313 static bool classof(const PureAttr *A) { return true; } 314}; 315 316class NonNullAttr : public Attr { 317 unsigned* ArgNums; 318 unsigned Size; 319public: 320 NonNullAttr(unsigned* arg_nums = 0, unsigned size = 0) : Attr(NonNull), 321 ArgNums(0), Size(0) { 322 323 if (size == 0) return; 324 assert(arg_nums); 325 ArgNums = new unsigned[size]; 326 Size = size; 327 memcpy(ArgNums, arg_nums, sizeof(*ArgNums)*size); 328 } 329 330 virtual ~NonNullAttr() { 331 delete [] ArgNums; 332 } 333 334 bool isNonNull(unsigned arg) const { 335 return ArgNums ? std::binary_search(ArgNums, ArgNums+Size, arg) : true; 336 } 337 338 static bool classof(const Attr *A) { return A->getKind() == NonNull; } 339 static bool classof(const NonNullAttr *A) { return true; } 340}; 341 342class FormatAttr : public Attr { 343 std::string Type; 344 int formatIdx, firstArg; 345public: 346 FormatAttr(const std::string &type, int idx, int first) : Attr(Format), 347 Type(type), formatIdx(idx), firstArg(first) {} 348 349 const std::string& getType() const { return Type; } 350 int getFormatIdx() const { return formatIdx; } 351 int getFirstArg() const { return firstArg; } 352 353 // Implement isa/cast/dyncast/etc. 354 355 static bool classof(const Attr *A) { return A->getKind() == Format; } 356 static bool classof(const FormatAttr *A) { return true; } 357}; 358 359class VisibilityAttr : public Attr { 360public: 361 /// @brief An enumeration for the kinds of visibility of symbols. 362 enum VisibilityTypes { 363 DefaultVisibility = 0, 364 HiddenVisibility, 365 ProtectedVisibility 366 }; 367private: 368 VisibilityTypes VisibilityType; 369public: 370 VisibilityAttr(VisibilityTypes v) : Attr(Visibility), 371 VisibilityType(v) {} 372 373 VisibilityTypes getVisibility() const { return VisibilityType; } 374 375 // Implement isa/cast/dyncast/etc. 376 377 static bool classof(const Attr *A) { return A->getKind() == Visibility; } 378 static bool classof(const VisibilityAttr *A) { return true; } 379}; 380 381class DLLImportAttr : public Attr { 382public: 383 DLLImportAttr() : Attr(DLLImport) {} 384 385 // Implement isa/cast/dyncast/etc. 386 387 static bool classof(const Attr *A) { return A->getKind() == DLLImport; } 388 static bool classof(const DLLImportAttr *A) { return true; } 389}; 390 391class DLLExportAttr : public Attr { 392public: 393 DLLExportAttr() : Attr(DLLExport) {} 394 395 // Implement isa/cast/dyncast/etc. 396 397 static bool classof(const Attr *A) { return A->getKind() == DLLExport; } 398 static bool classof(const DLLExportAttr *A) { return true; } 399}; 400 401class FastCallAttr : public Attr { 402public: 403 FastCallAttr() : Attr(FastCall) {} 404 405 // Implement isa/cast/dyncast/etc. 406 407 static bool classof(const Attr *A) { return A->getKind() == FastCall; } 408 static bool classof(const FastCallAttr *A) { return true; } 409}; 410 411class StdCallAttr : public Attr { 412public: 413 StdCallAttr() : Attr(StdCall) {} 414 415 // Implement isa/cast/dyncast/etc. 416 417 static bool classof(const Attr *A) { return A->getKind() == StdCall; } 418 static bool classof(const StdCallAttr *A) { return true; } 419}; 420 421class TransparentUnionAttr : public Attr { 422public: 423 TransparentUnionAttr() : Attr(TransparentUnion) {} 424 425 // Implement isa/cast/dyncast/etc. 426 427 static bool classof(const Attr *A) { return A->getKind() == TransparentUnion; } 428 static bool classof(const TransparentUnionAttr *A) { return true; } 429}; 430 431class ObjCGCAttr : public Attr { 432public: 433 enum GCAttrTypes { 434 Weak = 0, 435 Strong 436 }; 437private: 438 GCAttrTypes GCAttrType; 439public: 440 ObjCGCAttr(GCAttrTypes t) : Attr(ObjCGC), GCAttrType(t) {} 441 442 GCAttrTypes getType() const { return GCAttrType; } 443 444 // Implement isa/cast/dyncast/etc. 445 446 static bool classof(const Attr *A) { return A->getKind() == ObjCGC; } 447 static bool classof(const ObjCGCAttr *A) { return true; } 448}; 449 450class ObjCNSObjectAttr : public Attr { 451// Implement isa/cast/dyncast/etc. 452public: 453 ObjCNSObjectAttr() : Attr(ObjCNSObject) {} 454 455static bool classof(const Attr *A) { return A->getKind() == ObjCNSObject; } 456static bool classof(const ObjCNSObjectAttr *A) { return true; } 457}; 458 459 460class ObjCExceptionAttr : public Attr { 461public: 462 ObjCExceptionAttr() : Attr(ObjCException) {} 463 464 // Implement isa/cast/dyncast/etc. 465 static bool classof(const Attr *A) { return A->getKind() == ObjCException; } 466 static bool classof(const ObjCExceptionAttr *A) { return true; } 467}; 468 469 470class OverloadableAttr : public Attr { 471public: 472 OverloadableAttr() : Attr(Overloadable) { } 473 474 virtual bool isMerged() const { return false; } 475 476 static bool classof(const Attr *A) { return A->getKind() == Overloadable; } 477 static bool classof(const OverloadableAttr *) { return true; } 478}; 479 480class BlocksAttr : public Attr { 481public: 482 enum BlocksAttrTypes { 483 ByRef = 0 484 }; 485private: 486 BlocksAttrTypes BlocksAttrType; 487public: 488 BlocksAttr(BlocksAttrTypes t) : Attr(Blocks), BlocksAttrType(t) {} 489 490 BlocksAttrTypes getType() const { return BlocksAttrType; } 491 492 // Implement isa/cast/dyncast/etc. 493 494 static bool classof(const Attr *A) { return A->getKind() == Blocks; } 495 static bool classof(const BlocksAttr *A) { return true; } 496}; 497 498class FunctionDecl; 499 500class CleanupAttr : public Attr { 501 FunctionDecl *FD; 502 503public: 504 CleanupAttr(FunctionDecl *fd) : Attr(Cleanup), FD(fd) {} 505 506 const FunctionDecl *getFunctionDecl() const { return FD; } 507 508 // Implement isa/cast/dyncast/etc. 509 510 static bool classof(const Attr *A) { return A->getKind() == Cleanup; } 511 static bool classof(const CleanupAttr *A) { return true; } 512}; 513 514class NodebugAttr : public Attr { 515public: 516 NodebugAttr() : Attr(Nodebug) {} 517 518 // Implement isa/cast/dyncast/etc. 519 520 static bool classof(const Attr *A) { return A->getKind() == Nodebug; } 521 static bool classof(const DeprecatedAttr *A) { return true; } 522}; 523 524class WarnUnusedResultAttr : public Attr { 525public: 526 WarnUnusedResultAttr() : Attr(WarnUnusedResult) {} 527 528 // Implement isa/cast/dyncast/etc. 529 static bool classof(const Attr *A) { return A->getKind() == WarnUnusedResult;} 530 static bool classof(const WarnUnusedResultAttr *A) { return true; } 531}; 532 533} // end namespace clang 534 535#endif 536