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 using namespace clang; 18 19 void LoopHintAttr::printPrettyPragma(raw_ostream &OS, 20 const PrintingPolicy &Policy) const { 21 unsigned SpellingIndex = getAttributeSpellingListIndex(); 22 // For "#pragma unroll" and "#pragma nounroll" the string "unroll" or 23 // "nounroll" is already emitted as the pragma name. 24 if (SpellingIndex == Pragma_nounroll || 25 SpellingIndex == Pragma_nounroll_and_jam) 26 return; 27 else if (SpellingIndex == Pragma_unroll || 28 SpellingIndex == Pragma_unroll_and_jam) { 29 OS << ' ' << getValueString(Policy); 30 return; 31 } 32 33 assert(SpellingIndex == Pragma_clang_loop && "Unexpected spelling"); 34 OS << ' ' << getOptionName(option) << getValueString(Policy); 35 } 36 37 // Return a string containing the loop hint argument including the 38 // enclosing parentheses. 39 std::string LoopHintAttr::getValueString(const PrintingPolicy &Policy) const { 40 std::string ValueName; 41 llvm::raw_string_ostream OS(ValueName); 42 OS << "("; 43 if (state == Numeric) 44 value->printPretty(OS, nullptr, Policy); 45 else if (state == FixedWidth || state == ScalableWidth) { 46 if (value) { 47 value->printPretty(OS, nullptr, Policy); 48 if (state == ScalableWidth) 49 OS << ", scalable"; 50 } else if (state == ScalableWidth) 51 OS << "scalable"; 52 else 53 OS << "fixed"; 54 } else if (state == Enable) 55 OS << "enable"; 56 else if (state == Full) 57 OS << "full"; 58 else if (state == AssumeSafety) 59 OS << "assume_safety"; 60 else 61 OS << "disable"; 62 OS << ")"; 63 return ValueName; 64 } 65 66 // Return a string suitable for identifying this attribute in diagnostics. 67 std::string 68 LoopHintAttr::getDiagnosticName(const PrintingPolicy &Policy) const { 69 unsigned SpellingIndex = getAttributeSpellingListIndex(); 70 if (SpellingIndex == Pragma_nounroll) 71 return "#pragma nounroll"; 72 else if (SpellingIndex == Pragma_unroll) 73 return "#pragma unroll" + 74 (option == UnrollCount ? getValueString(Policy) : ""); 75 else if (SpellingIndex == Pragma_nounroll_and_jam) 76 return "#pragma nounroll_and_jam"; 77 else if (SpellingIndex == Pragma_unroll_and_jam) 78 return "#pragma unroll_and_jam" + 79 (option == UnrollAndJamCount ? getValueString(Policy) : ""); 80 81 assert(SpellingIndex == Pragma_clang_loop && "Unexpected spelling"); 82 return getOptionName(option) + getValueString(Policy); 83 } 84 85 void OMPDeclareSimdDeclAttr::printPrettyPragma( 86 raw_ostream &OS, const PrintingPolicy &Policy) const { 87 if (getBranchState() != BS_Undefined) 88 OS << ' ' << ConvertBranchStateTyToStr(getBranchState()); 89 if (auto *E = getSimdlen()) { 90 OS << " simdlen("; 91 E->printPretty(OS, nullptr, Policy); 92 OS << ")"; 93 } 94 if (uniforms_size() > 0) { 95 OS << " uniform"; 96 StringRef Sep = "("; 97 for (auto *E : uniforms()) { 98 OS << Sep; 99 E->printPretty(OS, nullptr, Policy); 100 Sep = ", "; 101 } 102 OS << ")"; 103 } 104 alignments_iterator NI = alignments_begin(); 105 for (auto *E : aligneds()) { 106 OS << " aligned("; 107 E->printPretty(OS, nullptr, Policy); 108 if (*NI) { 109 OS << ": "; 110 (*NI)->printPretty(OS, nullptr, Policy); 111 } 112 OS << ")"; 113 ++NI; 114 } 115 steps_iterator I = steps_begin(); 116 modifiers_iterator MI = modifiers_begin(); 117 for (auto *E : linears()) { 118 OS << " linear("; 119 if (*MI != OMPC_LINEAR_unknown) 120 OS << getOpenMPSimpleClauseTypeName(llvm::omp::Clause::OMPC_linear, *MI) 121 << "("; 122 E->printPretty(OS, nullptr, Policy); 123 if (*MI != OMPC_LINEAR_unknown) 124 OS << ")"; 125 if (*I) { 126 OS << ": "; 127 (*I)->printPretty(OS, nullptr, Policy); 128 } 129 OS << ")"; 130 ++I; 131 ++MI; 132 } 133 } 134 135 void OMPDeclareTargetDeclAttr::printPrettyPragma( 136 raw_ostream &OS, const PrintingPolicy &Policy) const { 137 // Use fake syntax because it is for testing and debugging purpose only. 138 if (getDevType() != DT_Any) 139 OS << " device_type(" << ConvertDevTypeTyToStr(getDevType()) << ")"; 140 if (getMapType() != MT_To) 141 OS << ' ' << ConvertMapTypeTyToStr(getMapType()); 142 if (Expr *E = getIndirectExpr()) { 143 OS << " indirect("; 144 E->printPretty(OS, nullptr, Policy); 145 OS << ")"; 146 } else if (getIndirect()) { 147 OS << " indirect"; 148 } 149 } 150 151 llvm::Optional<OMPDeclareTargetDeclAttr *> 152 OMPDeclareTargetDeclAttr::getActiveAttr(const ValueDecl *VD) { 153 if (!VD->hasAttrs()) 154 return llvm::None; 155 unsigned Level = 0; 156 OMPDeclareTargetDeclAttr *FoundAttr = nullptr; 157 for (auto *Attr : VD->specific_attrs<OMPDeclareTargetDeclAttr>()) { 158 if (Level <= Attr->getLevel()) { 159 Level = Attr->getLevel(); 160 FoundAttr = Attr; 161 } 162 } 163 if (FoundAttr) 164 return FoundAttr; 165 return llvm::None; 166 } 167 168 llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> 169 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(const ValueDecl *VD) { 170 llvm::Optional<OMPDeclareTargetDeclAttr *> ActiveAttr = getActiveAttr(VD); 171 if (ActiveAttr) 172 return ActiveAttr.value()->getMapType(); 173 return llvm::None; 174 } 175 176 llvm::Optional<OMPDeclareTargetDeclAttr::DevTypeTy> 177 OMPDeclareTargetDeclAttr::getDeviceType(const ValueDecl *VD) { 178 llvm::Optional<OMPDeclareTargetDeclAttr *> ActiveAttr = getActiveAttr(VD); 179 if (ActiveAttr) 180 return ActiveAttr.value()->getDevType(); 181 return llvm::None; 182 } 183 184 llvm::Optional<SourceLocation> 185 OMPDeclareTargetDeclAttr::getLocation(const ValueDecl *VD) { 186 llvm::Optional<OMPDeclareTargetDeclAttr *> ActiveAttr = getActiveAttr(VD); 187 if (ActiveAttr) 188 return ActiveAttr.value()->getRange().getBegin(); 189 return llvm::None; 190 } 191 192 namespace clang { 193 llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const OMPTraitInfo &TI); 194 llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const OMPTraitInfo *TI); 195 } 196 197 void OMPDeclareVariantAttr::printPrettyPragma( 198 raw_ostream &OS, const PrintingPolicy &Policy) const { 199 if (const Expr *E = getVariantFuncRef()) { 200 OS << "("; 201 E->printPretty(OS, nullptr, Policy); 202 OS << ")"; 203 } 204 OS << " match(" << traitInfos << ")"; 205 206 auto PrintExprs = [&OS, &Policy](Expr **Begin, Expr **End) { 207 for (Expr **I = Begin; I != End; ++I) { 208 assert(*I && "Expected non-null Stmt"); 209 if (I != Begin) 210 OS << ","; 211 (*I)->printPretty(OS, nullptr, Policy); 212 } 213 }; 214 if (adjustArgsNothing_size()) { 215 OS << " adjust_args(nothing:"; 216 PrintExprs(adjustArgsNothing_begin(), adjustArgsNothing_end()); 217 OS << ")"; 218 } 219 if (adjustArgsNeedDevicePtr_size()) { 220 OS << " adjust_args(need_device_ptr:"; 221 PrintExprs(adjustArgsNeedDevicePtr_begin(), adjustArgsNeedDevicePtr_end()); 222 OS << ")"; 223 } 224 225 auto PrintInteropTypes = [&OS](InteropType *Begin, InteropType *End) { 226 for (InteropType *I = Begin; I != End; ++I) { 227 if (I != Begin) 228 OS << ", "; 229 OS << "interop("; 230 OS << ConvertInteropTypeToStr(*I); 231 OS << ")"; 232 } 233 }; 234 if (appendArgs_size()) { 235 OS << " append_args("; 236 PrintInteropTypes(appendArgs_begin(), appendArgs_end()); 237 OS << ")"; 238 } 239 } 240 241 #include "clang/AST/AttrImpl.inc" 242