blob: 7818fbb1918bb328d3aa45f7c4e7f295733687a4 [file] [log] [blame]
Anton Korobeynikov3d364fd2010-01-10 14:38:13 +00001//===--- AttrImpl.cpp - Classes for representing attributes -----*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Anton Korobeynikov3d364fd2010-01-10 14:38:13 +00006//
7//===----------------------------------------------------------------------===//
8//
Benjamin Kramer845e32c2015-03-19 16:06:49 +00009// This file contains out-of-line methods for Attr classes.
Anton Korobeynikov3d364fd2010-01-10 14:38:13 +000010//
11//===----------------------------------------------------------------------===//
12
Anton Korobeynikov3d364fd2010-01-10 14:38:13 +000013#include "clang/AST/ASTContext.h"
Mehdi Amini9670f842016-07-18 19:02:11 +000014#include "clang/AST/Attr.h"
Alexis Huntdcfba7b2010-08-18 23:23:40 +000015#include "clang/AST/Expr.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000016#include "clang/AST/Type.h"
Anton Korobeynikov3d364fd2010-01-10 14:38:13 +000017using namespace clang;
18
Reid Kleckner26d254f2020-03-11 20:22:14 -070019void 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.
39std::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.
58std::string
59LoopHintAttr::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
76void 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)
Johannes Doerfert419a5592020-03-30 19:58:40 -0500111 OS << getOpenMPSimpleClauseTypeName(llvm::omp::Clause::OMPC_linear, *MI)
112 << "(";
Reid Kleckner26d254f2020-03-11 20:22:14 -0700113 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
126void 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
135llvm::Optional<OMPDeclareTargetDeclAttr::MapTypeTy>
136OMPDeclareTargetDeclAttr::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
145llvm::Optional<OMPDeclareTargetDeclAttr::DevTypeTy>
146OMPDeclareTargetDeclAttr::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
Reid Klecknerba1ffd22020-04-03 12:35:30 -0700155namespace clang {
156llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const OMPTraitInfo &TI);
157llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const OMPTraitInfo *TI);
158}
159
Reid Kleckner26d254f2020-03-11 20:22:14 -0700160void 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 }
Reid Klecknerba1ffd22020-04-03 12:35:30 -0700167 OS << " match(" << traitInfos << ")";
Reid Kleckner26d254f2020-03-11 20:22:14 -0700168}
169
Alexis Huntdcfba7b2010-08-18 23:23:40 +0000170#include "clang/AST/AttrImpl.inc"