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 == Enable) 46 OS << "enable"; 47 else if (state == Full) 48 OS << "full"; 49 else if (state == AssumeSafety) 50 OS << "assume_safety"; 51 else 52 OS << "disable"; 53 OS << ")"; 54 return OS.str(); 55 } 56 57 // Return a string suitable for identifying this attribute in diagnostics. 58 std::string 59 LoopHintAttr::getDiagnosticName(const PrintingPolicy &Policy) const { 60 unsigned SpellingIndex = getAttributeSpellingListIndex(); 61 if (SpellingIndex == Pragma_nounroll) 62 return "#pragma nounroll"; 63 else if (SpellingIndex == Pragma_unroll) 64 return "#pragma unroll" + 65 (option == UnrollCount ? getValueString(Policy) : ""); 66 else if (SpellingIndex == Pragma_nounroll_and_jam) 67 return "#pragma nounroll_and_jam"; 68 else if (SpellingIndex == Pragma_unroll_and_jam) 69 return "#pragma unroll_and_jam" + 70 (option == UnrollAndJamCount ? getValueString(Policy) : ""); 71 72 assert(SpellingIndex == Pragma_clang_loop && "Unexpected spelling"); 73 return getOptionName(option) + getValueString(Policy); 74 } 75 76 void OMPDeclareSimdDeclAttr::printPrettyPragma( 77 raw_ostream &OS, const PrintingPolicy &Policy) const { 78 if (getBranchState() != BS_Undefined) 79 OS << ' ' << ConvertBranchStateTyToStr(getBranchState()); 80 if (auto *E = getSimdlen()) { 81 OS << " simdlen("; 82 E->printPretty(OS, nullptr, Policy); 83 OS << ")"; 84 } 85 if (uniforms_size() > 0) { 86 OS << " uniform"; 87 StringRef Sep = "("; 88 for (auto *E : uniforms()) { 89 OS << Sep; 90 E->printPretty(OS, nullptr, Policy); 91 Sep = ", "; 92 } 93 OS << ")"; 94 } 95 alignments_iterator NI = alignments_begin(); 96 for (auto *E : aligneds()) { 97 OS << " aligned("; 98 E->printPretty(OS, nullptr, Policy); 99 if (*NI) { 100 OS << ": "; 101 (*NI)->printPretty(OS, nullptr, Policy); 102 } 103 OS << ")"; 104 ++NI; 105 } 106 steps_iterator I = steps_begin(); 107 modifiers_iterator MI = modifiers_begin(); 108 for (auto *E : linears()) { 109 OS << " linear("; 110 if (*MI != OMPC_LINEAR_unknown) 111 OS << getOpenMPSimpleClauseTypeName(llvm::omp::Clause::OMPC_linear, *MI) 112 << "("; 113 E->printPretty(OS, nullptr, Policy); 114 if (*MI != OMPC_LINEAR_unknown) 115 OS << ")"; 116 if (*I) { 117 OS << ": "; 118 (*I)->printPretty(OS, nullptr, Policy); 119 } 120 OS << ")"; 121 ++I; 122 ++MI; 123 } 124 } 125 126 void OMPDeclareTargetDeclAttr::printPrettyPragma( 127 raw_ostream &OS, const PrintingPolicy &Policy) const { 128 // Use fake syntax because it is for testing and debugging purpose only. 129 if (getDevType() != DT_Any) 130 OS << " device_type(" << ConvertDevTypeTyToStr(getDevType()) << ")"; 131 if (getMapType() != MT_To) 132 OS << ' ' << ConvertMapTypeTyToStr(getMapType()); 133 } 134 135 llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy> 136 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(const ValueDecl *VD) { 137 if (!VD->hasAttrs()) 138 return llvm::None; 139 if (const auto *Attr = VD->getAttr<OMPDeclareTargetDeclAttr>()) 140 return Attr->getMapType(); 141 142 return llvm::None; 143 } 144 145 llvm::Optional<OMPDeclareTargetDeclAttr::DevTypeTy> 146 OMPDeclareTargetDeclAttr::getDeviceType(const ValueDecl *VD) { 147 if (!VD->hasAttrs()) 148 return llvm::None; 149 if (const auto *Attr = VD->getAttr<OMPDeclareTargetDeclAttr>()) 150 return Attr->getDevType(); 151 152 return llvm::None; 153 } 154 155 namespace clang { 156 llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const OMPTraitInfo &TI); 157 llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const OMPTraitInfo *TI); 158 } 159 160 void OMPDeclareVariantAttr::printPrettyPragma( 161 raw_ostream &OS, const PrintingPolicy &Policy) const { 162 if (const Expr *E = getVariantFuncRef()) { 163 OS << "("; 164 E->printPretty(OS, nullptr, Policy); 165 OS << ")"; 166 } 167 OS << " match(" << traitInfos << ")"; 168 } 169 170 #include "clang/AST/AttrImpl.inc" 171