1//===---- OpenACCClause.cpp - Classes for OpenACC Clauses ----------------===//
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 implements the subclasses of the OpenACCClause class declared in
10// OpenACCClause.h
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/AST/OpenACCClause.h"
15#include "clang/AST/ASTContext.h"
16#include "clang/AST/Expr.h"
17
18using namespace clang;
19
20OpenACCDefaultClause *OpenACCDefaultClause::Create(const ASTContext &C,
21 OpenACCDefaultClauseKind K,
22 SourceLocation BeginLoc,
23 SourceLocation LParenLoc,
24 SourceLocation EndLoc) {
25 void *Mem =
26 C.Allocate(Size: sizeof(OpenACCDefaultClause), Align: alignof(OpenACCDefaultClause));
27
28 return new (Mem) OpenACCDefaultClause(K, BeginLoc, LParenLoc, EndLoc);
29}
30
31OpenACCIfClause *OpenACCIfClause::Create(const ASTContext &C,
32 SourceLocation BeginLoc,
33 SourceLocation LParenLoc,
34 Expr *ConditionExpr,
35 SourceLocation EndLoc) {
36 void *Mem = C.Allocate(Size: sizeof(OpenACCIfClause), Align: alignof(OpenACCIfClause));
37 return new (Mem) OpenACCIfClause(BeginLoc, LParenLoc, ConditionExpr, EndLoc);
38}
39
40OpenACCIfClause::OpenACCIfClause(SourceLocation BeginLoc,
41 SourceLocation LParenLoc, Expr *ConditionExpr,
42 SourceLocation EndLoc)
43 : OpenACCClauseWithCondition(OpenACCClauseKind::If, BeginLoc, LParenLoc,
44 ConditionExpr, EndLoc) {
45 assert(ConditionExpr && "if clause requires condition expr");
46 assert((ConditionExpr->isInstantiationDependent() ||
47 ConditionExpr->getType()->isScalarType()) &&
48 "Condition expression type not scalar/dependent");
49}
50
51OpenACCSelfClause *OpenACCSelfClause::Create(const ASTContext &C,
52 SourceLocation BeginLoc,
53 SourceLocation LParenLoc,
54 Expr *ConditionExpr,
55 SourceLocation EndLoc) {
56 void *Mem = C.Allocate(Size: sizeof(OpenACCIfClause), Align: alignof(OpenACCIfClause));
57 return new (Mem)
58 OpenACCSelfClause(BeginLoc, LParenLoc, ConditionExpr, EndLoc);
59}
60
61OpenACCSelfClause::OpenACCSelfClause(SourceLocation BeginLoc,
62 SourceLocation LParenLoc,
63 Expr *ConditionExpr, SourceLocation EndLoc)
64 : OpenACCClauseWithCondition(OpenACCClauseKind::Self, BeginLoc, LParenLoc,
65 ConditionExpr, EndLoc) {
66 assert((!ConditionExpr || ConditionExpr->isInstantiationDependent() ||
67 ConditionExpr->getType()->isScalarType()) &&
68 "Condition expression type not scalar/dependent");
69}
70
71OpenACCClause::child_range OpenACCClause::children() {
72 switch (getClauseKind()) {
73 default:
74 assert(false && "Clause children function not implemented");
75 break;
76#define VISIT_CLAUSE(CLAUSE_NAME) \
77 case OpenACCClauseKind::CLAUSE_NAME: \
78 return cast<OpenACC##CLAUSE_NAME##Clause>(this)->children();
79
80#include "clang/Basic/OpenACCClauses.def"
81 }
82 return child_range(child_iterator(), child_iterator());
83}
84
85OpenACCNumWorkersClause::OpenACCNumWorkersClause(SourceLocation BeginLoc,
86 SourceLocation LParenLoc,
87 Expr *IntExpr,
88 SourceLocation EndLoc)
89 : OpenACCClauseWithSingleIntExpr(OpenACCClauseKind::NumWorkers, BeginLoc,
90 LParenLoc, IntExpr, EndLoc) {
91 assert((!IntExpr || IntExpr->isInstantiationDependent() ||
92 IntExpr->getType()->isIntegerType()) &&
93 "Condition expression type not scalar/dependent");
94}
95
96OpenACCNumWorkersClause *
97OpenACCNumWorkersClause::Create(const ASTContext &C, SourceLocation BeginLoc,
98 SourceLocation LParenLoc, Expr *IntExpr,
99 SourceLocation EndLoc) {
100 void *Mem = C.Allocate(Size: sizeof(OpenACCNumWorkersClause),
101 Align: alignof(OpenACCNumWorkersClause));
102 return new (Mem)
103 OpenACCNumWorkersClause(BeginLoc, LParenLoc, IntExpr, EndLoc);
104}
105
106OpenACCVectorLengthClause::OpenACCVectorLengthClause(SourceLocation BeginLoc,
107 SourceLocation LParenLoc,
108 Expr *IntExpr,
109 SourceLocation EndLoc)
110 : OpenACCClauseWithSingleIntExpr(OpenACCClauseKind::VectorLength, BeginLoc,
111 LParenLoc, IntExpr, EndLoc) {
112 assert((!IntExpr || IntExpr->isInstantiationDependent() ||
113 IntExpr->getType()->isIntegerType()) &&
114 "Condition expression type not scalar/dependent");
115}
116
117OpenACCVectorLengthClause *
118OpenACCVectorLengthClause::Create(const ASTContext &C, SourceLocation BeginLoc,
119 SourceLocation LParenLoc, Expr *IntExpr,
120 SourceLocation EndLoc) {
121 void *Mem = C.Allocate(Size: sizeof(OpenACCVectorLengthClause),
122 Align: alignof(OpenACCVectorLengthClause));
123 return new (Mem)
124 OpenACCVectorLengthClause(BeginLoc, LParenLoc, IntExpr, EndLoc);
125}
126
127OpenACCNumGangsClause *OpenACCNumGangsClause::Create(const ASTContext &C,
128 SourceLocation BeginLoc,
129 SourceLocation LParenLoc,
130 ArrayRef<Expr *> IntExprs,
131 SourceLocation EndLoc) {
132 void *Mem = C.Allocate(
133 Size: OpenACCNumGangsClause::totalSizeToAlloc<Expr *>(Counts: IntExprs.size()));
134 return new (Mem) OpenACCNumGangsClause(BeginLoc, LParenLoc, IntExprs, EndLoc);
135}
136
137//===----------------------------------------------------------------------===//
138// OpenACC clauses printing methods
139//===----------------------------------------------------------------------===//
140void OpenACCClausePrinter::VisitDefaultClause(const OpenACCDefaultClause &C) {
141 OS << "default(" << C.getDefaultClauseKind() << ")";
142}
143
144void OpenACCClausePrinter::VisitIfClause(const OpenACCIfClause &C) {
145 OS << "if(" << C.getConditionExpr() << ")";
146}
147
148void OpenACCClausePrinter::VisitSelfClause(const OpenACCSelfClause &C) {
149 OS << "self";
150 if (const Expr *CondExpr = C.getConditionExpr())
151 OS << "(" << CondExpr << ")";
152}
153
154void OpenACCClausePrinter::VisitNumGangsClause(const OpenACCNumGangsClause &C) {
155 OS << "num_gangs(";
156 llvm::interleaveComma(c: C.getIntExprs(), os&: OS);
157 OS << ")";
158}
159
160void OpenACCClausePrinter::VisitNumWorkersClause(
161 const OpenACCNumWorkersClause &C) {
162 OS << "num_workers(" << C.getIntExpr() << ")";
163}
164
165void OpenACCClausePrinter::VisitVectorLengthClause(
166 const OpenACCVectorLengthClause &C) {
167 OS << "vector_length(" << C.getIntExpr() << ")";
168}
169

source code of clang/lib/AST/OpenACCClause.cpp