Attr.h revision 3c385e5f8d9008fff18597ca302be19fa86e51f6
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 void setType(const std::string &type) { Type = type; } 351 int getFormatIdx() const { return formatIdx; } 352 int getFirstArg() const { return firstArg; } 353 354 // Implement isa/cast/dyncast/etc. 355 356 static bool classof(const Attr *A) { return A->getKind() == Format; } 357 static bool classof(const FormatAttr *A) { return true; } 358}; 359 360class VisibilityAttr : public Attr { 361public: 362 /// @brief An enumeration for the kinds of visibility of symbols. 363 enum VisibilityTypes { 364 DefaultVisibility = 0, 365 HiddenVisibility, 366 ProtectedVisibility 367 }; 368private: 369 VisibilityTypes VisibilityType; 370public: 371 VisibilityAttr(VisibilityTypes v) : Attr(Visibility), 372 VisibilityType(v) {} 373 374 VisibilityTypes getVisibility() const { return VisibilityType; } 375 376 // Implement isa/cast/dyncast/etc. 377 378 static bool classof(const Attr *A) { return A->getKind() == Visibility; } 379 static bool classof(const VisibilityAttr *A) { return true; } 380}; 381 382class DLLImportAttr : public Attr { 383public: 384 DLLImportAttr() : Attr(DLLImport) {} 385 386 // Implement isa/cast/dyncast/etc. 387 388 static bool classof(const Attr *A) { return A->getKind() == DLLImport; } 389 static bool classof(const DLLImportAttr *A) { return true; } 390}; 391 392class DLLExportAttr : public Attr { 393public: 394 DLLExportAttr() : Attr(DLLExport) {} 395 396 // Implement isa/cast/dyncast/etc. 397 398 static bool classof(const Attr *A) { return A->getKind() == DLLExport; } 399 static bool classof(const DLLExportAttr *A) { return true; } 400}; 401 402class FastCallAttr : public Attr { 403public: 404 FastCallAttr() : Attr(FastCall) {} 405 406 // Implement isa/cast/dyncast/etc. 407 408 static bool classof(const Attr *A) { return A->getKind() == FastCall; } 409 static bool classof(const FastCallAttr *A) { return true; } 410}; 411 412class StdCallAttr : public Attr { 413public: 414 StdCallAttr() : Attr(StdCall) {} 415 416 // Implement isa/cast/dyncast/etc. 417 418 static bool classof(const Attr *A) { return A->getKind() == StdCall; } 419 static bool classof(const StdCallAttr *A) { return true; } 420}; 421 422class TransparentUnionAttr : public Attr { 423public: 424 TransparentUnionAttr() : Attr(TransparentUnion) {} 425 426 // Implement isa/cast/dyncast/etc. 427 428 static bool classof(const Attr *A) { return A->getKind() == TransparentUnion; } 429 static bool classof(const TransparentUnionAttr *A) { return true; } 430}; 431 432class ObjCGCAttr : public Attr { 433public: 434 enum GCAttrTypes { 435 Weak = 0, 436 Strong 437 }; 438private: 439 GCAttrTypes GCAttrType; 440public: 441 ObjCGCAttr(GCAttrTypes t) : Attr(ObjCGC), GCAttrType(t) {} 442 443 GCAttrTypes getType() const { return GCAttrType; } 444 445 // Implement isa/cast/dyncast/etc. 446 447 static bool classof(const Attr *A) { return A->getKind() == ObjCGC; } 448 static bool classof(const ObjCGCAttr *A) { return true; } 449}; 450 451class ObjCNSObjectAttr : public Attr { 452// Implement isa/cast/dyncast/etc. 453public: 454 ObjCNSObjectAttr() : Attr(ObjCNSObject) {} 455 456static bool classof(const Attr *A) { return A->getKind() == ObjCNSObject; } 457static bool classof(const ObjCNSObjectAttr *A) { return true; } 458}; 459 460 461class ObjCExceptionAttr : public Attr { 462public: 463 ObjCExceptionAttr() : Attr(ObjCException) {} 464 465 // Implement isa/cast/dyncast/etc. 466 static bool classof(const Attr *A) { return A->getKind() == ObjCException; } 467 static bool classof(const ObjCExceptionAttr *A) { return true; } 468}; 469 470 471class OverloadableAttr : public Attr { 472public: 473 OverloadableAttr() : Attr(Overloadable) { } 474 475 virtual bool isMerged() const { return false; } 476 477 static bool classof(const Attr *A) { return A->getKind() == Overloadable; } 478 static bool classof(const OverloadableAttr *) { return true; } 479}; 480 481class BlocksAttr : public Attr { 482public: 483 enum BlocksAttrTypes { 484 ByRef = 0 485 }; 486private: 487 BlocksAttrTypes BlocksAttrType; 488public: 489 BlocksAttr(BlocksAttrTypes t) : Attr(Blocks), BlocksAttrType(t) {} 490 491 BlocksAttrTypes getType() const { return BlocksAttrType; } 492 493 // Implement isa/cast/dyncast/etc. 494 495 static bool classof(const Attr *A) { return A->getKind() == Blocks; } 496 static bool classof(const BlocksAttr *A) { return true; } 497}; 498 499class FunctionDecl; 500 501class CleanupAttr : public Attr { 502 FunctionDecl *FD; 503 504public: 505 CleanupAttr(FunctionDecl *fd) : Attr(Cleanup), FD(fd) {} 506 507 const FunctionDecl *getFunctionDecl() const { return FD; } 508 509 // Implement isa/cast/dyncast/etc. 510 511 static bool classof(const Attr *A) { return A->getKind() == Cleanup; } 512 static bool classof(const CleanupAttr *A) { return true; } 513}; 514 515class NodebugAttr : public Attr { 516public: 517 NodebugAttr() : Attr(Nodebug) {} 518 519 // Implement isa/cast/dyncast/etc. 520 521 static bool classof(const Attr *A) { return A->getKind() == Nodebug; } 522 static bool classof(const DeprecatedAttr *A) { return true; } 523}; 524 525class WarnUnusedResultAttr : public Attr { 526public: 527 WarnUnusedResultAttr() : Attr(WarnUnusedResult) {} 528 529 // Implement isa/cast/dyncast/etc. 530 static bool classof(const Attr *A) { return A->getKind() == WarnUnusedResult;} 531 static bool classof(const WarnUnusedResultAttr *A) { return true; } 532}; 533 534} // end namespace clang 535 536#endif 537