Skip to content

src/DeclResolver.h

This file implements the class DeclResolver which is used to record and order types and functions in the input code that are required by the target regions.

Namespaces

Name
clang

Classes

Name
struct DeclInfo
Records information to resolve a single declaration, including if its declared in a system header and other declaration that this declaration depends on.
class DeclResolver
Records, orders and finds the dependencies of Decls (TypeDecls or FunctionDecls)
class TypeDeclResolver
Implements DeclResolver for types (typedefs, structs enums) used in target regions.
class FunctionDeclResolver
Implements DeclResolver for functions used in target regions.

Types

Name
using std::map< clang::Decl *, DeclInfo > DeclMap

Functions

Name
llvm::Optional< std::string > getSystemHeaderForDecl(const clang::Decl * D)

Types Documentation

using DeclMap

1
using DeclMap =  std::map<clang::Decl *, DeclInfo>;

Functions Documentation

function getSystemHeaderForDecl

1
2
3
llvm::Optional< std::string > getSystemHeaderForDecl(
    const clang::Decl * D
)

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
//===-- sotoc/src/DeclResolver.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 <map>
#include <set>
#include <stack>
#include <unordered_set>

llvm::Optional<std::string> getSystemHeaderForDecl(const clang::Decl *D);

namespace clang {
class Decl;
}

class TargetCode;

struct DeclInfo {
  const clang::Decl *Decl;
  std::set<clang::Decl *> DeclDependencies;
  // std::set<clang::Decl *> ForwardDecls;  // for the moment we solve this
  // differently
  bool IsFromSystemHeader;

  DeclInfo(clang::Decl *D, bool isFromSysHeader)
      : Decl(D), IsFromSystemHeader(isFromSysHeader){};
};

using DeclMap = std::map<clang::Decl *, DeclInfo>;

class DeclResolver {
  DeclMap AllDecls;
  std::set<clang::Decl *> NonDependentDecls;
  std::set<std::string> RequiredSystemHeaders;

public:
  virtual ~DeclResolver() = 0;
  void addDecl(clang::Decl *D);
  void orderAndAddFragments(TargetCode &TC);

protected:
  virtual void runOwnVisitor(clang::Decl *D,
                             std::function<void(clang::Decl *Dep)> Fn) = 0;
  virtual void
  findDependDecls(clang::Decl *D,
                  std::unordered_set<clang::Decl *> &UnresolvedDecls);

private:
  void topoSort(std::stack<clang::Decl *> &q);
  void topoSortUtil(std::stack<clang::Decl *> &q,
                    std::map<clang::Decl *, bool> &visited, clang::Decl *D);
};

class TypeDeclResolver : public DeclResolver {
private:
  void runOwnVisitor(clang::Decl *D,
                     std::function<void(clang::Decl *Dep)> Fn) override;
};

class FunctionDeclResolver : public DeclResolver {
  TypeDeclResolver &Types;

public:
  FunctionDeclResolver(TypeDeclResolver &Types) : Types(Types){};

private:
  void runOwnVisitor(clang::Decl *D,
                     std::function<void(clang::Decl *Dep)> Fn) override;
  void
  findDependDecls(clang::Decl *D,
                  std::unordered_set<clang::Decl *> &UnresolvedDecls) override;
};

Last update: 2021-11-24
Back to top