Skip to content

src/Visitors.h

Namespaces

Name
clang

Classes

Name
class DiscoverTypesInDeclVisitor
Traverses (parts of) the AST to find DeclRefExpr that refer to types that need to be present for that part of the AST to compile correctly.
class DiscoverFunctionsInDeclVisitor
Traverses (parts of) the AST to find DeclRefExpr that refer to functions that need to be present for that part of the AST to compile correctly.
class FindDeclRefExprVisitor
class FindLoopStmtVisitor
class FindTargetCodeVisitor
Traverses the AST to find target and process target regions and function and variables that are annotated by an 'omp declare target' target pragma.
class FindArraySectionVisitor
class FindPrivateVariablesVisitor

Source code

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
//===-- sotoc/src/Visitor.h -----------------------------------------------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#pragma once

#include <unordered_set>

#include "DeclResolver.h"
#include "clang/AST/RecursiveASTVisitor.h"
#include "llvm/ADT/Optional.h"

namespace clang {
class Stmt;
class Decl;
class Type;
class CapturedStmt;
class OMPExecutableDirective;
class SourceLocation;
class FunctionDecl;
class Attr;
class Rewriter;
class ASTContext;
} // namespace clang

class TargetCode;
class TargetCodeFragment;
class TargetCodeRegion;

class TypeDeclResolver;

class DiscoverTypesInDeclVisitor
    : public clang::RecursiveASTVisitor<DiscoverTypesInDeclVisitor> {

  std::function<void(clang::TypeDecl *)> OnEachTypeRef;
  void processType(const clang::Type *D);

public:
  DiscoverTypesInDeclVisitor(TypeDeclResolver &Types);
  DiscoverTypesInDeclVisitor(std::function<void(clang::TypeDecl *)> F)
      : OnEachTypeRef(F){};
  bool VisitDecl(clang::Decl *D);
  bool VisitExpr(clang::Expr *D);
  bool VisitType(clang::Type *T);
};

class DiscoverFunctionsInDeclVisitor
    : public clang::RecursiveASTVisitor<DiscoverFunctionsInDeclVisitor> {

  std::function<void(clang::FunctionDecl *)> OnEachFuncRef;

public:
  DiscoverFunctionsInDeclVisitor(FunctionDeclResolver &Functions);
  DiscoverFunctionsInDeclVisitor(std::function<void(clang::FunctionDecl *)> F)
      : OnEachFuncRef(F){};

  bool VisitExpr(clang::Expr *E);
};

class FindDeclRefExprVisitor
    : public clang::RecursiveASTVisitor<FindDeclRefExprVisitor> {

  std::unordered_set<clang::VarDecl *> VarSet;

public:
  FindDeclRefExprVisitor() {}
  bool VisitStmt(clang::Stmt *S);
  // bool VisitDecl(clang::Decl *D);
  std::unordered_set<clang::VarDecl *> *getVarSet() { return &VarSet; }
};

class FindLoopStmtVisitor
    : public clang::RecursiveASTVisitor<FindLoopStmtVisitor> {

  FindDeclRefExprVisitor FindDeclRefVisitor;

public:
  FindLoopStmtVisitor() {}
  bool VisitStmt(clang::Stmt *S);
  std::unordered_set<clang::VarDecl *> *getVarSet() {
    return FindDeclRefVisitor.getVarSet();
  }
};

class FindTargetCodeVisitor
    : public clang::RecursiveASTVisitor<FindTargetCodeVisitor> {

  clang::ASTContext &Context;

  TargetCode &TargetCodeInfo;
  DiscoverTypesInDeclVisitor DiscoverTypeVisitor;
  DiscoverFunctionsInDeclVisitor DiscoverFunctionVisitor;
  FunctionDeclResolver &Functions;
  FindDeclRefExprVisitor FindDeclRefVisitor;

  std::stack<clang::FunctionDecl *> LastVisitedFuncDecl;
  std::unordered_set<std::string> FuncDeclWithoutBody;

public:
  FindTargetCodeVisitor(TargetCode &Code, TypeDeclResolver &Types,
                        FunctionDeclResolver &Functions,
                        clang::ASTContext &Context)
      : Context(Context), TargetCodeInfo(Code), DiscoverTypeVisitor(Types),
        DiscoverFunctionVisitor(Functions), Functions(Functions){};
  bool TraverseDecl(clang::Decl *D);
  bool VisitStmt(clang::Stmt *S);
  bool VisitDecl(clang::Decl *D);

private:
  bool processTargetRegion(clang::OMPExecutableDirective *TargetDirective);
  void addTargetRegionArgs(clang::CapturedStmt *S,
                           clang::OMPExecutableDirective *TargetDirective,
                           std::shared_ptr<TargetCodeRegion> TCR);
};

class FindArraySectionVisitor
    : public clang::RecursiveASTVisitor<FindArraySectionVisitor> {

  std::map<clang::VarDecl *, clang::Expr *> &LowerBoundsMap;

public:
  FindArraySectionVisitor(
      std::map<clang::VarDecl *, clang::Expr *> &LowerBoundsMap)
      : LowerBoundsMap(LowerBoundsMap) {}
  bool VisitExpr(clang::Expr *E);
};

class FindPrivateVariablesVisitor
    : public clang::RecursiveASTVisitor<FindPrivateVariablesVisitor> {

  clang::SourceManager &SM;
  clang::SourceLocation RegionTopSourceLocation;
  std::set<clang::VarDecl *> VarSet;

public:
  FindPrivateVariablesVisitor(clang::SourceLocation TopSourceLocation, clang::SourceManager &SM)
      : SM(SM), RegionTopSourceLocation(TopSourceLocation) {}

  bool VisitExpr(clang::Expr *E);
  std::set<clang::VarDecl *> &getVarSet() {
    return VarSet;
  }
};

Last update: 2021-11-24
Back to top