1 //===--- DeclOpenMP.cpp - Declaration OpenMP AST Node Implementation ------===//
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 /// This file implements OMPThreadPrivateDecl, OMPCapturedExprDecl
11 /// classes.
12 ///
13 //===----------------------------------------------------------------------===//
14 
15 #include "clang/AST/ASTContext.h"
16 #include "clang/AST/Decl.h"
17 #include "clang/AST/DeclBase.h"
18 #include "clang/AST/DeclOpenMP.h"
19 #include "clang/AST/Expr.h"
20 
21 using namespace clang;
22 
23 //===----------------------------------------------------------------------===//
24 // OMPThreadPrivateDecl Implementation.
25 //===----------------------------------------------------------------------===//
26 
27 void OMPThreadPrivateDecl::anchor() { }
28 
29 OMPThreadPrivateDecl *OMPThreadPrivateDecl::Create(ASTContext &C,
30                                                    DeclContext *DC,
31                                                    SourceLocation L,
32                                                    ArrayRef<Expr *> VL) {
33   OMPThreadPrivateDecl *D =
34       new (C, DC, additionalSizeToAlloc<Expr *>(VL.size()))
35           OMPThreadPrivateDecl(OMPThreadPrivate, DC, L);
36   D->NumVars = VL.size();
37   D->setVars(VL);
38   return D;
39 }
40 
41 OMPThreadPrivateDecl *OMPThreadPrivateDecl::CreateDeserialized(ASTContext &C,
42                                                                unsigned ID,
43                                                                unsigned N) {
44   OMPThreadPrivateDecl *D = new (C, ID, additionalSizeToAlloc<Expr *>(N))
45       OMPThreadPrivateDecl(OMPThreadPrivate, nullptr, SourceLocation());
46   D->NumVars = N;
47   return D;
48 }
49 
50 void OMPThreadPrivateDecl::setVars(ArrayRef<Expr *> VL) {
51   assert(VL.size() == NumVars &&
52          "Number of variables is not the same as the preallocated buffer");
53   std::uninitialized_copy(VL.begin(), VL.end(), getTrailingObjects<Expr *>());
54 }
55 
56 //===----------------------------------------------------------------------===//
57 // OMPDeclareReductionDecl Implementation.
58 //===----------------------------------------------------------------------===//
59 
60 OMPDeclareReductionDecl::OMPDeclareReductionDecl(
61     Kind DK, DeclContext *DC, SourceLocation L, DeclarationName Name,
62     QualType Ty, OMPDeclareReductionDecl *PrevDeclInScope)
63     : ValueDecl(DK, DC, L, Name, Ty), DeclContext(DK), Combiner(nullptr),
64       PrevDeclInScope(PrevDeclInScope) {
65   setInitializer(nullptr, CallInit);
66 }
67 
68 void OMPDeclareReductionDecl::anchor() {}
69 
70 OMPDeclareReductionDecl *OMPDeclareReductionDecl::Create(
71     ASTContext &C, DeclContext *DC, SourceLocation L, DeclarationName Name,
72     QualType T, OMPDeclareReductionDecl *PrevDeclInScope) {
73   return new (C, DC) OMPDeclareReductionDecl(OMPDeclareReduction, DC, L, Name,
74                                              T, PrevDeclInScope);
75 }
76 
77 OMPDeclareReductionDecl *
78 OMPDeclareReductionDecl::CreateDeserialized(ASTContext &C, unsigned ID) {
79   return new (C, ID) OMPDeclareReductionDecl(
80       OMPDeclareReduction, /*DC=*/nullptr, SourceLocation(), DeclarationName(),
81       QualType(), /*PrevDeclInScope=*/nullptr);
82 }
83 
84 OMPDeclareReductionDecl *OMPDeclareReductionDecl::getPrevDeclInScope() {
85   return cast_or_null<OMPDeclareReductionDecl>(
86       PrevDeclInScope.get(getASTContext().getExternalSource()));
87 }
88 const OMPDeclareReductionDecl *
89 OMPDeclareReductionDecl::getPrevDeclInScope() const {
90   return cast_or_null<OMPDeclareReductionDecl>(
91       PrevDeclInScope.get(getASTContext().getExternalSource()));
92 }
93 
94 //===----------------------------------------------------------------------===//
95 // OMPCapturedExprDecl Implementation.
96 //===----------------------------------------------------------------------===//
97 
98 void OMPCapturedExprDecl::anchor() {}
99 
100 OMPCapturedExprDecl *OMPCapturedExprDecl::Create(ASTContext &C, DeclContext *DC,
101                                                  IdentifierInfo *Id, QualType T,
102                                                  SourceLocation StartLoc) {
103   return new (C, DC) OMPCapturedExprDecl(
104       C, DC, Id, T, C.getTrivialTypeSourceInfo(T), StartLoc);
105 }
106 
107 OMPCapturedExprDecl *OMPCapturedExprDecl::CreateDeserialized(ASTContext &C,
108                                                              unsigned ID) {
109   return new (C, ID) OMPCapturedExprDecl(C, nullptr, nullptr, QualType(),
110                                          /*TInfo=*/nullptr, SourceLocation());
111 }
112 
113 SourceRange OMPCapturedExprDecl::getSourceRange() const {
114   assert(hasInit());
115   return SourceRange(getInit()->getBeginLoc(), getInit()->getLocEnd());
116 }
117