/src/llvm-project/clang/lib/AST/AttrImpl.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | //===--- AttrImpl.cpp - Classes for representing 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 contains out-of-line methods for Attr classes. |
10 | | // |
11 | | //===----------------------------------------------------------------------===// |
12 | | |
13 | | #include "clang/AST/ASTContext.h" |
14 | | #include "clang/AST/Attr.h" |
15 | | #include "clang/AST/Expr.h" |
16 | | #include "clang/AST/Type.h" |
17 | | #include <optional> |
18 | | using namespace clang; |
19 | | |
20 | | void LoopHintAttr::printPrettyPragma(raw_ostream &OS, |
21 | 0 | const PrintingPolicy &Policy) const { |
22 | 0 | unsigned SpellingIndex = getAttributeSpellingListIndex(); |
23 | | // For "#pragma unroll" and "#pragma nounroll" the string "unroll" or |
24 | | // "nounroll" is already emitted as the pragma name. |
25 | 0 | if (SpellingIndex == Pragma_nounroll || |
26 | 0 | SpellingIndex == Pragma_nounroll_and_jam) |
27 | 0 | return; |
28 | 0 | else if (SpellingIndex == Pragma_unroll || |
29 | 0 | SpellingIndex == Pragma_unroll_and_jam) { |
30 | 0 | OS << ' ' << getValueString(Policy); |
31 | 0 | return; |
32 | 0 | } |
33 | | |
34 | 0 | assert(SpellingIndex == Pragma_clang_loop && "Unexpected spelling"); |
35 | 0 | OS << ' ' << getOptionName(option) << getValueString(Policy); |
36 | 0 | } |
37 | | |
38 | | // Return a string containing the loop hint argument including the |
39 | | // enclosing parentheses. |
40 | 0 | std::string LoopHintAttr::getValueString(const PrintingPolicy &Policy) const { |
41 | 0 | std::string ValueName; |
42 | 0 | llvm::raw_string_ostream OS(ValueName); |
43 | 0 | OS << "("; |
44 | 0 | if (state == Numeric) |
45 | 0 | value->printPretty(OS, nullptr, Policy); |
46 | 0 | else if (state == FixedWidth || state == ScalableWidth) { |
47 | 0 | if (value) { |
48 | 0 | value->printPretty(OS, nullptr, Policy); |
49 | 0 | if (state == ScalableWidth) |
50 | 0 | OS << ", scalable"; |
51 | 0 | } else if (state == ScalableWidth) |
52 | 0 | OS << "scalable"; |
53 | 0 | else |
54 | 0 | OS << "fixed"; |
55 | 0 | } else if (state == Enable) |
56 | 0 | OS << "enable"; |
57 | 0 | else if (state == Full) |
58 | 0 | OS << "full"; |
59 | 0 | else if (state == AssumeSafety) |
60 | 0 | OS << "assume_safety"; |
61 | 0 | else |
62 | 0 | OS << "disable"; |
63 | 0 | OS << ")"; |
64 | 0 | return ValueName; |
65 | 0 | } |
66 | | |
67 | | // Return a string suitable for identifying this attribute in diagnostics. |
68 | | std::string |
69 | 0 | LoopHintAttr::getDiagnosticName(const PrintingPolicy &Policy) const { |
70 | 0 | unsigned SpellingIndex = getAttributeSpellingListIndex(); |
71 | 0 | if (SpellingIndex == Pragma_nounroll) |
72 | 0 | return "#pragma nounroll"; |
73 | 0 | else if (SpellingIndex == Pragma_unroll) |
74 | 0 | return "#pragma unroll" + |
75 | 0 | (option == UnrollCount ? getValueString(Policy) : ""); |
76 | 0 | else if (SpellingIndex == Pragma_nounroll_and_jam) |
77 | 0 | return "#pragma nounroll_and_jam"; |
78 | 0 | else if (SpellingIndex == Pragma_unroll_and_jam) |
79 | 0 | return "#pragma unroll_and_jam" + |
80 | 0 | (option == UnrollAndJamCount ? getValueString(Policy) : ""); |
81 | | |
82 | 0 | assert(SpellingIndex == Pragma_clang_loop && "Unexpected spelling"); |
83 | 0 | return getOptionName(option) + getValueString(Policy); |
84 | 0 | } |
85 | | |
86 | | void OMPDeclareSimdDeclAttr::printPrettyPragma( |
87 | 0 | raw_ostream &OS, const PrintingPolicy &Policy) const { |
88 | 0 | if (getBranchState() != BS_Undefined) |
89 | 0 | OS << ' ' << ConvertBranchStateTyToStr(getBranchState()); |
90 | 0 | if (auto *E = getSimdlen()) { |
91 | 0 | OS << " simdlen("; |
92 | 0 | E->printPretty(OS, nullptr, Policy); |
93 | 0 | OS << ")"; |
94 | 0 | } |
95 | 0 | if (uniforms_size() > 0) { |
96 | 0 | OS << " uniform"; |
97 | 0 | StringRef Sep = "("; |
98 | 0 | for (auto *E : uniforms()) { |
99 | 0 | OS << Sep; |
100 | 0 | E->printPretty(OS, nullptr, Policy); |
101 | 0 | Sep = ", "; |
102 | 0 | } |
103 | 0 | OS << ")"; |
104 | 0 | } |
105 | 0 | alignments_iterator NI = alignments_begin(); |
106 | 0 | for (auto *E : aligneds()) { |
107 | 0 | OS << " aligned("; |
108 | 0 | E->printPretty(OS, nullptr, Policy); |
109 | 0 | if (*NI) { |
110 | 0 | OS << ": "; |
111 | 0 | (*NI)->printPretty(OS, nullptr, Policy); |
112 | 0 | } |
113 | 0 | OS << ")"; |
114 | 0 | ++NI; |
115 | 0 | } |
116 | 0 | steps_iterator I = steps_begin(); |
117 | 0 | modifiers_iterator MI = modifiers_begin(); |
118 | 0 | for (auto *E : linears()) { |
119 | 0 | OS << " linear("; |
120 | 0 | if (*MI != OMPC_LINEAR_unknown) |
121 | 0 | OS << getOpenMPSimpleClauseTypeName(llvm::omp::Clause::OMPC_linear, *MI) |
122 | 0 | << "("; |
123 | 0 | E->printPretty(OS, nullptr, Policy); |
124 | 0 | if (*MI != OMPC_LINEAR_unknown) |
125 | 0 | OS << ")"; |
126 | 0 | if (*I) { |
127 | 0 | OS << ": "; |
128 | 0 | (*I)->printPretty(OS, nullptr, Policy); |
129 | 0 | } |
130 | 0 | OS << ")"; |
131 | 0 | ++I; |
132 | 0 | ++MI; |
133 | 0 | } |
134 | 0 | } |
135 | | |
136 | | void OMPDeclareTargetDeclAttr::printPrettyPragma( |
137 | 0 | raw_ostream &OS, const PrintingPolicy &Policy) const { |
138 | | // Use fake syntax because it is for testing and debugging purpose only. |
139 | 0 | if (getDevType() != DT_Any) |
140 | 0 | OS << " device_type(" << ConvertDevTypeTyToStr(getDevType()) << ")"; |
141 | 0 | if (getMapType() != MT_To && getMapType() != MT_Enter) |
142 | 0 | OS << ' ' << ConvertMapTypeTyToStr(getMapType()); |
143 | 0 | if (Expr *E = getIndirectExpr()) { |
144 | 0 | OS << " indirect("; |
145 | 0 | E->printPretty(OS, nullptr, Policy); |
146 | 0 | OS << ")"; |
147 | 0 | } else if (getIndirect()) { |
148 | 0 | OS << " indirect"; |
149 | 0 | } |
150 | 0 | } |
151 | | |
152 | | std::optional<OMPDeclareTargetDeclAttr *> |
153 | 0 | OMPDeclareTargetDeclAttr::getActiveAttr(const ValueDecl *VD) { |
154 | 0 | if (llvm::all_of(VD->redecls(), [](const Decl *D) { return !D->hasAttrs(); })) |
155 | 0 | return std::nullopt; |
156 | 0 | unsigned Level = 0; |
157 | 0 | OMPDeclareTargetDeclAttr *FoundAttr = nullptr; |
158 | 0 | for (const Decl *D : VD->redecls()) { |
159 | 0 | for (auto *Attr : D->specific_attrs<OMPDeclareTargetDeclAttr>()) { |
160 | 0 | if (Level <= Attr->getLevel()) { |
161 | 0 | Level = Attr->getLevel(); |
162 | 0 | FoundAttr = Attr; |
163 | 0 | } |
164 | 0 | } |
165 | 0 | } |
166 | 0 | if (FoundAttr) |
167 | 0 | return FoundAttr; |
168 | 0 | return std::nullopt; |
169 | 0 | } |
170 | | |
171 | | std::optional<OMPDeclareTargetDeclAttr::MapTypeTy> |
172 | 0 | OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(const ValueDecl *VD) { |
173 | 0 | std::optional<OMPDeclareTargetDeclAttr *> ActiveAttr = getActiveAttr(VD); |
174 | 0 | if (ActiveAttr) |
175 | 0 | return (*ActiveAttr)->getMapType(); |
176 | 0 | return std::nullopt; |
177 | 0 | } |
178 | | |
179 | | std::optional<OMPDeclareTargetDeclAttr::DevTypeTy> |
180 | 0 | OMPDeclareTargetDeclAttr::getDeviceType(const ValueDecl *VD) { |
181 | 0 | std::optional<OMPDeclareTargetDeclAttr *> ActiveAttr = getActiveAttr(VD); |
182 | 0 | if (ActiveAttr) |
183 | 0 | return (*ActiveAttr)->getDevType(); |
184 | 0 | return std::nullopt; |
185 | 0 | } |
186 | | |
187 | | std::optional<SourceLocation> |
188 | 0 | OMPDeclareTargetDeclAttr::getLocation(const ValueDecl *VD) { |
189 | 0 | std::optional<OMPDeclareTargetDeclAttr *> ActiveAttr = getActiveAttr(VD); |
190 | 0 | if (ActiveAttr) |
191 | 0 | return (*ActiveAttr)->getRange().getBegin(); |
192 | 0 | return std::nullopt; |
193 | 0 | } |
194 | | |
195 | | namespace clang { |
196 | | llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const OMPTraitInfo &TI); |
197 | | llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const OMPTraitInfo *TI); |
198 | | } |
199 | | |
200 | | void OMPDeclareVariantAttr::printPrettyPragma( |
201 | 0 | raw_ostream &OS, const PrintingPolicy &Policy) const { |
202 | 0 | if (const Expr *E = getVariantFuncRef()) { |
203 | 0 | OS << "("; |
204 | 0 | E->printPretty(OS, nullptr, Policy); |
205 | 0 | OS << ")"; |
206 | 0 | } |
207 | 0 | OS << " match(" << traitInfos << ")"; |
208 | |
|
209 | 0 | auto PrintExprs = [&OS, &Policy](Expr **Begin, Expr **End) { |
210 | 0 | for (Expr **I = Begin; I != End; ++I) { |
211 | 0 | assert(*I && "Expected non-null Stmt"); |
212 | 0 | if (I != Begin) |
213 | 0 | OS << ","; |
214 | 0 | (*I)->printPretty(OS, nullptr, Policy); |
215 | 0 | } |
216 | 0 | }; |
217 | 0 | if (adjustArgsNothing_size()) { |
218 | 0 | OS << " adjust_args(nothing:"; |
219 | 0 | PrintExprs(adjustArgsNothing_begin(), adjustArgsNothing_end()); |
220 | 0 | OS << ")"; |
221 | 0 | } |
222 | 0 | if (adjustArgsNeedDevicePtr_size()) { |
223 | 0 | OS << " adjust_args(need_device_ptr:"; |
224 | 0 | PrintExprs(adjustArgsNeedDevicePtr_begin(), adjustArgsNeedDevicePtr_end()); |
225 | 0 | OS << ")"; |
226 | 0 | } |
227 | |
|
228 | 0 | auto PrintInteropInfo = [&OS](OMPInteropInfo *Begin, OMPInteropInfo *End) { |
229 | 0 | for (OMPInteropInfo *I = Begin; I != End; ++I) { |
230 | 0 | if (I != Begin) |
231 | 0 | OS << ", "; |
232 | 0 | OS << "interop("; |
233 | 0 | OS << getInteropTypeString(I); |
234 | 0 | OS << ")"; |
235 | 0 | } |
236 | 0 | }; |
237 | 0 | if (appendArgs_size()) { |
238 | 0 | OS << " append_args("; |
239 | 0 | PrintInteropInfo(appendArgs_begin(), appendArgs_end()); |
240 | 0 | OS << ")"; |
241 | 0 | } |
242 | 0 | } |
243 | | |
244 | 0 | unsigned AlignedAttr::getAlignment(ASTContext &Ctx) const { |
245 | 0 | assert(!isAlignmentDependent()); |
246 | 0 | if (getCachedAlignmentValue()) |
247 | 0 | return *getCachedAlignmentValue(); |
248 | | |
249 | | // Handle alignmentType case. |
250 | 0 | if (!isAlignmentExpr()) { |
251 | 0 | QualType T = getAlignmentType()->getType(); |
252 | | |
253 | | // C++ [expr.alignof]p3: |
254 | | // When alignof is applied to a reference type, the result is the |
255 | | // alignment of the referenced type. |
256 | 0 | T = T.getNonReferenceType(); |
257 | |
|
258 | 0 | if (T.getQualifiers().hasUnaligned()) |
259 | 0 | return Ctx.getCharWidth(); |
260 | | |
261 | 0 | return Ctx.getTypeAlignInChars(T.getTypePtr()).getQuantity() * |
262 | 0 | Ctx.getCharWidth(); |
263 | 0 | } |
264 | | |
265 | | // Handle alignmentExpr case. |
266 | 0 | if (alignmentExpr) |
267 | 0 | return alignmentExpr->EvaluateKnownConstInt(Ctx).getZExtValue() * |
268 | 0 | Ctx.getCharWidth(); |
269 | | |
270 | 0 | return Ctx.getTargetDefaultAlignForAttributeAligned(); |
271 | 0 | } |
272 | | |
273 | | #include "clang/AST/AttrImpl.inc" |