1 //===--- AttrImpl.cpp - Classes for representing attributes -----*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 // This file contains out-of-line methods for Attr classes.
11 //===----------------------------------------------------------------------===//
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;
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)
27 else if (SpellingIndex == Pragma_unroll ||
28 SpellingIndex == Pragma_unroll_and_jam) {
29 OS << ' ' << getValueString(Policy);
33 assert(SpellingIndex == Pragma_clang_loop && "Unexpected spelling");
34 OS << ' ' << getOptionName(option) << getValueString(Policy);
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);
44 value->printPretty(OS, nullptr, Policy);
45 else if (state == Enable)
47 else if (state == Full)
49 else if (state == AssumeSafety)
50 OS << "assume_safety";
57 // Return a string suitable for identifying this attribute in diagnostics.
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) : "");
72 assert(SpellingIndex == Pragma_clang_loop && "Unexpected spelling");
73 return getOptionName(option) + getValueString(Policy);
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()) {
82 E->printPretty(OS, nullptr, Policy);
85 if (uniforms_size() > 0) {
88 for (auto *E : uniforms()) {
90 E->printPretty(OS, nullptr, Policy);
95 alignments_iterator NI = alignments_begin();
96 for (auto *E : aligneds()) {
98 E->printPretty(OS, nullptr, Policy);
101 (*NI)->printPretty(OS, nullptr, Policy);
106 steps_iterator I = steps_begin();
107 modifiers_iterator MI = modifiers_begin();
108 for (auto *E : linears()) {
110 if (*MI != OMPC_LINEAR_unknown)
111 OS << getOpenMPSimpleClauseTypeName(OMPC_linear, *MI) << "(";
112 E->printPretty(OS, nullptr, Policy);
113 if (*MI != OMPC_LINEAR_unknown)
117 (*I)->printPretty(OS, nullptr, Policy);
125 void OMPDeclareTargetDeclAttr::printPrettyPragma(
126 raw_ostream &OS, const PrintingPolicy &Policy) const {
127 // Use fake syntax because it is for testing and debugging purpose only.
128 if (getDevType() != DT_Any)
129 OS << " device_type(" << ConvertDevTypeTyToStr(getDevType()) << ")";
130 if (getMapType() != MT_To)
131 OS << ' ' << ConvertMapTypeTyToStr(getMapType());
134 llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy>
135 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(const ValueDecl *VD) {
138 if (const auto *Attr = VD->getAttr<OMPDeclareTargetDeclAttr>())
139 return Attr->getMapType();
144 llvm::Optional<OMPDeclareTargetDeclAttr::DevTypeTy>
145 OMPDeclareTargetDeclAttr::getDeviceType(const ValueDecl *VD) {
148 if (const auto *Attr = VD->getAttr<OMPDeclareTargetDeclAttr>())
149 return Attr->getDevType();
154 void OMPDeclareVariantAttr::printPrettyPragma(
155 raw_ostream &OS, const PrintingPolicy &Policy) const {
156 if (const Expr *E = getVariantFuncRef()) {
158 E->printPretty(OS, nullptr, Policy);
162 traitInfos.print(OS, Policy);
166 #include "clang/AST/AttrImpl.inc"