blob: 09ecfe090922e510a9f3a33039d7cfea8ab877a0 [file] [log] [blame]
Alexey Bataevc6400582013-03-22 06:34:35 +00001//===--- SemaOpenMP.cpp - Semantic Analysis for OpenMP constructs ----------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9/// \file
10/// \brief This file implements semantic analysis for OpenMP directives and
Alexey Bataev6af701f2013-05-13 04:18:18 +000011/// clauses.
Alexey Bataevc6400582013-03-22 06:34:35 +000012///
13//===----------------------------------------------------------------------===//
14
15#include "clang/Basic/OpenMPKinds.h"
16#include "clang/AST/Decl.h"
Alexey Bataev4fa7eab2013-07-19 03:13:43 +000017#include "clang/AST/DeclCXX.h"
Alexey Bataevc6400582013-03-22 06:34:35 +000018#include "clang/AST/DeclOpenMP.h"
Alexey Bataev4fa7eab2013-07-19 03:13:43 +000019#include "clang/AST/StmtCXX.h"
20#include "clang/AST/StmtOpenMP.h"
21#include "clang/AST/StmtVisitor.h"
Alexey Bataevc6400582013-03-22 06:34:35 +000022#include "clang/Lex/Preprocessor.h"
Alexey Bataev4fa7eab2013-07-19 03:13:43 +000023#include "clang/Sema/Initialization.h"
Alexey Bataevc6400582013-03-22 06:34:35 +000024#include "clang/Sema/SemaInternal.h"
25#include "clang/Sema/Lookup.h"
Alexey Bataev4fa7eab2013-07-19 03:13:43 +000026#include "clang/Sema/Scope.h"
27#include "clang/Sema/ScopeInfo.h"
Alexey Bataevc6400582013-03-22 06:34:35 +000028using namespace clang;
29
30namespace {
31
Alexey Bataev6af701f2013-05-13 04:18:18 +000032class VarDeclFilterCCC : public CorrectionCandidateCallback {
33private:
34 Sema &Actions;
35public:
36 VarDeclFilterCCC(Sema &S) : Actions(S) { }
37 virtual bool ValidateCandidate(const TypoCorrection &Candidate) {
38 NamedDecl *ND = Candidate.getCorrectionDecl();
39 if (VarDecl *VD = dyn_cast_or_null<VarDecl>(ND)) {
40 return VD->hasGlobalStorage() &&
41 Actions.isDeclInScope(ND, Actions.getCurLexicalContext(),
42 Actions.getCurScope());
Alexey Bataevc6400582013-03-22 06:34:35 +000043 }
Alexey Bataev6af701f2013-05-13 04:18:18 +000044 return false;
Alexey Bataevc6400582013-03-22 06:34:35 +000045 }
Alexey Bataev6af701f2013-05-13 04:18:18 +000046};
47}
48
49ExprResult Sema::ActOnOpenMPIdExpression(Scope *CurScope,
50 CXXScopeSpec &ScopeSpec,
51 const DeclarationNameInfo &Id) {
52 LookupResult Lookup(*this, Id, LookupOrdinaryName);
53 LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
54
55 if (Lookup.isAmbiguous())
56 return ExprError();
57
58 VarDecl *VD;
59 if (!Lookup.isSingleResult()) {
60 VarDeclFilterCCC Validator(*this);
Richard Smith2d670972013-08-17 00:46:16 +000061 if (TypoCorrection Corrected = CorrectTypo(Id, LookupOrdinaryName, CurScope,
62 0, Validator)) {
63 diagnoseTypo(Corrected,
64 PDiag(Lookup.empty()? diag::err_undeclared_var_use_suggest
65 : diag::err_omp_expected_var_arg_suggest)
66 << Id.getName());
67 VD = Corrected.getCorrectionDeclAs<VarDecl>();
Alexey Bataev6af701f2013-05-13 04:18:18 +000068 } else {
Richard Smith2d670972013-08-17 00:46:16 +000069 Diag(Id.getLoc(), Lookup.empty() ? diag::err_undeclared_var_use
70 : diag::err_omp_expected_var_arg)
71 << Id.getName();
72 return ExprError();
Alexey Bataev6af701f2013-05-13 04:18:18 +000073 }
Alexey Bataev6af701f2013-05-13 04:18:18 +000074 } else {
75 if (!(VD = Lookup.getAsSingle<VarDecl>())) {
Richard Smith2d670972013-08-17 00:46:16 +000076 Diag(Id.getLoc(), diag::err_omp_expected_var_arg)
77 << Id.getName();
Alexey Bataev6af701f2013-05-13 04:18:18 +000078 Diag(Lookup.getFoundDecl()->getLocation(), diag::note_declared_at);
79 return ExprError();
80 }
81 }
82 Lookup.suppressDiagnostics();
83
84 // OpenMP [2.9.2, Syntax, C/C++]
85 // Variables must be file-scope, namespace-scope, or static block-scope.
86 if (!VD->hasGlobalStorage()) {
87 Diag(Id.getLoc(), diag::err_omp_global_var_arg)
88 << getOpenMPDirectiveName(OMPD_threadprivate)
89 << !VD->isStaticLocal();
90 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
91 VarDecl::DeclarationOnly;
92 Diag(VD->getLocation(),
93 IsDecl ? diag::note_previous_decl : diag::note_defined_here) << VD;
94 return ExprError();
95 }
96
97 // OpenMP [2.9.2, Restrictions, C/C++, p.2]
98 // A threadprivate directive for file-scope variables must appear outside
99 // any definition or declaration.
100 // OpenMP [2.9.2, Restrictions, C/C++, p.3]
101 // A threadprivate directive for static class member variables must appear
102 // in the class definition, in the same scope in which the member
103 // variables are declared.
104 // OpenMP [2.9.2, Restrictions, C/C++, p.4]
105 // A threadprivate directive for namespace-scope variables must appear
106 // outside any definition or declaration other than the namespace
107 // definition itself.
108 // OpenMP [2.9.2, Restrictions, C/C++, p.6]
109 // A threadprivate directive for static block-scope variables must appear
110 // in the scope of the variable and not in a nested scope.
111 NamedDecl *ND = cast<NamedDecl>(VD);
112 if (!isDeclInScope(ND, getCurLexicalContext(), CurScope)) {
113 Diag(Id.getLoc(), diag::err_omp_var_scope)
114 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
115 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
116 VarDecl::DeclarationOnly;
117 Diag(VD->getLocation(), IsDecl ? diag::note_previous_decl :
118 diag::note_defined_here) << VD;
119 return ExprError();
120 }
121
122 // OpenMP [2.9.2, Restrictions, C/C++, p.2-6]
123 // A threadprivate directive must lexically precede all references to any
124 // of the variables in its list.
125 if (VD->isUsed()) {
126 Diag(Id.getLoc(), diag::err_omp_var_used)
127 << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
128 return ExprError();
129 }
130
131 QualType ExprType = VD->getType().getNonReferenceType();
132 ExprResult DE = BuildDeclRefExpr(VD, ExprType, VK_RValue, Id.getLoc());
133 return DE;
134}
135
136Sema::DeclGroupPtrTy Sema::ActOnOpenMPThreadprivateDirective(
137 SourceLocation Loc,
138 ArrayRef<Expr *> VarList) {
139 if (OMPThreadPrivateDecl *D = CheckOMPThreadPrivateDecl(Loc, VarList)) {
Alexey Bataevc6400582013-03-22 06:34:35 +0000140 CurContext->addDecl(D);
141 return DeclGroupPtrTy::make(DeclGroupRef(D));
142 }
143 return DeclGroupPtrTy();
144}
145
146OMPThreadPrivateDecl *Sema::CheckOMPThreadPrivateDecl(
147 SourceLocation Loc,
Alexey Bataev6af701f2013-05-13 04:18:18 +0000148 ArrayRef<Expr *> VarList) {
149 SmallVector<Expr *, 8> Vars;
150 for (ArrayRef<Expr *>::iterator I = VarList.begin(),
Alexey Bataevc6400582013-03-22 06:34:35 +0000151 E = VarList.end();
152 I != E; ++I) {
Alexey Bataev6af701f2013-05-13 04:18:18 +0000153 DeclRefExpr *DE = cast<DeclRefExpr>(*I);
154 VarDecl *VD = cast<VarDecl>(DE->getDecl());
155 SourceLocation ILoc = DE->getExprLoc();
Alexey Bataevc6400582013-03-22 06:34:35 +0000156
157 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
158 // A threadprivate variable must not have an incomplete type.
159 if (RequireCompleteType(ILoc, VD->getType(),
Alexey Bataev6af701f2013-05-13 04:18:18 +0000160 diag::err_omp_threadprivate_incomplete_type)) {
Alexey Bataevc6400582013-03-22 06:34:35 +0000161 continue;
162 }
163
164 // OpenMP [2.9.2, Restrictions, C/C++, p.10]
165 // A threadprivate variable must not have a reference type.
166 if (VD->getType()->isReferenceType()) {
167 Diag(ILoc, diag::err_omp_ref_type_arg)
Alexey Bataev4fa7eab2013-07-19 03:13:43 +0000168 << getOpenMPDirectiveName(OMPD_threadprivate)
169 << VD->getType();
Alexey Bataev6af701f2013-05-13 04:18:18 +0000170 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
171 VarDecl::DeclarationOnly;
172 Diag(VD->getLocation(), IsDecl ? diag::note_previous_decl :
173 diag::note_defined_here) << VD;
Alexey Bataevc6400582013-03-22 06:34:35 +0000174 continue;
175 }
176
Richard Smith38afbc72013-04-13 02:43:54 +0000177 // Check if this is a TLS variable.
178 if (VD->getTLSKind()) {
Alexey Bataevc6400582013-03-22 06:34:35 +0000179 Diag(ILoc, diag::err_omp_var_thread_local) << VD;
Alexey Bataev6af701f2013-05-13 04:18:18 +0000180 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
181 VarDecl::DeclarationOnly;
182 Diag(VD->getLocation(), IsDecl ? diag::note_previous_decl :
183 diag::note_defined_here) << VD;
Alexey Bataevc6400582013-03-22 06:34:35 +0000184 continue;
185 }
186
187 Vars.push_back(*I);
188 }
189 return Vars.empty() ?
190 0 : OMPThreadPrivateDecl::Create(Context,
191 getCurLexicalContext(),
192 Loc, Vars);
193}
Alexey Bataev6af701f2013-05-13 04:18:18 +0000194
Alexey Bataev4fa7eab2013-07-19 03:13:43 +0000195StmtResult Sema::ActOnOpenMPExecutableDirective(OpenMPDirectiveKind Kind,
196 ArrayRef<OMPClause *> Clauses,
197 Stmt *AStmt,
198 SourceLocation StartLoc,
199 SourceLocation EndLoc) {
200 StmtResult Res = StmtError();
201 switch (Kind) {
202 case OMPD_parallel:
203 Res = ActOnOpenMPParallelDirective(Clauses, AStmt, StartLoc, EndLoc);
204 break;
205 case OMPD_threadprivate:
206 case OMPD_task:
207 llvm_unreachable("OpenMP Directive is not allowed");
208 case OMPD_unknown:
209 case NUM_OPENMP_DIRECTIVES:
210 llvm_unreachable("Unknown OpenMP directive");
211 }
212 return Res;
213}
214
215StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses,
216 Stmt *AStmt,
217 SourceLocation StartLoc,
218 SourceLocation EndLoc) {
219 getCurFunction()->setHasBranchProtectedScope();
220
221 return Owned(OMPParallelDirective::Create(Context, StartLoc, EndLoc,
222 Clauses, AStmt));
223}
224
225OMPClause *Sema::ActOnOpenMPSimpleClause(OpenMPClauseKind Kind,
226 unsigned Argument,
227 SourceLocation ArgumentLoc,
228 SourceLocation StartLoc,
229 SourceLocation LParenLoc,
230 SourceLocation EndLoc) {
231 OMPClause *Res = 0;
232 switch (Kind) {
233 case OMPC_default:
Rafael Espindola43678292013-09-03 14:33:09 +0000234 Res = ActOnOpenMPDefaultClause(
235 static_cast<OpenMPDefaultClauseKind>(Argument),
236 ArgumentLoc, StartLoc, LParenLoc, EndLoc);
Alexey Bataev4fa7eab2013-07-19 03:13:43 +0000237 break;
238 case OMPC_private:
239 case OMPC_threadprivate:
240 case OMPC_unknown:
241 case NUM_OPENMP_CLAUSES:
242 llvm_unreachable("Clause is not allowed.");
243 }
244 return Res;
245}
246
247OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind,
248 SourceLocation KindKwLoc,
249 SourceLocation StartLoc,
250 SourceLocation LParenLoc,
251 SourceLocation EndLoc) {
252 if (Kind == OMPC_DEFAULT_unknown) {
253 std::string Values;
254 std::string Sep(NUM_OPENMP_DEFAULT_KINDS > 1 ? ", " : "");
255 for (unsigned i = OMPC_DEFAULT_unknown + 1;
256 i < NUM_OPENMP_DEFAULT_KINDS; ++i) {
257 Values += "'";
258 Values += getOpenMPSimpleClauseTypeName(OMPC_default, i);
259 Values += "'";
260 switch (i) {
261 case NUM_OPENMP_DEFAULT_KINDS - 2:
262 Values += " or ";
263 break;
264 case NUM_OPENMP_DEFAULT_KINDS - 1:
265 break;
266 default:
267 Values += Sep;
268 break;
269 }
270 }
271 Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
272 << Values << getOpenMPClauseName(OMPC_default);
273 return 0;
274 }
275 return new (Context) OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc,
276 EndLoc);
277}
278
279OMPClause *Sema::ActOnOpenMPVarListClause(OpenMPClauseKind Kind,
280 ArrayRef<Expr *> VarList,
281 SourceLocation StartLoc,
282 SourceLocation LParenLoc,
283 SourceLocation EndLoc) {
284 OMPClause *Res = 0;
285 switch (Kind) {
286 case OMPC_private:
287 Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
288 break;
289 case OMPC_default:
290 case OMPC_threadprivate:
291 case OMPC_unknown:
292 case NUM_OPENMP_CLAUSES:
293 llvm_unreachable("Clause is not allowed.");
294 }
295 return Res;
296}
297
298OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
299 SourceLocation StartLoc,
300 SourceLocation LParenLoc,
301 SourceLocation EndLoc) {
302 SmallVector<Expr *, 8> Vars;
303 for (ArrayRef<Expr *>::iterator I = VarList.begin(), E = VarList.end();
304 I != E; ++I) {
305 if (*I && isa<DependentScopeDeclRefExpr>(*I)) {
306 // It will be analyzed later.
307 Vars.push_back(*I);
308 continue;
309 }
310
311 SourceLocation ELoc = (*I)->getExprLoc();
312 // OpenMP [2.1, C/C++]
313 // A list item is a variable name.
314 // OpenMP [2.9.3.3, Restrictions, p.1]
315 // A variable that is part of another variable (as an array or
316 // structure element) cannot appear in a private clause.
317 DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(*I);
318 if (!DE || !isa<VarDecl>(DE->getDecl())) {
319 Diag(ELoc, diag::err_omp_expected_var_name)
320 << (*I)->getSourceRange();
321 continue;
322 }
323 Decl *D = DE->getDecl();
324 VarDecl *VD = cast<VarDecl>(D);
325
326 QualType Type = VD->getType();
327 if (Type->isDependentType() || Type->isInstantiationDependentType()) {
328 // It will be analyzed later.
329 Vars.push_back(DE);
330 continue;
331 }
332
333 // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
334 // A variable that appears in a private clause must not have an incomplete
335 // type or a reference type.
336 if (RequireCompleteType(ELoc, Type,
337 diag::err_omp_private_incomplete_type)) {
338 continue;
339 }
340 if (Type->isReferenceType()) {
341 Diag(ELoc, diag::err_omp_clause_ref_type_arg)
342 << getOpenMPClauseName(OMPC_private) << Type;
343 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
344 VarDecl::DeclarationOnly;
345 Diag(VD->getLocation(), IsDecl ? diag::note_previous_decl :
346 diag::note_defined_here) << VD;
347 continue;
348 }
349
350 // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
351 // A variable of class type (or array thereof) that appears in a private
352 // clause requires an accesible, unambiguous default constructor for the
353 // class type.
354 while (Type.getNonReferenceType()->isArrayType()) {
355 Type = cast<ArrayType>(
356 Type.getNonReferenceType().getTypePtr())->getElementType();
357 }
358 CXXRecordDecl *RD = getLangOpts().CPlusPlus ?
359 Type.getNonReferenceType()->getAsCXXRecordDecl() : 0;
360 if (RD) {
361 CXXConstructorDecl *CD = LookupDefaultConstructor(RD);
362 PartialDiagnostic PD =
363 PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
364 if (!CD ||
365 CheckConstructorAccess(ELoc, CD,
366 InitializedEntity::InitializeTemporary(Type),
367 CD->getAccess(), PD) == AR_inaccessible ||
368 CD->isDeleted()) {
369 Diag(ELoc, diag::err_omp_required_method)
370 << getOpenMPClauseName(OMPC_private) << 0;
371 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
372 VarDecl::DeclarationOnly;
373 Diag(VD->getLocation(), IsDecl ? diag::note_previous_decl :
374 diag::note_defined_here) << VD;
375 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
376 continue;
377 }
378 MarkFunctionReferenced(ELoc, CD);
379 DiagnoseUseOfDecl(CD, ELoc);
380
381 CXXDestructorDecl *DD = RD->getDestructor();
382 if (DD) {
383 if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible ||
384 DD->isDeleted()) {
385 Diag(ELoc, diag::err_omp_required_method)
386 << getOpenMPClauseName(OMPC_private) << 4;
387 bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
388 VarDecl::DeclarationOnly;
389 Diag(VD->getLocation(), IsDecl ? diag::note_previous_decl :
390 diag::note_defined_here) << VD;
391 Diag(RD->getLocation(), diag::note_previous_decl) << RD;
392 continue;
393 }
394 MarkFunctionReferenced(ELoc, DD);
395 DiagnoseUseOfDecl(DD, ELoc);
396 }
397 }
398
399 Vars.push_back(DE);
400 }
401
402 if (Vars.empty()) return 0;
403
404 return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
405}
406