clang  14.0.0git
AttributeCommonInfo.h
Go to the documentation of this file.
1 //======- AttributeCommonInfo.h - Base info about Attributes-----*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines the AttributeCommonInfo type, which is the base for a
10 // ParsedAttr and is used by Attr as a way to share info between the two.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_CLANG_BASIC_ATTRIBUTECOMMONINFO_H
15 #define LLVM_CLANG_BASIC_ATTRIBUTECOMMONINFO_H
17 
18 namespace clang {
19 class IdentifierInfo;
20 class ASTRecordWriter;
21 
23 public:
24  /// The style used to specify an attribute.
25  enum Syntax {
26  /// __attribute__((...))
28 
29  /// [[...]]
31 
32  /// [[...]]
34 
35  /// __declspec(...)
37 
38  /// [uuid("...")] class Foo
40 
41  /// __ptr16, alignas(...), etc.
43 
44  /// #pragma ...
46 
47  // Note TableGen depends on the order above. Do not add or change the order
48  // without adding related code to TableGen/ClangAttrEmitter.cpp.
49  /// Context-sensitive version of a keyword attribute.
51  };
52  enum Kind {
53 #define PARSED_ATTR(NAME) AT_##NAME,
54 #include "clang/Sema/AttrParsedAttrList.inc"
55 #undef PARSED_ATTR
59  };
60 
61 private:
62  const IdentifierInfo *AttrName = nullptr;
63  const IdentifierInfo *ScopeName = nullptr;
64  SourceRange AttrRange;
65  const SourceLocation ScopeLoc;
66  // Corresponds to the Kind enum.
67  unsigned AttrKind : 16;
68  /// Corresponds to the Syntax enum.
69  unsigned SyntaxUsed : 3;
70  unsigned SpellingIndex : 4;
71 
72 protected:
73  static constexpr unsigned SpellingNotCalculated = 0xf;
74 
75 public:
77  : AttrRange(AttrRange), ScopeLoc(), AttrKind(0), SyntaxUsed(0),
78  SpellingIndex(SpellingNotCalculated) {}
79 
81  : AttrRange(AttrLoc), ScopeLoc(), AttrKind(0), SyntaxUsed(0),
82  SpellingIndex(SpellingNotCalculated) {}
83 
85  const IdentifierInfo *ScopeName, SourceRange AttrRange,
86  SourceLocation ScopeLoc, Syntax SyntaxUsed)
87  : AttrName(AttrName), ScopeName(ScopeName), AttrRange(AttrRange),
88  ScopeLoc(ScopeLoc),
89  AttrKind(getParsedKind(AttrName, ScopeName, SyntaxUsed)),
90  SyntaxUsed(SyntaxUsed), SpellingIndex(SpellingNotCalculated) {}
91 
93  const IdentifierInfo *ScopeName, SourceRange AttrRange,
94  SourceLocation ScopeLoc, Kind AttrKind, Syntax SyntaxUsed)
95  : AttrName(AttrName), ScopeName(ScopeName), AttrRange(AttrRange),
96  ScopeLoc(ScopeLoc), AttrKind(AttrKind), SyntaxUsed(SyntaxUsed),
97  SpellingIndex(SpellingNotCalculated) {}
98 
100  const IdentifierInfo *ScopeName, SourceRange AttrRange,
101  SourceLocation ScopeLoc, Kind AttrKind, Syntax SyntaxUsed,
102  unsigned Spelling)
103  : AttrName(AttrName), ScopeName(ScopeName), AttrRange(AttrRange),
104  ScopeLoc(ScopeLoc), AttrKind(AttrKind), SyntaxUsed(SyntaxUsed),
105  SpellingIndex(Spelling) {}
106 
107  AttributeCommonInfo(const IdentifierInfo *AttrName, SourceRange AttrRange,
108  Syntax SyntaxUsed)
109  : AttrName(AttrName), ScopeName(nullptr), AttrRange(AttrRange),
110  ScopeLoc(), AttrKind(getParsedKind(AttrName, ScopeName, SyntaxUsed)),
111  SyntaxUsed(SyntaxUsed), SpellingIndex(SpellingNotCalculated) {}
112 
113  AttributeCommonInfo(SourceRange AttrRange, Kind K, Syntax SyntaxUsed)
114  : AttrName(nullptr), ScopeName(nullptr), AttrRange(AttrRange), ScopeLoc(),
115  AttrKind(K), SyntaxUsed(SyntaxUsed),
116  SpellingIndex(SpellingNotCalculated) {}
117 
118  AttributeCommonInfo(SourceRange AttrRange, Kind K, Syntax SyntaxUsed,
119  unsigned Spelling)
120  : AttrName(nullptr), ScopeName(nullptr), AttrRange(AttrRange), ScopeLoc(),
121  AttrKind(K), SyntaxUsed(SyntaxUsed), SpellingIndex(Spelling) {}
122 
124  AttributeCommonInfo(const AttributeCommonInfo &) = default;
125 
126  Kind getParsedKind() const { return Kind(AttrKind); }
127  Syntax getSyntax() const { return Syntax(SyntaxUsed); }
128  const IdentifierInfo *getAttrName() const { return AttrName; }
129  SourceLocation getLoc() const { return AttrRange.getBegin(); }
130  SourceRange getRange() const { return AttrRange; }
131  void setRange(SourceRange R) { AttrRange = R; }
132 
133  bool hasScope() const { return ScopeName; }
134  const IdentifierInfo *getScopeName() const { return ScopeName; }
135  SourceLocation getScopeLoc() const { return ScopeLoc; }
136 
137  /// Gets the normalized full name, which consists of both scope and name and
138  /// with surrounding underscores removed as appropriate (e.g.
139  /// __gnu__::__attr__ will be normalized to gnu::attr).
141 
142  bool isDeclspecAttribute() const { return SyntaxUsed == AS_Declspec; }
143  bool isMicrosoftAttribute() const { return SyntaxUsed == AS_Microsoft; }
144 
145  bool isGNUScope() const;
146 
147  bool isAlignasAttribute() const {
148  // FIXME: Use a better mechanism to determine this.
149  return getParsedKind() == AT_Aligned && isKeywordAttribute();
150  }
151 
152  bool isCXX11Attribute() const {
153  return SyntaxUsed == AS_CXX11 || isAlignasAttribute();
154  }
155 
156  bool isC2xAttribute() const { return SyntaxUsed == AS_C2x; }
157 
158  /// The attribute is spelled [[]] in either C or C++ mode, including standard
159  /// attributes spelled with a keyword, like alignas.
161  return isCXX11Attribute() || isC2xAttribute();
162  }
163 
164  bool isKeywordAttribute() const {
165  return SyntaxUsed == AS_Keyword || SyntaxUsed == AS_ContextSensitiveKeyword;
166  }
167 
169  return SyntaxUsed == AS_ContextSensitiveKeyword;
170  }
171 
172  unsigned getAttributeSpellingListIndex() const {
173  assert((isAttributeSpellingListCalculated() || AttrName) &&
174  "Spelling cannot be found");
176  ? SpellingIndex
177  : calculateAttributeSpellingListIndex();
178  }
179  void setAttributeSpellingListIndex(unsigned V) { SpellingIndex = V; }
180 
181  static Kind getParsedKind(const IdentifierInfo *Name,
182  const IdentifierInfo *Scope, Syntax SyntaxUsed);
183 
184 private:
185  /// Get an index into the attribute spelling list
186  /// defined in Attr.td. This index is used by an attribute
187  /// to pretty print itself.
188  unsigned calculateAttributeSpellingListIndex() const;
189 
191  // Used exclusively by ASTDeclWriter to get the raw spelling list state.
192  unsigned getAttributeSpellingListIndexRaw() const { return SpellingIndex; }
193 
194 protected:
196  return SpellingIndex != SpellingNotCalculated;
197  }
198 };
199 } // namespace clang
200 
201 #endif // LLVM_CLANG_BASIC_ATTRIBUTECOMMONINFO_H
clang::AttributeCommonInfo::getParsedKind
Kind getParsedKind() const
Definition: AttributeCommonInfo.h:126
clang::AttributeCommonInfo::AS_Microsoft
@ AS_Microsoft
[uuid("...")] class Foo
Definition: AttributeCommonInfo.h:39
clang::AttributeCommonInfo::getLoc
SourceLocation getLoc() const
Definition: AttributeCommonInfo.h:129
clang::AttributeCommonInfo::Kind
Kind
Definition: AttributeCommonInfo.h:52
clang::SourceRange
A trivial tuple used to represent a source range.
Definition: SourceLocation.h:212
string
string(SUBSTRING ${CMAKE_CURRENT_BINARY_DIR} 0 ${PATH_LIB_START} PATH_HEAD) string(SUBSTRING $
Definition: CMakeLists.txt:22
clang::AttributeCommonInfo::setRange
void setRange(SourceRange R)
Definition: AttributeCommonInfo.h:131
clang::SourceLocation
Encodes a location in the source.
Definition: SourceLocation.h:88
clang::SourceRange::getBegin
SourceLocation getBegin() const
Definition: SourceLocation.h:221
clang::AttributeCommonInfo::isKeywordAttribute
bool isKeywordAttribute() const
Definition: AttributeCommonInfo.h:164
clang::AttributeCommonInfo::hasScope
bool hasScope() const
Definition: AttributeCommonInfo.h:133
clang::AttributeCommonInfo::AS_CXX11
@ AS_CXX11
[[...]]
Definition: AttributeCommonInfo.h:30
clang::AttributeCommonInfo::AttributeCommonInfo
AttributeCommonInfo(SourceLocation AttrLoc)
Definition: AttributeCommonInfo.h:80
clang::AttributeCommonInfo::getAttrName
const IdentifierInfo * getAttrName() const
Definition: AttributeCommonInfo.h:128
clang::AttributeCommonInfo::AttributeCommonInfo
AttributeCommonInfo(SourceRange AttrRange, Kind K, Syntax SyntaxUsed)
Definition: AttributeCommonInfo.h:113
clang::AttributeCommonInfo::Syntax
Syntax
The style used to specify an attribute.
Definition: AttributeCommonInfo.h:25
clang::AttributeCommonInfo::AS_GNU
@ AS_GNU
attribute((...))
Definition: AttributeCommonInfo.h:27
clang::AttributeCommonInfo::isDeclspecAttribute
bool isDeclspecAttribute() const
Definition: AttributeCommonInfo.h:142
clang::AttributeCommonInfo::AttributeCommonInfo
AttributeCommonInfo(SourceRange AttrRange, Kind K, Syntax SyntaxUsed, unsigned Spelling)
Definition: AttributeCommonInfo.h:118
clang::AttributeCommonInfo::AS_Keyword
@ AS_Keyword
__ptr16, alignas(...), etc.
Definition: AttributeCommonInfo.h:42
clang::AttributeCommonInfo::AttributeCommonInfo
AttributeCommonInfo(const IdentifierInfo *AttrName, const IdentifierInfo *ScopeName, SourceRange AttrRange, SourceLocation ScopeLoc, Kind AttrKind, Syntax SyntaxUsed)
Definition: AttributeCommonInfo.h:92
clang::AttributeCommonInfo::isC2xAttribute
bool isC2xAttribute() const
Definition: AttributeCommonInfo.h:156
V
#define V(N, I)
Definition: ASTContext.h:3121
clang::AttributeCommonInfo::UnknownAttribute
@ UnknownAttribute
Definition: AttributeCommonInfo.h:58
clang::AttributeCommonInfo::AttributeCommonInfo
AttributeCommonInfo(const IdentifierInfo *AttrName, const IdentifierInfo *ScopeName, SourceRange AttrRange, SourceLocation ScopeLoc, Syntax SyntaxUsed)
Definition: AttributeCommonInfo.h:84
clang::AttributeCommonInfo::AS_ContextSensitiveKeyword
@ AS_ContextSensitiveKeyword
Context-sensitive version of a keyword attribute.
Definition: AttributeCommonInfo.h:50
clang::AttributeCommonInfo::isStandardAttributeSyntax
bool isStandardAttributeSyntax() const
The attribute is spelled [[]] in either C or C++ mode, including standard attributes spelled with a k...
Definition: AttributeCommonInfo.h:160
clang::Scope
Scope - A scope is a transient data structure that is used while parsing the program.
Definition: Scope.h:40
clang::AttributeCommonInfo::getSyntax
Syntax getSyntax() const
Definition: AttributeCommonInfo.h:127
clang::AttributeCommonInfo::isContextSensitiveKeywordAttribute
bool isContextSensitiveKeywordAttribute() const
Definition: AttributeCommonInfo.h:168
clang::AttributeCommonInfo::AttributeCommonInfo
AttributeCommonInfo(const IdentifierInfo *AttrName, SourceRange AttrRange, Syntax SyntaxUsed)
Definition: AttributeCommonInfo.h:107
clang::AttributeCommonInfo::AttributeCommonInfo
AttributeCommonInfo(const IdentifierInfo *AttrName, const IdentifierInfo *ScopeName, SourceRange AttrRange, SourceLocation ScopeLoc, Kind AttrKind, Syntax SyntaxUsed, unsigned Spelling)
Definition: AttributeCommonInfo.h:99
clang::AttributeCommonInfo::isAttributeSpellingListCalculated
bool isAttributeSpellingListCalculated() const
Definition: AttributeCommonInfo.h:195
clang::AttributeCommonInfo::getScopeLoc
SourceLocation getScopeLoc() const
Definition: AttributeCommonInfo.h:135
clang::AttributeCommonInfo::AS_C2x
@ AS_C2x
[[...]]
Definition: AttributeCommonInfo.h:33
clang::AttributeCommonInfo::isMicrosoftAttribute
bool isMicrosoftAttribute() const
Definition: AttributeCommonInfo.h:143
SourceLocation.h
clang::AttributeCommonInfo::isCXX11Attribute
bool isCXX11Attribute() const
Definition: AttributeCommonInfo.h:152
clang::AttributeCommonInfo::getRange
SourceRange getRange() const
Definition: AttributeCommonInfo.h:130
clang::AttributeCommonInfo::getScopeName
const IdentifierInfo * getScopeName() const
Definition: AttributeCommonInfo.h:134
clang::AttributeCommonInfo::isAlignasAttribute
bool isAlignasAttribute() const
Definition: AttributeCommonInfo.h:147
clang::AttributeCommonInfo
Definition: AttributeCommonInfo.h:22
clang::ASTRecordWriter
An object for streaming information to a record.
Definition: ASTRecordWriter.h:26
clang::AttributeCommonInfo::getNormalizedFullName
std::string getNormalizedFullName() const
Gets the normalized full name, which consists of both scope and name and with surrounding underscores...
Definition: Attributes.cpp:114
clang::AttributeCommonInfo::NoSemaHandlerAttribute
@ NoSemaHandlerAttribute
Definition: AttributeCommonInfo.h:56
clang::AttributeCommonInfo::AS_Declspec
@ AS_Declspec
__declspec(...)
Definition: AttributeCommonInfo.h:36
clang::AttributeCommonInfo::IgnoredAttribute
@ IgnoredAttribute
Definition: AttributeCommonInfo.h:57
clang::IdentifierInfo
One of these records is kept for each identifier that is lexed.
Definition: IdentifierTable.h:84
clang::AttributeCommonInfo::AttributeCommonInfo
AttributeCommonInfo(SourceRange AttrRange)
Definition: AttributeCommonInfo.h:76
clang
Definition: CalledOnceCheck.h:17
clang::AttributeCommonInfo::setAttributeSpellingListIndex
void setAttributeSpellingListIndex(unsigned V)
Definition: AttributeCommonInfo.h:179
clang::AttributeCommonInfo::SpellingNotCalculated
static constexpr unsigned SpellingNotCalculated
Definition: AttributeCommonInfo.h:73
clang::AttributeCommonInfo::AS_Pragma
@ AS_Pragma
#pragma ...
Definition: AttributeCommonInfo.h:45
clang::AttributeCommonInfo::getAttributeSpellingListIndex
unsigned getAttributeSpellingListIndex() const
Definition: AttributeCommonInfo.h:172
clang::AttributeCommonInfo::isGNUScope
bool isGNUScope() const
Definition: Attributes.cpp:84