clang: lib/Sema/SemaSYCL.cpp Source File (original) (raw)
1
2
3
4
5
6
7
8
9
10
21
22using namespace clang;
23
24
25
26
27
29
31 unsigned DiagID) {
33 "Should only be called during SYCL compilation");
35 SemaDiagnosticBuilder::Kind DiagKind = [this, FD] {
36 if (!FD)
37 return SemaDiagnosticBuilder::K_Nop;
39 return SemaDiagnosticBuilder::K_ImmediateWithCallStack;
40 return SemaDiagnosticBuilder::K_Deferred;
41 }();
43}
44
47 return CAT->isZeroSize();
48 return false;
49}
50
52 llvm::DenseSet Visited,
55 "Should only be called during SYCL compilation");
56
57
58 bool NeedToEmitNotes = true;
59
61 bool ErrorFound = false;
63 DiagIfDeviceCode(UsedAt, diag::err_typecheck_zero_array_size) << 1;
64 ErrorFound = true;
65 }
66
67 if (ErrorFound) {
68 if (NeedToEmitNotes) {
69 if (auto *FD = dyn_cast(D))
71 diag::note_illegal_field_declared_here)
72 << FD->getType()->isPointerType() << FD->getType();
73 else
75 }
76 }
77
78 return ErrorFound;
79 };
80
81
83 StackForRecursion.push_back(DeclToCheck);
84
85
87 History.push_back(nullptr);
88
89 do {
90 const ValueDecl *Next = StackForRecursion.pop_back_val();
91 if (!Next) {
92 assert(!History.empty());
93
94 History.pop_back();
95 continue;
96 }
97 QualType NextTy = Next->getType();
98
99 if (.insert(NextTy).second)
100 continue;
101
102 auto EmitHistory = [&]() {
103
104 for (uint64_t Index = 1; Index < History.size(); ++Index) {
106 diag::note_within_field_of_type)
107 << History[Index]->getType();
108 }
109 };
110
111 if (Check(NextTy, Next)) {
112 if (NeedToEmitNotes)
113 EmitHistory();
114 NeedToEmitNotes = false;
115 }
116
117
118
123 else
125 if (Check(NextTy, Next)) {
126 if (NeedToEmitNotes)
127 EmitHistory();
128 NeedToEmitNotes = false;
129 }
130 }
131
133 if (auto *NextFD = dyn_cast(Next))
134 History.push_back(NextFD);
135
136
137 StackForRecursion.push_back(nullptr);
138 llvm::copy(RecDecl->fields(), std::back_inserter(StackForRecursion));
139 }
140 } while (!StackForRecursion.empty());
141}
142
148 RParen, TSI);
149}
150
157
160 if (!TSI)
162
164}
165
167
168 const auto *FD = cast(D);
170 assert(FT && "Function template is expected");
171
172
174 if (TL->size() < 2) {
175 Diag(FT->getLocation(), diag::warn_sycl_kernel_num_of_template_params);
176 return;
177 }
178
179
180 for (unsigned I = 0; I < 2; ++I) {
182 if (isa(TParam)) {
184 diag::warn_sycl_kernel_invalid_template_param_type);
185 return;
186 }
187 }
188
189
191 Diag(FT->getLocation(), diag::warn_sycl_kernel_num_of_function_params);
192 return;
193 }
194
195
198 Diag(FT->getLocation(), diag::warn_sycl_kernel_return_type);
199 return;
200 }
201
202 handleSimpleAttribute(*this, D, AL);
203}
204
209 assert(TSI && "no type source info for attribute argument");
212}
213
214
215
216
217
218
219
223 if (const TagType *TT = dyn_cast(T))
224 Loc = TT->getDecl()->getLocation();
225 else if (const ObjCInterfaceType *ObjCIT = dyn_cast(T))
226 Loc = ObjCIT->getDecl()->getLocation();
227 return Loc;
228}
229
233
235
236
237
238
239
240
241
242
243 S.Diag(Loc, diag::warn_sycl_kernel_name_not_a_class_type) << KernelName;
245 if (DeclTypeLoc.isValid())
246 S.Diag(DeclTypeLoc, diag::note_entity_declared_at) << KernelName;
247 return true;
248 }
249
250 return false;
251}
252
254
255
256 SYCLKernelEntryPointAttr *SKEPAttr = nullptr;
257 for (auto *SAI : FD->specific_attrs()) {
258 if (!SKEPAttr) {
259 SKEPAttr = SAI;
260 continue;
261 }
262 if (().hasSameType(SAI->getKernelName(),
263 SKEPAttr->getKernelName())) {
264 Diag(SAI->getLocation(), diag::err_sycl_entry_point_invalid_redeclaration)
265 << SAI->getKernelName() << SKEPAttr->getKernelName();
266 Diag(SKEPAttr->getLocation(), diag::note_previous_attribute);
267 SAI->setInvalidAttr();
268 } else {
269 Diag(SAI->getLocation(),
270 diag::warn_sycl_entry_point_redundant_declaration);
271 Diag(SKEPAttr->getLocation(), diag::note_previous_attribute);
272 }
273 }
274 assert(SKEPAttr && "Missing sycl_kernel_entry_point attribute");
275
276
277 if (!SKEPAttr->getKernelName()->isDependentType() &&
279 SKEPAttr->getKernelName()))
280 SKEPAttr->setInvalidAttr();
281
282
283
286 const auto *PrevSKEPAttr = PrevFD->getAttr();
287 if (PrevSKEPAttr && !PrevSKEPAttr->isInvalidAttr()) {
289 PrevSKEPAttr->getKernelName())) {
290 Diag(SKEPAttr->getLocation(),
291 diag::err_sycl_entry_point_invalid_redeclaration)
292 << SKEPAttr->getKernelName() << PrevSKEPAttr->getKernelName();
293 Diag(PrevSKEPAttr->getLocation(), diag::note_previous_decl) << PrevFD;
294 SKEPAttr->setInvalidAttr();
295 }
296 }
297 }
298
299 if (const auto *MD = dyn_cast(FD)) {
300 if (!MD->isStatic()) {
301 Diag(SKEPAttr->getLocation(), diag::err_sycl_entry_point_invalid)
302 << 0;
303 SKEPAttr->setInvalidAttr();
304 }
305 }
306
308 Diag(SKEPAttr->getLocation(), diag::err_sycl_entry_point_invalid)
309 << 1;
310 SKEPAttr->setInvalidAttr();
311 }
312
314 Diag(SKEPAttr->getLocation(), diag::err_sycl_entry_point_invalid)
315 << 3;
316 SKEPAttr->setInvalidAttr();
318 Diag(SKEPAttr->getLocation(), diag::err_sycl_entry_point_invalid)
319 << 2;
320 SKEPAttr->setInvalidAttr();
321 }
322
324 Diag(SKEPAttr->getLocation(), diag::err_sycl_entry_point_invalid)
325 << 5;
326 SKEPAttr->setInvalidAttr();
328 Diag(SKEPAttr->getLocation(), diag::err_sycl_entry_point_invalid)
329 << 4;
330 SKEPAttr->setInvalidAttr();
331 }
332
334 Diag(SKEPAttr->getLocation(), diag::err_sycl_entry_point_invalid)
335 << 6;
336 SKEPAttr->setInvalidAttr();
337 }
338
340 Diag(SKEPAttr->getLocation(),
341 diag::err_sycl_entry_point_deduced_return_type);
342 SKEPAttr->setInvalidAttr();
345 Diag(SKEPAttr->getLocation(), diag::err_sycl_entry_point_return_type);
346 SKEPAttr->setInvalidAttr();
347 }
348
350 !SKEPAttr->isInvalidAttr()) {
353 if (SKI) {
355
356
357 Diag(FD->getLocation(), diag::err_sycl_kernel_name_conflict);
359 diag::note_previous_declaration);
360 SKEPAttr->setInvalidAttr();
361 }
362 } else {
364 }
365 }
366}
367
368namespace {
369
370
371
372
373
374
375class OutlinedFunctionDeclBodyInstantiator
376 : public TreeTransform {
377public:
378 using ParmDeclMap = llvm::DenseMap<ParmVarDecl *, VarDecl *>;
379
380 OutlinedFunctionDeclBodyInstantiator(Sema &S, ParmDeclMap &M)
381 : TreeTransform(S), SemaRef(S),
382 MapRef(M) {}
383
384
385 bool AlwaysRebuild() { return true; }
386
387
390 if (PVD) {
391 ParmDeclMap::iterator I = MapRef.find(PVD);
392 if (I != MapRef.end()) {
394 assert(SemaRef.getASTContext().hasSameUnqualifiedType(PVD->getType(),
397 SemaRef.getASTContext()));
403 }
404 }
405 return DRE;
406 }
407
408private:
409 Sema &SemaRef;
410 ParmDeclMap &MapRef;
411};
412
413}
414
420
421 const auto *SKEPAttr = FD->getAttr();
422 assert(SKEPAttr && "Missing sycl_kernel_entry_point attribute");
423 assert(!SKEPAttr->isInvalidAttr() &&
424 "sycl_kernel_entry_point attribute is invalid");
425
426
427
431 "SYCL kernel name conflict");
432 (void)SKI;
433
434 using ParmDeclMap = OutlinedFunctionDeclBodyInstantiator::ParmDeclMap;
435 ParmDeclMap ParmMap;
436
440 unsigned i = 0;
446 ParmMap[PVD] = IPD;
447 ++i;
448 }
449
450 OutlinedFunctionDeclBodyInstantiator OFDBodyInstantiator(SemaRef, ParmMap);
451 Stmt *OFDBody = OFDBodyInstantiator.TransformStmt(Body).get();
455
456 return NewBody;
457}
Defines the Diagnostic-related interfaces.
llvm::DenseSet< const void * > Visited
This file declares types used to describe SYCL kernels.
static SourceLocation SourceLocationForUserDeclaredType(QualType QT)
static bool isZeroSizedArray(SemaSYCL &S, QualType Ty)
static bool CheckSYCLKernelName(Sema &S, SourceLocation Loc, QualType KernelName)
This file declares semantic analysis for SYCL constructs.
This file defines SYCL AST classes used to represent calls to SYCL kernels.
Allows QualTypes to be sorted and hence used in maps and sets.
const ConstantArrayType * getAsConstantArrayType(QualType T) const
bool hasSameType(QualType T1, QualType T2) const
Determine whether the given types T1 and T2 are equivalent.
TypeSourceInfo * getTrivialTypeSourceInfo(QualType T, SourceLocation Loc=SourceLocation()) const
Allocate a TypeSourceInfo where all locations have been initialized to a given location,...
void registerSYCLEntryPointFunction(FunctionDecl *FD)
Generates and stores SYCL kernel metadata for the provided SYCL kernel entry point function.
const SYCLKernelInfo & getSYCLKernelInfo(QualType T) const
Given a type used as a SYCL kernel name, returns a reference to the metadata generated from the corre...
const SYCLKernelInfo * findSYCLKernelInfo(QualType T) const
Returns a pointer to the metadata generated from the corresponding SYCLkernel entry point if the prov...
CompoundStmt - This represents a group of statements like { stmt stmt }.
A reference to a declared variable, function, enum, etc.
DeclarationNameInfo getNameInfo() const
SourceLocation getTemplateKeywordLoc() const
Retrieve the location of the template keyword preceding this name, if any.
static DeclRefExpr * Create(const ASTContext &Context, NestedNameSpecifierLoc QualifierLoc, SourceLocation TemplateKWLoc, ValueDecl *D, bool RefersToEnclosingVariableOrCapture, SourceLocation NameLoc, QualType T, ExprValueKind VK, NamedDecl *FoundD=nullptr, const TemplateArgumentListInfo *TemplateArgs=nullptr, NonOdrUseReason NOUR=NOUR_None)
NestedNameSpecifierLoc getQualifierLoc() const
If the name was qualified, retrieves the nested-name-specifier that precedes the name,...
Decl - This represents one declaration (or definition), e.g.
bool isTemplated() const
Determine whether this declaration is a templated entity (whether it is.
bool isInvalidDecl() const
llvm::iterator_range< specific_attr_iterator< T > > specific_attrs() const
SourceLocation getLocation() const
void setIsUsed()
Set whether the declaration is used, in the sense of odr-use.
ExprValueKind getValueKind() const
getValueKind - The value kind that this expression produces.
Represents a function declaration or definition.
bool isNoReturn() const
Determines whether this function is known to be 'noreturn', through an attribute on its declaration o...
QualType getReturnType() const
ArrayRef< ParmVarDecl * > parameters() const
bool hasPrototype() const
Whether this function has a prototype, either because one was explicitly written or because it was "i...
bool isVariadic() const
Whether this function is variadic.
bool isDeleted() const
Whether this function has been deleted.
bool isConstexpr() const
Whether this is a (C++11) constexpr function or constexpr constructor.
bool isDefaulted() const
Whether this function is defaulted.
unsigned getNumParams() const
Return the number of parameters this function must have based on its FunctionType.
Declaration of a template function.
static ImplicitParamDecl * Create(ASTContext &C, DeclContext *DC, SourceLocation IdLoc, IdentifierInfo *Id, QualType T, ImplicitParamKind ParamKind)
Create implicit parameter.
This represents a decl that may have a name.
IdentifierInfo * getIdentifier() const
Get the identifier that names this declaration, if there is one.
Interfaces are the core concept in Objective-C for object oriented design.
Represents a partial function definition.
static OutlinedFunctionDecl * Create(ASTContext &C, DeclContext *DC, unsigned NumParams)
void setNothrow(bool Nothrow=true)
void setParam(unsigned i, ImplicitParamDecl *P)
Represents a parameter to a function.
ParsedAttr - Represents a syntactic attribute.
const ParsedType & getTypeArg() const
A (possibly-)qualified type.
bool isNull() const
Return true if this QualType doesn't point to a type yet.
bool isMoreQualifiedThan(QualType Other, const ASTContext &Ctx) const
Determine whether this type is more qualified than the other given type, requiring exact equality for...
decl_type * getPreviousDecl()
Return the previous declaration of this declaration or NULL if this is the first declaration.
SYCLKernelCallStmt represents the transformation that is applied to the body of a function declared w...
const FunctionDecl * getKernelEntryPointDecl() const
static SYCLUniqueStableNameExpr * Create(const ASTContext &Ctx, SourceLocation OpLoc, SourceLocation LParen, SourceLocation RParen, TypeSourceInfo *TSI)
A generic diagnostic builder for errors which may or may not be deferred.
SemaDiagnosticBuilder Diag(SourceLocation Loc, unsigned DiagID, bool DeferHint=false)
Emit a diagnostic.
ASTContext & getASTContext() const
const LangOptions & getLangOpts() const
SemaDiagnosticBuilder DiagIfDeviceCode(SourceLocation Loc, unsigned DiagID)
Creates a SemaDiagnosticBuilder that emits the diagnostic if the current context is "used as device c...
ExprResult BuildUniqueStableNameExpr(SourceLocation OpLoc, SourceLocation LParen, SourceLocation RParen, TypeSourceInfo *TSI)
void handleKernelEntryPointAttr(Decl *D, const ParsedAttr &AL)
void deepTypeCheckForDevice(SourceLocation UsedAt, llvm::DenseSet< QualType > Visited, ValueDecl *DeclToCheck)
void handleKernelAttr(Decl *D, const ParsedAttr &AL)
StmtResult BuildSYCLKernelCallStmt(FunctionDecl *FD, CompoundStmt *Body)
void CheckSYCLEntryPointFunctionDecl(FunctionDecl *FD)
ExprResult ActOnUniqueStableNameExpr(SourceLocation OpLoc, SourceLocation LParen, SourceLocation RParen, ParsedType ParsedTy)
Sema - This implements semantic analysis and AST building for C.
ASTContext & getASTContext() const
DeclContext * getCurLexicalContext() const
FunctionEmissionStatus getEmissionStatus(const FunctionDecl *Decl, bool Final=false)
DeclContext * CurContext
CurContext - This is the current declaration context of parsing.
static QualType GetTypeFromParser(ParsedType Ty, TypeSourceInfo **TInfo=nullptr)
Encodes a location in the source.
bool isValid() const
Return true if this is a valid SourceLocation object.
Stmt - This represents one statement.
TemplateParameterList * getTemplateParameters() const
Get the list of template parameters.
Stores a list of template parameters for a TemplateDecl and its derived classes.
NamedDecl * getParam(unsigned Idx)
A semantic tree transformation that allows one to transform one abstract syntax tree into another.
A container of type source information.
The base class of the type hierarchy.
bool isReferenceType() const
const Type * getArrayElementTypeNoTypeQual() const
If this is an array type, return the element type of the array, potentially with type qualifiers miss...
QualType getPointeeType() const
If this is a pointer, ObjC object pointer, or block pointer, this returns the respective pointee.
bool isDependentType() const
Whether this type is a dependent type, meaning that its definition somehow depends on a template para...
bool isUndeducedType() const
Determine whether this type is an undeduced type, meaning that it somehow involves a C++11 'auto' typ...
bool isStructureOrClassType() const
bool isAnyPointerType() const
const Type * getUnqualifiedDesugaredType() const
Return the specified type with any "sugar" removed from the type, removing any typedefs,...
RecordDecl * getAsRecordDecl() const
Retrieves the RecordDecl this type refers to.
Represent the declaration of a variable (in which case it is an lvalue) a function (in which case it ...
Represents a variable declaration or definition.
The JSON file list parser is used to communicate input to InstallAPI.
QualType getFunctionOrMethodResultType(const Decl *D)
const FunctionProtoType * T
unsigned getFunctionOrMethodNumParams(const Decl *D)
getFunctionOrMethodNumParams - Return number of function or method parameters.
bool declaresSameEntity(const Decl *D1, const Decl *D2)
Determine whether two declarations declare the same entity.
@ Other
Other implicit parameter.