]> CyberLeo.Net >> Repos - FreeBSD/releng/10.2.git/blob - contrib/llvm/tools/clang/lib/Sema/SemaOpenMP.cpp
- Copy stable/10@285827 to releng/10.2 in preparation for 10.2-RC1
[FreeBSD/releng/10.2.git] / contrib / llvm / tools / clang / lib / Sema / SemaOpenMP.cpp
1 //===--- 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
11 /// clauses.
12 ///
13 //===----------------------------------------------------------------------===//
14
15 #include "clang/Basic/OpenMPKinds.h"
16 #include "clang/AST/Decl.h"
17 #include "clang/AST/DeclCXX.h"
18 #include "clang/AST/DeclOpenMP.h"
19 #include "clang/AST/StmtCXX.h"
20 #include "clang/AST/StmtOpenMP.h"
21 #include "clang/AST/StmtVisitor.h"
22 #include "clang/Lex/Preprocessor.h"
23 #include "clang/Sema/Initialization.h"
24 #include "clang/Sema/SemaInternal.h"
25 #include "clang/Sema/Lookup.h"
26 #include "clang/Sema/Scope.h"
27 #include "clang/Sema/ScopeInfo.h"
28 using namespace clang;
29
30 //===----------------------------------------------------------------------===//
31 // Stack of data-sharing attributes for variables
32 //===----------------------------------------------------------------------===//
33
34 namespace {
35 /// \brief Default data sharing attributes, which can be applied to directive.
36 enum DefaultDataSharingAttributes {
37   DSA_unspecified = 0,   /// \brief Data sharing attribute not specified.
38   DSA_none = 1 << 0,     /// \brief Default data sharing attribute 'none'.
39   DSA_shared = 1 << 1    /// \brief Default data sharing attribute 'shared'.
40 };
41
42 /// \brief Stack for tracking declarations used in OpenMP directives and
43 /// clauses and their data-sharing attributes.
44 class DSAStackTy {
45 public:
46   struct DSAVarData {
47     OpenMPDirectiveKind DKind;
48     OpenMPClauseKind CKind;
49     DeclRefExpr *RefExpr;
50     DSAVarData() : DKind(OMPD_unknown), CKind(OMPC_unknown), RefExpr(0) { }
51   };
52 private:
53   struct DSAInfo {
54     OpenMPClauseKind Attributes;
55     DeclRefExpr *RefExpr;
56   };
57   typedef llvm::SmallDenseMap<VarDecl *, DSAInfo, 64> DeclSAMapTy;
58
59   struct SharingMapTy {
60     DeclSAMapTy SharingMap;
61     DefaultDataSharingAttributes DefaultAttr;
62     OpenMPDirectiveKind Directive;
63     DeclarationNameInfo DirectiveName;
64     Scope *CurScope;
65     SharingMapTy(OpenMPDirectiveKind DKind,
66                  const DeclarationNameInfo &Name,
67                  Scope *CurScope)
68       : SharingMap(), DefaultAttr(DSA_unspecified), Directive(DKind),
69         DirectiveName(Name), CurScope(CurScope) { }
70     SharingMapTy()
71       : SharingMap(), DefaultAttr(DSA_unspecified),
72         Directive(OMPD_unknown), DirectiveName(),
73         CurScope(0) { }
74   };
75
76   typedef SmallVector<SharingMapTy, 64> StackTy;
77
78   /// \brief Stack of used declaration and their data-sharing attributes.
79   StackTy Stack;
80   Sema &Actions;
81
82   typedef SmallVector<SharingMapTy, 8>::reverse_iterator reverse_iterator;
83
84   DSAVarData getDSA(StackTy::reverse_iterator Iter, VarDecl *D);
85 public:
86   explicit DSAStackTy(Sema &S) : Stack(1), Actions(S) { }
87
88   void push(OpenMPDirectiveKind DKind, const DeclarationNameInfo &DirName,
89             Scope *CurScope) {
90     Stack.push_back(SharingMapTy(DKind, DirName, CurScope));
91   }
92
93   void pop() {
94     assert(Stack.size() > 1 && "Data-sharing attributes stack is empty!");
95     Stack.pop_back();
96   }
97
98   /// \brief Adds explicit data sharing attribute to the specified declaration.
99   void addDSA(VarDecl *D, DeclRefExpr *E, OpenMPClauseKind A);
100
101   /// \brief Checks if the variable is a local for OpenMP region.
102   bool isOpenMPLocal(VarDecl *D);
103
104   /// \brief Returns data sharing attributes from top of the stack for the
105   /// specified declaration.
106   DSAVarData getTopDSA(VarDecl *D);
107   /// \brief Returns data-sharing attributes for the specified declaration.
108   DSAVarData getImplicitDSA(VarDecl *D);
109   /// \brief Checks if the specified variables has \a CKind data-sharing
110   /// attribute in \a DKind directive.
111   DSAVarData hasDSA(VarDecl *D, OpenMPClauseKind CKind,
112                     OpenMPDirectiveKind DKind = OMPD_unknown);
113
114
115   /// \brief Returns currently analyzed directive.
116   OpenMPDirectiveKind getCurrentDirective() const {
117     return Stack.back().Directive;
118   }
119
120   /// \brief Set default data sharing attribute to none.
121   void setDefaultDSANone() { Stack.back().DefaultAttr = DSA_none; }
122   /// \brief Set default data sharing attribute to shared.
123   void setDefaultDSAShared() { Stack.back().DefaultAttr = DSA_shared; }
124
125   DefaultDataSharingAttributes getDefaultDSA() const {
126     return Stack.back().DefaultAttr;
127   }
128
129   Scope *getCurScope() { return Stack.back().CurScope; }
130 };
131 } // end anonymous namespace.
132
133 DSAStackTy::DSAVarData DSAStackTy::getDSA(StackTy::reverse_iterator Iter,
134                                           VarDecl *D) {
135   DSAVarData DVar;
136   if (Iter == Stack.rend() - 1) {
137     // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
138     // in a region but not in construct]
139     //  File-scope or namespace-scope variables referenced in called routines
140     //  in the region are shared unless they appear in a threadprivate
141     //  directive.
142     // TODO
143     if (!D->isFunctionOrMethodVarDecl())
144       DVar.CKind = OMPC_shared;
145
146     // OpenMP [2.9.1.2, Data-sharing Attribute Rules for Variables Referenced
147     // in a region but not in construct]
148     //  Variables with static storage duration that are declared in called
149     //  routines in the region are shared.
150     if (D->hasGlobalStorage())
151       DVar.CKind = OMPC_shared;
152
153     return DVar;
154   }
155   DVar.DKind = Iter->Directive;
156   // Explicitly specified attributes and local variables with predetermined
157   // attributes.
158   if (Iter->SharingMap.count(D)) {
159     DVar.RefExpr = Iter->SharingMap[D].RefExpr;
160     DVar.CKind = Iter->SharingMap[D].Attributes;
161     return DVar;
162   }
163
164   // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
165   // in a Construct, C/C++, implicitly determined, p.1]
166   //  In a parallel or task construct, the data-sharing attributes of these
167   //  variables are determined by the default clause, if present.
168   switch (Iter->DefaultAttr) {
169   case DSA_shared:
170     DVar.CKind = OMPC_shared;
171     return DVar;
172   case DSA_none:
173     return DVar;
174   case DSA_unspecified:
175     // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
176     // in a Construct, implicitly determined, p.2]
177     //  In a parallel construct, if no default clause is present, these
178     //  variables are shared.
179     if (DVar.DKind == OMPD_parallel) {
180       DVar.CKind = OMPC_shared;
181       return DVar;
182     }
183
184     // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
185     // in a Construct, implicitly determined, p.4]
186     //  In a task construct, if no default clause is present, a variable that in
187     //  the enclosing context is determined to be shared by all implicit tasks
188     //  bound to the current team is shared.
189     // TODO
190     if (DVar.DKind == OMPD_task) {
191       DSAVarData DVarTemp;
192       for (StackTy::reverse_iterator I = Iter + 1,
193                                      EE = Stack.rend() - 1;
194            I != EE; ++I) {
195         // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
196         // in a Construct, implicitly determined, p.6]
197         //  In a task construct, if no default clause is present, a variable
198         //  whose data-sharing attribute is not determined by the rules above is
199         //  firstprivate.
200         DVarTemp = getDSA(I, D);
201         if (DVarTemp.CKind != OMPC_shared) {
202           DVar.RefExpr = 0;
203           DVar.DKind = OMPD_task;
204           DVar.CKind = OMPC_firstprivate;
205           return DVar;
206         }
207         if (I->Directive == OMPD_parallel) break;
208       }
209       DVar.DKind = OMPD_task;
210       DVar.CKind =
211         (DVarTemp.CKind == OMPC_unknown) ? OMPC_firstprivate : OMPC_shared;
212       return DVar;
213     }
214   }
215   // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
216   // in a Construct, implicitly determined, p.3]
217   //  For constructs other than task, if no default clause is present, these
218   //  variables inherit their data-sharing attributes from the enclosing
219   //  context.
220   return getDSA(Iter + 1, D);
221 }
222
223 void DSAStackTy::addDSA(VarDecl *D, DeclRefExpr *E, OpenMPClauseKind A) {
224   if (A == OMPC_threadprivate) {
225     Stack[0].SharingMap[D].Attributes = A;
226     Stack[0].SharingMap[D].RefExpr = E;
227   } else {
228     assert(Stack.size() > 1 && "Data-sharing attributes stack is empty");
229     Stack.back().SharingMap[D].Attributes = A;
230     Stack.back().SharingMap[D].RefExpr = E;
231   }
232 }
233
234 bool DSAStackTy::isOpenMPLocal(VarDecl *D) {
235   Scope *CurScope = getCurScope();
236   while (CurScope && !CurScope->isDeclScope(D))
237     CurScope = CurScope->getParent();
238   while (CurScope && !CurScope->isOpenMPDirectiveScope())
239     CurScope = CurScope->getParent();
240   bool isOpenMPLocal = !!CurScope;
241   if (!isOpenMPLocal) {
242     CurScope = getCurScope();
243     while (CurScope && !CurScope->isOpenMPDirectiveScope())
244       CurScope = CurScope->getParent();
245     isOpenMPLocal =
246       CurScope &&
247       isa<CapturedDecl>(D->getDeclContext()) &&
248       CurScope->getFnParent()->getEntity()->Encloses(D->getDeclContext());
249   }
250   return isOpenMPLocal;
251 }
252
253 DSAStackTy::DSAVarData DSAStackTy::getTopDSA(VarDecl *D) {
254   DSAVarData DVar;
255
256   // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
257   // in a Construct, C/C++, predetermined, p.1]
258   //  Variables appearing in threadprivate directives are threadprivate.
259   if (D->getTLSKind() != VarDecl::TLS_None) {
260     DVar.CKind = OMPC_threadprivate;
261     return DVar;
262   }
263   if (Stack[0].SharingMap.count(D)) {
264     DVar.RefExpr = Stack[0].SharingMap[D].RefExpr;
265     DVar.CKind = OMPC_threadprivate;
266     return DVar;
267   }
268
269   // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
270   // in a Construct, C/C++, predetermined, p.1]
271   // Variables with automatic storage duration that are declared in a scope
272   // inside the construct are private.
273   if (isOpenMPLocal(D) && D->isLocalVarDecl() &&
274       (D->getStorageClass() == SC_Auto ||
275        D->getStorageClass() == SC_None)) {
276     DVar.CKind = OMPC_private;
277     return DVar;
278   }
279
280   // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
281   // in a Construct, C/C++, predetermined, p.4]
282   //  Static data memebers are shared.
283   if (D->isStaticDataMember()) {
284     // Variables with const-qualified type having no mutable member may be listed
285     // in a firstprivate clause, even if they are static data members.
286     DSAVarData DVarTemp = hasDSA(D, OMPC_firstprivate);
287     if (DVarTemp.CKind == OMPC_firstprivate && DVarTemp.RefExpr)
288       return DVar;
289
290     DVar.CKind = OMPC_shared;
291     return DVar;
292   }
293
294   QualType Type = D->getType().getNonReferenceType().getCanonicalType();
295   bool IsConstant = Type.isConstant(Actions.getASTContext());
296   while (Type->isArrayType()) {
297     QualType ElemType = cast<ArrayType>(Type.getTypePtr())->getElementType();
298     Type = ElemType.getNonReferenceType().getCanonicalType();
299   }
300   // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
301   // in a Construct, C/C++, predetermined, p.6]
302   //  Variables with const qualified type having no mutable member are
303   //  shared.
304   CXXRecordDecl *RD = Actions.getLangOpts().CPlusPlus ?
305                                 Type->getAsCXXRecordDecl() : 0;
306   if (IsConstant &&
307       !(Actions.getLangOpts().CPlusPlus && RD && RD->hasMutableFields())) {
308     // Variables with const-qualified type having no mutable member may be
309     // listed in a firstprivate clause, even if they are static data members.
310     DSAVarData DVarTemp = hasDSA(D, OMPC_firstprivate);
311     if (DVarTemp.CKind == OMPC_firstprivate && DVarTemp.RefExpr)
312       return DVar;
313
314     DVar.CKind = OMPC_shared;
315     return DVar;
316   }
317
318   // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
319   // in a Construct, C/C++, predetermined, p.7]
320   //  Variables with static storage duration that are declared in a scope
321   //  inside the construct are shared.
322   if (isOpenMPLocal(D) && D->isStaticLocal()) {
323     DVar.CKind = OMPC_shared;
324     return DVar;
325   }
326
327   // Explicitly specified attributes and local variables with predetermined
328   // attributes.
329   if (Stack.back().SharingMap.count(D)) {
330     DVar.RefExpr = Stack.back().SharingMap[D].RefExpr;
331     DVar.CKind = Stack.back().SharingMap[D].Attributes;
332   }
333
334   return DVar;
335 }
336
337 DSAStackTy::DSAVarData DSAStackTy::getImplicitDSA(VarDecl *D) {
338   return getDSA(Stack.rbegin() + 1, D);
339 }
340
341 DSAStackTy::DSAVarData DSAStackTy::hasDSA(VarDecl *D, OpenMPClauseKind CKind,
342                                           OpenMPDirectiveKind DKind) {
343   for (StackTy::reverse_iterator I = Stack.rbegin() + 1,
344                                  E = Stack.rend() - 1;
345        I != E; ++I) {
346     if (DKind != OMPD_unknown && DKind != I->Directive) continue;
347     DSAVarData DVar = getDSA(I, D);
348     if (DVar.CKind == CKind)
349       return DVar;
350   }
351   return DSAVarData();
352 }
353
354 void Sema::InitDataSharingAttributesStack() {
355   VarDataSharingAttributesStack = new DSAStackTy(*this);
356 }
357
358 #define DSAStack static_cast<DSAStackTy *>(VarDataSharingAttributesStack)
359
360 void Sema::DestroyDataSharingAttributesStack() {
361   delete DSAStack;
362 }
363
364 void Sema::StartOpenMPDSABlock(OpenMPDirectiveKind DKind,
365                                const DeclarationNameInfo &DirName,
366                                Scope *CurScope) {
367   DSAStack->push(DKind, DirName, CurScope);
368   PushExpressionEvaluationContext(PotentiallyEvaluated);
369 }
370
371 void Sema::EndOpenMPDSABlock(Stmt *CurDirective) {
372   DSAStack->pop();
373   DiscardCleanupsInEvaluationContext();
374   PopExpressionEvaluationContext();
375 }
376
377 namespace {
378
379 class VarDeclFilterCCC : public CorrectionCandidateCallback {
380 private:
381   Sema &Actions;
382 public:
383   VarDeclFilterCCC(Sema &S) : Actions(S) { }
384   virtual bool ValidateCandidate(const TypoCorrection &Candidate) {
385     NamedDecl *ND = Candidate.getCorrectionDecl();
386     if (VarDecl *VD = dyn_cast_or_null<VarDecl>(ND)) {
387       return VD->hasGlobalStorage() &&
388              Actions.isDeclInScope(ND, Actions.getCurLexicalContext(),
389                                    Actions.getCurScope());
390     }
391     return false;
392   }
393 };
394 }
395
396 ExprResult Sema::ActOnOpenMPIdExpression(Scope *CurScope,
397                                          CXXScopeSpec &ScopeSpec,
398                                          const DeclarationNameInfo &Id) {
399   LookupResult Lookup(*this, Id, LookupOrdinaryName);
400   LookupParsedName(Lookup, CurScope, &ScopeSpec, true);
401
402   if (Lookup.isAmbiguous())
403     return ExprError();
404
405   VarDecl *VD;
406   if (!Lookup.isSingleResult()) {
407     VarDeclFilterCCC Validator(*this);
408     if (TypoCorrection Corrected = CorrectTypo(Id, LookupOrdinaryName, CurScope,
409                                                0, Validator)) {
410       diagnoseTypo(Corrected,
411                    PDiag(Lookup.empty()? diag::err_undeclared_var_use_suggest
412                                        : diag::err_omp_expected_var_arg_suggest)
413                      << Id.getName());
414       VD = Corrected.getCorrectionDeclAs<VarDecl>();
415     } else {
416       Diag(Id.getLoc(), Lookup.empty() ? diag::err_undeclared_var_use
417                                        : diag::err_omp_expected_var_arg)
418           << Id.getName();
419       return ExprError();
420     }
421   } else {
422     if (!(VD = Lookup.getAsSingle<VarDecl>())) {
423       Diag(Id.getLoc(), diag::err_omp_expected_var_arg)
424         << Id.getName();
425       Diag(Lookup.getFoundDecl()->getLocation(), diag::note_declared_at);
426       return ExprError();
427     }
428   }
429   Lookup.suppressDiagnostics();
430
431   // OpenMP [2.9.2, Syntax, C/C++]
432   //   Variables must be file-scope, namespace-scope, or static block-scope.
433   if (!VD->hasGlobalStorage()) {
434     Diag(Id.getLoc(), diag::err_omp_global_var_arg)
435       << getOpenMPDirectiveName(OMPD_threadprivate)
436       << !VD->isStaticLocal();
437     bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
438                   VarDecl::DeclarationOnly;
439     Diag(VD->getLocation(),
440          IsDecl ? diag::note_previous_decl : diag::note_defined_here) << VD;
441     return ExprError();
442   }
443
444   VarDecl *CanonicalVD = VD->getCanonicalDecl();
445   NamedDecl *ND = cast<NamedDecl>(CanonicalVD);
446   // OpenMP [2.9.2, Restrictions, C/C++, p.2]
447   //   A threadprivate directive for file-scope variables must appear outside
448   //   any definition or declaration.
449   if (CanonicalVD->getDeclContext()->isTranslationUnit() &&
450       !getCurLexicalContext()->isTranslationUnit()) {
451     Diag(Id.getLoc(), diag::err_omp_var_scope)
452       << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
453     bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
454                   VarDecl::DeclarationOnly;
455     Diag(VD->getLocation(), IsDecl ? diag::note_previous_decl :
456                                      diag::note_defined_here) << VD;
457     return ExprError();
458   }
459   // OpenMP [2.9.2, Restrictions, C/C++, p.3]
460   //   A threadprivate directive for static class member variables must appear
461   //   in the class definition, in the same scope in which the member
462   //   variables are declared.
463   if (CanonicalVD->isStaticDataMember() &&
464       !CanonicalVD->getDeclContext()->Equals(getCurLexicalContext())) {
465     Diag(Id.getLoc(), diag::err_omp_var_scope)
466       << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
467     bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
468                   VarDecl::DeclarationOnly;
469     Diag(VD->getLocation(), IsDecl ? diag::note_previous_decl :
470                                      diag::note_defined_here) << VD;
471     return ExprError();
472   }
473   // OpenMP [2.9.2, Restrictions, C/C++, p.4]
474   //   A threadprivate directive for namespace-scope variables must appear
475   //   outside any definition or declaration other than the namespace
476   //   definition itself.
477   if (CanonicalVD->getDeclContext()->isNamespace() &&
478       (!getCurLexicalContext()->isFileContext() ||
479        !getCurLexicalContext()->Encloses(CanonicalVD->getDeclContext()))) {
480     Diag(Id.getLoc(), diag::err_omp_var_scope)
481       << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
482     bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
483                   VarDecl::DeclarationOnly;
484     Diag(VD->getLocation(), IsDecl ? diag::note_previous_decl :
485                                      diag::note_defined_here) << VD;
486     return ExprError();
487   }
488   // OpenMP [2.9.2, Restrictions, C/C++, p.6]
489   //   A threadprivate directive for static block-scope variables must appear
490   //   in the scope of the variable and not in a nested scope.
491   if (CanonicalVD->isStaticLocal() && CurScope &&
492       !isDeclInScope(ND, getCurLexicalContext(), CurScope)) {
493     Diag(Id.getLoc(), diag::err_omp_var_scope)
494       << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
495     bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
496                   VarDecl::DeclarationOnly;
497     Diag(VD->getLocation(), IsDecl ? diag::note_previous_decl :
498                                      diag::note_defined_here) << VD;
499     return ExprError();
500   }
501
502   // OpenMP [2.9.2, Restrictions, C/C++, p.2-6]
503   //   A threadprivate directive must lexically precede all references to any
504   //   of the variables in its list.
505   if (VD->isUsed()) {
506     Diag(Id.getLoc(), diag::err_omp_var_used)
507       << getOpenMPDirectiveName(OMPD_threadprivate) << VD;
508     return ExprError();
509   }
510
511   QualType ExprType = VD->getType().getNonReferenceType();
512   ExprResult DE = BuildDeclRefExpr(VD, ExprType, VK_RValue, Id.getLoc());
513   DSAStack->addDSA(VD, cast<DeclRefExpr>(DE.get()), OMPC_threadprivate);
514   return DE;
515 }
516
517 Sema::DeclGroupPtrTy Sema::ActOnOpenMPThreadprivateDirective(
518                                 SourceLocation Loc,
519                                 ArrayRef<Expr *> VarList) {
520   if (OMPThreadPrivateDecl *D = CheckOMPThreadPrivateDecl(Loc, VarList)) {
521     CurContext->addDecl(D);
522     return DeclGroupPtrTy::make(DeclGroupRef(D));
523   }
524   return DeclGroupPtrTy();
525 }
526
527 OMPThreadPrivateDecl *Sema::CheckOMPThreadPrivateDecl(
528                                  SourceLocation Loc,
529                                  ArrayRef<Expr *> VarList) {
530   SmallVector<Expr *, 8> Vars;
531   for (ArrayRef<Expr *>::iterator I = VarList.begin(),
532                                          E = VarList.end();
533        I != E; ++I) {
534     DeclRefExpr *DE = cast<DeclRefExpr>(*I);
535     VarDecl *VD = cast<VarDecl>(DE->getDecl());
536     SourceLocation ILoc = DE->getExprLoc();
537
538     // OpenMP [2.9.2, Restrictions, C/C++, p.10]
539     //   A threadprivate variable must not have an incomplete type.
540     if (RequireCompleteType(ILoc, VD->getType(),
541                             diag::err_omp_threadprivate_incomplete_type)) {
542       continue;
543     }
544
545     // OpenMP [2.9.2, Restrictions, C/C++, p.10]
546     //   A threadprivate variable must not have a reference type.
547     if (VD->getType()->isReferenceType()) {
548       Diag(ILoc, diag::err_omp_ref_type_arg)
549         << getOpenMPDirectiveName(OMPD_threadprivate)
550         << VD->getType();
551       bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
552                     VarDecl::DeclarationOnly;
553       Diag(VD->getLocation(), IsDecl ? diag::note_previous_decl :
554                                        diag::note_defined_here) << VD;
555       continue;
556     }
557
558     // Check if this is a TLS variable.
559     if (VD->getTLSKind()) {
560       Diag(ILoc, diag::err_omp_var_thread_local) << VD;
561       bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
562                     VarDecl::DeclarationOnly;
563       Diag(VD->getLocation(), IsDecl ? diag::note_previous_decl :
564                                        diag::note_defined_here) << VD;
565       continue;
566     }
567
568     Vars.push_back(*I);
569   }
570   return Vars.empty() ?
571               0 : OMPThreadPrivateDecl::Create(Context,
572                                                getCurLexicalContext(),
573                                                Loc, Vars);
574 }
575
576 namespace {
577 class DSAAttrChecker : public StmtVisitor<DSAAttrChecker, void> {
578   DSAStackTy *Stack;
579   Sema &Actions;
580   bool ErrorFound;
581   CapturedStmt *CS;
582   llvm::SmallVector<Expr *, 8> ImplicitFirstprivate;
583 public:
584   void VisitDeclRefExpr(DeclRefExpr *E) {
585     if(VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) {
586       // Skip internally declared variables.
587       if (VD->isLocalVarDecl() && !CS->capturesVariable(VD)) return;
588
589       SourceLocation ELoc = E->getExprLoc();
590
591       OpenMPDirectiveKind DKind = Stack->getCurrentDirective();
592       DSAStackTy::DSAVarData DVar = Stack->getTopDSA(VD);
593       if (DVar.CKind != OMPC_unknown) {
594         if (DKind == OMPD_task && DVar.CKind != OMPC_shared &&
595             DVar.CKind != OMPC_threadprivate && !DVar.RefExpr)
596           ImplicitFirstprivate.push_back(DVar.RefExpr);
597         return;
598       }
599       // The default(none) clause requires that each variable that is referenced
600       // in the construct, and does not have a predetermined data-sharing
601       // attribute, must have its data-sharing attribute explicitly determined
602       // by being listed in a data-sharing attribute clause.
603       if (DVar.CKind == OMPC_unknown && Stack->getDefaultDSA() == DSA_none &&
604           (DKind == OMPD_parallel || DKind == OMPD_task)) {
605         ErrorFound = true;
606         Actions.Diag(ELoc, diag::err_omp_no_dsa_for_variable) << VD;
607         return;
608       }
609
610       // OpenMP [2.9.3.6, Restrictions, p.2]
611       //  A list item that appears in a reduction clause of the innermost
612       //  enclosing worksharing or parallel construct may not be accessed in an
613       //  explicit task.
614       // TODO:
615
616       // Define implicit data-sharing attributes for task.
617       DVar = Stack->getImplicitDSA(VD);
618       if (DKind == OMPD_task && DVar.CKind != OMPC_shared)
619         ImplicitFirstprivate.push_back(DVar.RefExpr);
620     }
621   }
622   void VisitOMPExecutableDirective(OMPExecutableDirective *S) {
623     for (ArrayRef<OMPClause *>::iterator I = S->clauses().begin(),
624                                          E = S->clauses().end();
625          I != E; ++I)
626       if (OMPClause *C = *I)
627         for (StmtRange R = C->children(); R; ++R)
628           if (Stmt *Child = *R)
629             Visit(Child);
630   }
631   void VisitStmt(Stmt *S) {
632     for (Stmt::child_iterator I = S->child_begin(), E = S->child_end();
633          I != E; ++I)
634       if (Stmt *Child = *I)
635         if (!isa<OMPExecutableDirective>(Child))
636           Visit(Child);
637     }
638
639   bool isErrorFound() { return ErrorFound; }
640   ArrayRef<Expr *> getImplicitFirstprivate() { return ImplicitFirstprivate; }
641
642   DSAAttrChecker(DSAStackTy *S, Sema &Actions, CapturedStmt *CS)
643     : Stack(S), Actions(Actions), ErrorFound(false), CS(CS) { }
644 };
645 }
646
647 StmtResult Sema::ActOnOpenMPExecutableDirective(OpenMPDirectiveKind Kind,
648                                                 ArrayRef<OMPClause *> Clauses,
649                                                 Stmt *AStmt,
650                                                 SourceLocation StartLoc,
651                                                 SourceLocation EndLoc) {
652   assert(AStmt && isa<CapturedStmt>(AStmt) && "Captured statement expected");
653
654   StmtResult Res = StmtError();
655
656   // Check default data sharing attributes for referenced variables.
657   DSAAttrChecker DSAChecker(DSAStack, *this, cast<CapturedStmt>(AStmt));
658   DSAChecker.Visit(cast<CapturedStmt>(AStmt)->getCapturedStmt());
659   if (DSAChecker.isErrorFound())
660     return StmtError();
661   // Generate list of implicitly defined firstprivate variables.
662   llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit;
663   ClausesWithImplicit.append(Clauses.begin(), Clauses.end());
664
665   bool ErrorFound = false;
666   if (!DSAChecker.getImplicitFirstprivate().empty()) {
667     if (OMPClause *Implicit =
668          ActOnOpenMPFirstprivateClause(DSAChecker.getImplicitFirstprivate(),
669                                        SourceLocation(), SourceLocation(),
670                                        SourceLocation())) {
671       ClausesWithImplicit.push_back(Implicit);
672       ErrorFound = cast<OMPFirstprivateClause>(Implicit)->varlist_size() !=
673                                     DSAChecker.getImplicitFirstprivate().size();
674     } else
675       ErrorFound = true;
676   }
677
678   switch (Kind) {
679   case OMPD_parallel:
680     Res = ActOnOpenMPParallelDirective(ClausesWithImplicit, AStmt,
681                                        StartLoc, EndLoc);
682     break;
683   case OMPD_threadprivate:
684   case OMPD_task:
685     llvm_unreachable("OpenMP Directive is not allowed");
686   case OMPD_unknown:
687   case NUM_OPENMP_DIRECTIVES:
688     llvm_unreachable("Unknown OpenMP directive");
689   }
690
691   if (ErrorFound) return StmtError();
692   return Res;
693 }
694
695 StmtResult Sema::ActOnOpenMPParallelDirective(ArrayRef<OMPClause *> Clauses,
696                                               Stmt *AStmt,
697                                               SourceLocation StartLoc,
698                                               SourceLocation EndLoc) {
699   getCurFunction()->setHasBranchProtectedScope();
700
701   return Owned(OMPParallelDirective::Create(Context, StartLoc, EndLoc,
702                                             Clauses, AStmt));
703 }
704
705 OMPClause *Sema::ActOnOpenMPSimpleClause(OpenMPClauseKind Kind,
706                                          unsigned Argument,
707                                          SourceLocation ArgumentLoc,
708                                          SourceLocation StartLoc,
709                                          SourceLocation LParenLoc,
710                                          SourceLocation EndLoc) {
711   OMPClause *Res = 0;
712   switch (Kind) {
713   case OMPC_default:
714     Res =
715       ActOnOpenMPDefaultClause(static_cast<OpenMPDefaultClauseKind>(Argument),
716                                ArgumentLoc, StartLoc, LParenLoc, EndLoc);
717     break;
718   case OMPC_private:
719   case OMPC_firstprivate:
720   case OMPC_shared:
721   case OMPC_threadprivate:
722   case OMPC_unknown:
723   case NUM_OPENMP_CLAUSES:
724     llvm_unreachable("Clause is not allowed.");
725   }
726   return Res;
727 }
728
729 OMPClause *Sema::ActOnOpenMPDefaultClause(OpenMPDefaultClauseKind Kind,
730                                           SourceLocation KindKwLoc,
731                                           SourceLocation StartLoc,
732                                           SourceLocation LParenLoc,
733                                           SourceLocation EndLoc) {
734   if (Kind == OMPC_DEFAULT_unknown) {
735     std::string Values;
736     std::string Sep(NUM_OPENMP_DEFAULT_KINDS > 1 ? ", " : "");
737     for (unsigned i = OMPC_DEFAULT_unknown + 1;
738          i < NUM_OPENMP_DEFAULT_KINDS; ++i) {
739       Values += "'";
740       Values += getOpenMPSimpleClauseTypeName(OMPC_default, i);
741       Values += "'";
742       switch (i) {
743       case NUM_OPENMP_DEFAULT_KINDS - 2:
744         Values += " or ";
745         break;
746       case NUM_OPENMP_DEFAULT_KINDS - 1:
747         break;
748       default:
749         Values += Sep;
750         break;
751       }
752     }
753     Diag(KindKwLoc, diag::err_omp_unexpected_clause_value)
754       << Values << getOpenMPClauseName(OMPC_default);
755     return 0;
756   }
757   switch (Kind) {
758   case OMPC_DEFAULT_none:
759     DSAStack->setDefaultDSANone();
760     break;
761   case OMPC_DEFAULT_shared:
762     DSAStack->setDefaultDSAShared();
763     break;
764   default:
765     break;
766   }
767   return new (Context) OMPDefaultClause(Kind, KindKwLoc, StartLoc, LParenLoc,
768                                         EndLoc);
769 }
770
771 OMPClause *Sema::ActOnOpenMPVarListClause(OpenMPClauseKind Kind,
772                                           ArrayRef<Expr *> VarList,
773                                           SourceLocation StartLoc,
774                                           SourceLocation LParenLoc,
775                                           SourceLocation EndLoc) {
776   OMPClause *Res = 0;
777   switch (Kind) {
778   case OMPC_private:
779     Res = ActOnOpenMPPrivateClause(VarList, StartLoc, LParenLoc, EndLoc);
780     break;
781   case OMPC_firstprivate:
782     Res = ActOnOpenMPFirstprivateClause(VarList, StartLoc, LParenLoc, EndLoc);
783     break;
784   case OMPC_shared:
785     Res = ActOnOpenMPSharedClause(VarList, StartLoc, LParenLoc, EndLoc);
786     break;
787   case OMPC_default:
788   case OMPC_threadprivate:
789   case OMPC_unknown:
790   case NUM_OPENMP_CLAUSES:
791     llvm_unreachable("Clause is not allowed.");
792   }
793   return Res;
794 }
795
796 OMPClause *Sema::ActOnOpenMPPrivateClause(ArrayRef<Expr *> VarList,
797                                           SourceLocation StartLoc,
798                                           SourceLocation LParenLoc,
799                                           SourceLocation EndLoc) {
800   SmallVector<Expr *, 8> Vars;
801   for (ArrayRef<Expr *>::iterator I = VarList.begin(), E = VarList.end();
802        I != E; ++I) {
803     assert(*I && "NULL expr in OpenMP private clause.");
804     if (isa<DependentScopeDeclRefExpr>(*I)) {
805       // It will be analyzed later.
806       Vars.push_back(*I);
807       continue;
808     }
809
810     SourceLocation ELoc = (*I)->getExprLoc();
811     // OpenMP [2.1, C/C++]
812     //  A list item is a variable name.
813     // OpenMP  [2.9.3.3, Restrictions, p.1]
814     //  A variable that is part of another variable (as an array or
815     //  structure element) cannot appear in a private clause.
816     DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(*I);
817     if (!DE || !isa<VarDecl>(DE->getDecl())) {
818       Diag(ELoc, diag::err_omp_expected_var_name)
819         << (*I)->getSourceRange();
820       continue;
821     }
822     Decl *D = DE->getDecl();
823     VarDecl *VD = cast<VarDecl>(D);
824
825     QualType Type = VD->getType();
826     if (Type->isDependentType() || Type->isInstantiationDependentType()) {
827       // It will be analyzed later.
828       Vars.push_back(DE);
829       continue;
830     }
831
832     // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
833     //  A variable that appears in a private clause must not have an incomplete
834     //  type or a reference type.
835     if (RequireCompleteType(ELoc, Type,
836                             diag::err_omp_private_incomplete_type)) {
837       continue;
838     }
839     if (Type->isReferenceType()) {
840       Diag(ELoc, diag::err_omp_clause_ref_type_arg)
841         << getOpenMPClauseName(OMPC_private) << Type;
842       bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
843                     VarDecl::DeclarationOnly;
844       Diag(VD->getLocation(), IsDecl ? diag::note_previous_decl :
845                                        diag::note_defined_here) << VD;
846       continue;
847     }
848
849     // OpenMP [2.9.3.3, Restrictions, C/C++, p.1]
850     //  A variable of class type (or array thereof) that appears in a private
851     //  clause requires an accesible, unambiguous default constructor for the
852     //  class type.
853     while (Type.getNonReferenceType()->isArrayType()) {
854       Type = cast<ArrayType>(
855                  Type.getNonReferenceType().getTypePtr())->getElementType();
856     }
857     CXXRecordDecl *RD = getLangOpts().CPlusPlus ?
858                           Type.getNonReferenceType()->getAsCXXRecordDecl() : 0;
859     if (RD) {
860       CXXConstructorDecl *CD = LookupDefaultConstructor(RD);
861       PartialDiagnostic PD =
862         PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
863       if (!CD ||
864           CheckConstructorAccess(ELoc, CD,
865                                  InitializedEntity::InitializeTemporary(Type),
866                                  CD->getAccess(), PD) == AR_inaccessible ||
867           CD->isDeleted()) {
868         Diag(ELoc, diag::err_omp_required_method)
869              << getOpenMPClauseName(OMPC_private) << 0;
870         bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
871                       VarDecl::DeclarationOnly;
872         Diag(VD->getLocation(), IsDecl ? diag::note_previous_decl :
873                                          diag::note_defined_here) << VD;
874         Diag(RD->getLocation(), diag::note_previous_decl) << RD;
875         continue;
876       }
877       MarkFunctionReferenced(ELoc, CD);
878       DiagnoseUseOfDecl(CD, ELoc);
879
880       CXXDestructorDecl *DD = RD->getDestructor();
881       if (DD) {
882         if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible ||
883             DD->isDeleted()) {
884           Diag(ELoc, diag::err_omp_required_method)
885                << getOpenMPClauseName(OMPC_private) << 4;
886           bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
887                         VarDecl::DeclarationOnly;
888           Diag(VD->getLocation(), IsDecl ? diag::note_previous_decl :
889                                            diag::note_defined_here) << VD;
890           Diag(RD->getLocation(), diag::note_previous_decl) << RD;
891           continue;
892         }
893         MarkFunctionReferenced(ELoc, DD);
894         DiagnoseUseOfDecl(DD, ELoc);
895       }
896     }
897
898     // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
899     // in a Construct]
900     //  Variables with the predetermined data-sharing attributes may not be
901     //  listed in data-sharing attributes clauses, except for the cases
902     //  listed below. For these exceptions only, listing a predetermined
903     //  variable in a data-sharing attribute clause is allowed and overrides
904     //  the variable's predetermined data-sharing attributes.
905     DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD);
906     if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_private) {
907       Diag(ELoc, diag::err_omp_wrong_dsa)
908          << getOpenMPClauseName(DVar.CKind)
909          << getOpenMPClauseName(OMPC_private);
910       if (DVar.RefExpr) {
911         Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
912              << getOpenMPClauseName(DVar.CKind);
913       } else {
914         Diag(VD->getLocation(), diag::note_omp_predetermined_dsa)
915              << getOpenMPClauseName(DVar.CKind);
916       }
917       continue;
918     }
919
920     DSAStack->addDSA(VD, DE, OMPC_private);
921     Vars.push_back(DE);
922   }
923
924   if (Vars.empty()) return 0;
925
926   return OMPPrivateClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
927 }
928
929 OMPClause *Sema::ActOnOpenMPFirstprivateClause(ArrayRef<Expr *> VarList,
930                                                SourceLocation StartLoc,
931                                                SourceLocation LParenLoc,
932                                                SourceLocation EndLoc) {
933   SmallVector<Expr *, 8> Vars;
934   for (ArrayRef<Expr *>::iterator I = VarList.begin(), E = VarList.end();
935        I != E; ++I) {
936     assert(*I && "NULL expr in OpenMP firstprivate clause.");
937     if (isa<DependentScopeDeclRefExpr>(*I)) {
938       // It will be analyzed later.
939       Vars.push_back(*I);
940       continue;
941     }
942
943     SourceLocation ELoc = (*I)->getExprLoc();
944     // OpenMP [2.1, C/C++]
945     //  A list item is a variable name.
946     // OpenMP  [2.9.3.3, Restrictions, p.1]
947     //  A variable that is part of another variable (as an array or
948     //  structure element) cannot appear in a private clause.
949     DeclRefExpr *DE = dyn_cast_or_null<DeclRefExpr>(*I);
950     if (!DE || !isa<VarDecl>(DE->getDecl())) {
951       Diag(ELoc, diag::err_omp_expected_var_name)
952         << (*I)->getSourceRange();
953       continue;
954     }
955     Decl *D = DE->getDecl();
956     VarDecl *VD = cast<VarDecl>(D);
957
958     QualType Type = VD->getType();
959     if (Type->isDependentType() || Type->isInstantiationDependentType()) {
960       // It will be analyzed later.
961       Vars.push_back(DE);
962       continue;
963     }
964
965     // OpenMP [2.9.3.3, Restrictions, C/C++, p.3]
966     //  A variable that appears in a private clause must not have an incomplete
967     //  type or a reference type.
968     if (RequireCompleteType(ELoc, Type,
969                             diag::err_omp_firstprivate_incomplete_type)) {
970       continue;
971     }
972     if (Type->isReferenceType()) {
973       Diag(ELoc, diag::err_omp_clause_ref_type_arg)
974         << getOpenMPClauseName(OMPC_firstprivate) << Type;
975       bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
976                     VarDecl::DeclarationOnly;
977       Diag(VD->getLocation(), IsDecl ? diag::note_previous_decl :
978                                        diag::note_defined_here) << VD;
979       continue;
980     }
981
982     // OpenMP [2.9.3.4, Restrictions, C/C++, p.1]
983     //  A variable of class type (or array thereof) that appears in a private
984     //  clause requires an accesible, unambiguous copy constructor for the
985     //  class type.
986     Type = Context.getBaseElementType(Type);
987     CXXRecordDecl *RD = getLangOpts().CPlusPlus ?
988                           Type.getNonReferenceType()->getAsCXXRecordDecl() : 0;
989     if (RD) {
990       CXXConstructorDecl *CD = LookupCopyingConstructor(RD, 0);
991       PartialDiagnostic PD =
992         PartialDiagnostic(PartialDiagnostic::NullDiagnostic());
993       if (!CD ||
994           CheckConstructorAccess(ELoc, CD,
995                                  InitializedEntity::InitializeTemporary(Type),
996                                  CD->getAccess(), PD) == AR_inaccessible ||
997           CD->isDeleted()) {
998         Diag(ELoc, diag::err_omp_required_method)
999              << getOpenMPClauseName(OMPC_firstprivate) << 1;
1000         bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
1001                       VarDecl::DeclarationOnly;
1002         Diag(VD->getLocation(), IsDecl ? diag::note_previous_decl :
1003                                          diag::note_defined_here) << VD;
1004         Diag(RD->getLocation(), diag::note_previous_decl) << RD;
1005         continue;
1006       }
1007       MarkFunctionReferenced(ELoc, CD);
1008       DiagnoseUseOfDecl(CD, ELoc);
1009
1010       CXXDestructorDecl *DD = RD->getDestructor();
1011       if (DD) {
1012         if (CheckDestructorAccess(ELoc, DD, PD) == AR_inaccessible ||
1013             DD->isDeleted()) {
1014           Diag(ELoc, diag::err_omp_required_method)
1015                << getOpenMPClauseName(OMPC_firstprivate) << 4;
1016           bool IsDecl = VD->isThisDeclarationADefinition(Context) ==
1017                         VarDecl::DeclarationOnly;
1018           Diag(VD->getLocation(), IsDecl ? diag::note_previous_decl :
1019                                            diag::note_defined_here) << VD;
1020           Diag(RD->getLocation(), diag::note_previous_decl) << RD;
1021           continue;
1022         }
1023         MarkFunctionReferenced(ELoc, DD);
1024         DiagnoseUseOfDecl(DD, ELoc);
1025       }
1026     }
1027
1028     // If StartLoc and EndLoc are invalid - this is an implicit firstprivate
1029     // variable and it was checked already.
1030     if (StartLoc.isValid() && EndLoc.isValid()) {
1031       DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD);
1032       Type = Type.getNonReferenceType().getCanonicalType();
1033       bool IsConstant = Type.isConstant(Context);
1034       Type = Context.getBaseElementType(Type);
1035       // OpenMP [2.4.13, Data-sharing Attribute Clauses]
1036       //  A list item that specifies a given variable may not appear in more
1037       // than one clause on the same directive, except that a variable may be
1038       //  specified in both firstprivate and lastprivate clauses.
1039       //  TODO: add processing for lastprivate.
1040       if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_firstprivate &&
1041           DVar.RefExpr) {
1042         Diag(ELoc, diag::err_omp_wrong_dsa)
1043            << getOpenMPClauseName(DVar.CKind)
1044            << getOpenMPClauseName(OMPC_firstprivate);
1045         Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
1046            << getOpenMPClauseName(DVar.CKind);
1047         continue;
1048       }
1049
1050       // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1051       // in a Construct]
1052       //  Variables with the predetermined data-sharing attributes may not be
1053       //  listed in data-sharing attributes clauses, except for the cases
1054       //  listed below. For these exceptions only, listing a predetermined
1055       //  variable in a data-sharing attribute clause is allowed and overrides
1056       //  the variable's predetermined data-sharing attributes.
1057       // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1058       // in a Construct, C/C++, p.2]
1059       //  Variables with const-qualified type having no mutable member may be
1060       //  listed in a firstprivate clause, even if they are static data members.
1061       if (!(IsConstant || VD->isStaticDataMember()) && !DVar.RefExpr &&
1062           DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared) {
1063         Diag(ELoc, diag::err_omp_wrong_dsa)
1064            << getOpenMPClauseName(DVar.CKind)
1065            << getOpenMPClauseName(OMPC_firstprivate);
1066         Diag(VD->getLocation(), diag::note_omp_predetermined_dsa)
1067            << getOpenMPClauseName(DVar.CKind);
1068         continue;
1069       }
1070
1071       // OpenMP [2.9.3.4, Restrictions, p.2]
1072       //  A list item that is private within a parallel region must not appear
1073       //  in a firstprivate clause on a worksharing construct if any of the
1074       //  worksharing regions arising from the worksharing construct ever bind
1075       //  to any of the parallel regions arising from the parallel construct.
1076       // OpenMP [2.9.3.4, Restrictions, p.3]
1077       //  A list item that appears in a reduction clause of a parallel construct
1078       //  must not appear in a firstprivate clause on a worksharing or task
1079       //  construct if any of the worksharing or task regions arising from the
1080       //  worksharing or task construct ever bind to any of the parallel regions
1081       //  arising from the parallel construct.
1082       // OpenMP [2.9.3.4, Restrictions, p.4]
1083       //  A list item that appears in a reduction clause in worksharing
1084       //  construct must not appear in a firstprivate clause in a task construct
1085       //  encountered during execution of any of the worksharing regions arising
1086       //  from the worksharing construct.
1087       // TODO:
1088     }
1089
1090     DSAStack->addDSA(VD, DE, OMPC_firstprivate);
1091     Vars.push_back(DE);
1092   }
1093
1094   if (Vars.empty()) return 0;
1095
1096   return OMPFirstprivateClause::Create(Context, StartLoc, LParenLoc, EndLoc,
1097                                        Vars);
1098 }
1099
1100 OMPClause *Sema::ActOnOpenMPSharedClause(ArrayRef<Expr *> VarList,
1101                                          SourceLocation StartLoc,
1102                                          SourceLocation LParenLoc,
1103                                          SourceLocation EndLoc) {
1104   SmallVector<Expr *, 8> Vars;
1105   for (ArrayRef<Expr *>::iterator I = VarList.begin(), E = VarList.end();
1106        I != E; ++I) {
1107     assert(*I && "NULL expr in OpenMP shared clause.");
1108     if (isa<DependentScopeDeclRefExpr>(*I)) {
1109       // It will be analyzed later.
1110       Vars.push_back(*I);
1111       continue;
1112     }
1113
1114     SourceLocation ELoc = (*I)->getExprLoc();
1115     // OpenMP [2.1, C/C++]
1116     //  A list item is a variable name.
1117     // OpenMP  [2.9.3.4, Restrictions, p.1]
1118     //  A variable that is part of another variable (as an array or
1119     //  structure element) cannot appear in a private clause.
1120     DeclRefExpr *DE = dyn_cast<DeclRefExpr>(*I);
1121     if (!DE || !isa<VarDecl>(DE->getDecl())) {
1122       Diag(ELoc, diag::err_omp_expected_var_name)
1123         << (*I)->getSourceRange();
1124       continue;
1125     }
1126     Decl *D = DE->getDecl();
1127     VarDecl *VD = cast<VarDecl>(D);
1128
1129     QualType Type = VD->getType();
1130     if (Type->isDependentType() || Type->isInstantiationDependentType()) {
1131       // It will be analyzed later.
1132       Vars.push_back(DE);
1133       continue;
1134     }
1135
1136     // OpenMP [2.9.1.1, Data-sharing Attribute Rules for Variables Referenced
1137     // in a Construct]
1138     //  Variables with the predetermined data-sharing attributes may not be
1139     //  listed in data-sharing attributes clauses, except for the cases
1140     //  listed below. For these exceptions only, listing a predetermined
1141     //  variable in a data-sharing attribute clause is allowed and overrides
1142     //  the variable's predetermined data-sharing attributes.
1143     DSAStackTy::DSAVarData DVar = DSAStack->getTopDSA(VD);
1144     if (DVar.CKind != OMPC_unknown && DVar.CKind != OMPC_shared && DVar.RefExpr) {
1145       Diag(ELoc, diag::err_omp_wrong_dsa)
1146          << getOpenMPClauseName(DVar.CKind)
1147          << getOpenMPClauseName(OMPC_shared);
1148       Diag(DVar.RefExpr->getExprLoc(), diag::note_omp_explicit_dsa)
1149            << getOpenMPClauseName(DVar.CKind);
1150       continue;
1151     }
1152
1153     DSAStack->addDSA(VD, DE, OMPC_shared);
1154     Vars.push_back(DE);
1155   }
1156
1157   if (Vars.empty()) return 0;
1158
1159   return OMPSharedClause::Create(Context, StartLoc, LParenLoc, EndLoc, Vars);
1160 }
1161
1162 #undef DSAStack