1//===- SemaSYCL.cpp - Semantic Analysis for SYCL constructs ---------------===//
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// This implements Semantic Analysis for SYCL constructs.
9//===----------------------------------------------------------------------===//
10
11#include "clang/Sema/SemaSYCL.h"
12#include "clang/AST/Mangle.h"
13#include "clang/Sema/Sema.h"
14#include "clang/Sema/SemaDiagnostic.h"
15
16using namespace clang;
17
18// -----------------------------------------------------------------------------
19// SYCL device specific diagnostics implementation
20// -----------------------------------------------------------------------------
21
22SemaSYCL::SemaSYCL(Sema &S) : SemaBase(S) {}
23
24Sema::SemaDiagnosticBuilder SemaSYCL::DiagIfDeviceCode(SourceLocation Loc,
25 unsigned DiagID) {
26 assert(getLangOpts().SYCLIsDevice &&
27 "Should only be called during SYCL compilation");
28 FunctionDecl *FD = dyn_cast<FunctionDecl>(Val: SemaRef.getCurLexicalContext());
29 SemaDiagnosticBuilder::Kind DiagKind = [this, FD] {
30 if (!FD)
31 return SemaDiagnosticBuilder::K_Nop;
32 if (SemaRef.getEmissionStatus(Decl: FD) == Sema::FunctionEmissionStatus::Emitted)
33 return SemaDiagnosticBuilder::K_ImmediateWithCallStack;
34 return SemaDiagnosticBuilder::K_Deferred;
35 }();
36 return SemaDiagnosticBuilder(DiagKind, Loc, DiagID, FD, SemaRef);
37}
38
39static bool isZeroSizedArray(SemaSYCL &S, QualType Ty) {
40 if (const auto *CAT = S.getASTContext().getAsConstantArrayType(T: Ty))
41 return CAT->isZeroSize();
42 return false;
43}
44
45void SemaSYCL::deepTypeCheckForDevice(SourceLocation UsedAt,
46 llvm::DenseSet<QualType> Visited,
47 ValueDecl *DeclToCheck) {
48 assert(getLangOpts().SYCLIsDevice &&
49 "Should only be called during SYCL compilation");
50 // Emit notes only for the first discovered declaration of unsupported type
51 // to avoid mess of notes. This flag is to track that error already happened.
52 bool NeedToEmitNotes = true;
53
54 auto Check = [&](QualType TypeToCheck, const ValueDecl *D) {
55 bool ErrorFound = false;
56 if (isZeroSizedArray(S&: *this, Ty: TypeToCheck)) {
57 DiagIfDeviceCode(UsedAt, diag::err_typecheck_zero_array_size) << 1;
58 ErrorFound = true;
59 }
60 // Checks for other types can also be done here.
61 if (ErrorFound) {
62 if (NeedToEmitNotes) {
63 if (auto *FD = dyn_cast<FieldDecl>(D))
64 DiagIfDeviceCode(FD->getLocation(),
65 diag::note_illegal_field_declared_here)
66 << FD->getType()->isPointerType() << FD->getType();
67 else
68 DiagIfDeviceCode(D->getLocation(), diag::note_declared_at);
69 }
70 }
71
72 return ErrorFound;
73 };
74
75 // In case we have a Record used do the DFS for a bad field.
76 SmallVector<const ValueDecl *, 4> StackForRecursion;
77 StackForRecursion.push_back(Elt: DeclToCheck);
78
79 // While doing DFS save how we get there to emit a nice set of notes.
80 SmallVector<const FieldDecl *, 4> History;
81 History.push_back(Elt: nullptr);
82
83 do {
84 const ValueDecl *Next = StackForRecursion.pop_back_val();
85 if (!Next) {
86 assert(!History.empty());
87 // Found a marker, we have gone up a level.
88 History.pop_back();
89 continue;
90 }
91 QualType NextTy = Next->getType();
92
93 if (!Visited.insert(V: NextTy).second)
94 continue;
95
96 auto EmitHistory = [&]() {
97 // The first element is always nullptr.
98 for (uint64_t Index = 1; Index < History.size(); ++Index) {
99 DiagIfDeviceCode(History[Index]->getLocation(),
100 diag::note_within_field_of_type)
101 << History[Index]->getType();
102 }
103 };
104
105 if (Check(NextTy, Next)) {
106 if (NeedToEmitNotes)
107 EmitHistory();
108 NeedToEmitNotes = false;
109 }
110
111 // In case pointer/array/reference type is met get pointee type, then
112 // proceed with that type.
113 while (NextTy->isAnyPointerType() || NextTy->isArrayType() ||
114 NextTy->isReferenceType()) {
115 if (NextTy->isArrayType())
116 NextTy = QualType{NextTy->getArrayElementTypeNoTypeQual(), 0};
117 else
118 NextTy = NextTy->getPointeeType();
119 if (Check(NextTy, Next)) {
120 if (NeedToEmitNotes)
121 EmitHistory();
122 NeedToEmitNotes = false;
123 }
124 }
125
126 if (const auto *RecDecl = NextTy->getAsRecordDecl()) {
127 if (auto *NextFD = dyn_cast<FieldDecl>(Val: Next))
128 History.push_back(Elt: NextFD);
129 // When nullptr is discovered, this means we've gone back up a level, so
130 // the history should be cleaned.
131 StackForRecursion.push_back(Elt: nullptr);
132 llvm::copy(Range: RecDecl->fields(), Out: std::back_inserter(x&: StackForRecursion));
133 }
134 } while (!StackForRecursion.empty());
135}
136
137ExprResult SemaSYCL::BuildUniqueStableNameExpr(SourceLocation OpLoc,
138 SourceLocation LParen,
139 SourceLocation RParen,
140 TypeSourceInfo *TSI) {
141 return SYCLUniqueStableNameExpr::Create(Ctx: getASTContext(), OpLoc, LParen,
142 RParen, TSI);
143}
144
145ExprResult SemaSYCL::ActOnUniqueStableNameExpr(SourceLocation OpLoc,
146 SourceLocation LParen,
147 SourceLocation RParen,
148 ParsedType ParsedTy) {
149 TypeSourceInfo *TSI = nullptr;
150 QualType Ty = SemaRef.GetTypeFromParser(Ty: ParsedTy, TInfo: &TSI);
151
152 if (Ty.isNull())
153 return ExprError();
154 if (!TSI)
155 TSI = getASTContext().getTrivialTypeSourceInfo(T: Ty, Loc: LParen);
156
157 return BuildUniqueStableNameExpr(OpLoc, LParen, RParen, TSI);
158}
159

source code of clang/lib/Sema/SemaSYCL.cpp