-
Notifications
You must be signed in to change notification settings - Fork 0
/
ProgramVar.cpp
119 lines (104 loc) · 4.05 KB
/
ProgramVar.cpp
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
//=--ProgramVar.cpp-----------------------------------------------*- C++-*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This file contains implementation of methods in ProgramVar.h
//
//===----------------------------------------------------------------------===//
#include "clang/3C/ProgramVar.h"
GlobalScope *GlobalScope::ProgScope = nullptr;
std::set<StructScope, PVSComp> StructScope::AllStScopes;
std::set<CtxStructScope, PVSComp> CtxStructScope::AllCtxStScopes;
std::set<FunctionParamScope, PVSComp> FunctionParamScope::AllFnParamScopes;
std::set<FunctionScope, PVSComp> FunctionScope::AllFnScopes;
std::set<CtxFunctionArgScope, PVSComp> CtxFunctionArgScope::AllCtxFnArgScopes;
GlobalScope *GlobalScope::getGlobalScope() {
if (ProgScope == nullptr) {
ProgScope = new GlobalScope();
}
return ProgScope;
}
const StructScope *StructScope::getStructScope(std::string StName) {
StructScope TmpS(StName);
if (AllStScopes.find(TmpS) == AllStScopes.end()) {
AllStScopes.insert(TmpS);
}
const auto &SS = *AllStScopes.find(TmpS);
return &SS;
}
const CtxStructScope *CtxStructScope::getCtxStructScope(const StructScope *SS,
std::string AS,
bool IsGlobal) {
CtxStructScope TmpCSS(SS->getSName(), AS, IsGlobal);
if (AllCtxStScopes.find(TmpCSS) == AllCtxStScopes.end()) {
AllCtxStScopes.insert(TmpCSS);
}
const auto &CSS = *AllCtxStScopes.find(TmpCSS);
return &CSS;
}
const FunctionParamScope *
FunctionParamScope::getFunctionParamScope(std::string FnName, bool IsSt) {
FunctionParamScope TmpFPS(FnName, IsSt);
if (AllFnParamScopes.find(TmpFPS) == AllFnParamScopes.end()) {
AllFnParamScopes.insert(TmpFPS);
}
const auto &FPS = *AllFnParamScopes.find(TmpFPS);
return &FPS;
}
bool FunctionScope::isInInnerScope(const ProgramVarScope &O) const {
// Global variables and function parameters are visible here.
if (clang::isa<GlobalScope>(&O))
return true;
// Function parameters of the same function are also visible
// inside the function.
if (auto *FPS = clang::dyn_cast<FunctionParamScope>(&O)) {
if (this->FName == FPS->getFName() &&
this->IsStatic == FPS->getIsStatic()) {
return true;
}
}
return false;
}
const CtxFunctionArgScope *
CtxFunctionArgScope::getCtxFunctionParamScope(const FunctionParamScope *FPS,
const PersistentSourceLoc &PSL) {
CtxFunctionArgScope TmpAS(std::string(FPS->getFName()), FPS->getIsStatic(),
PSL);
if (AllCtxFnArgScopes.find(TmpAS) == AllCtxFnArgScopes.end()) {
AllCtxFnArgScopes.insert(TmpAS);
}
const auto &CFAS = *AllCtxFnArgScopes.find(TmpAS);
return &CFAS;
}
const FunctionScope *FunctionScope::getFunctionScope(std::string FnName,
bool IsSt) {
FunctionScope TmpFS(FnName, IsSt);
if (AllFnScopes.find(TmpFS) == AllFnScopes.end()) {
AllFnScopes.insert(TmpFS);
}
const auto &FS = *AllFnScopes.find(TmpFS);
return &FS;
}
std::set<const ProgramVar *> ProgramVar::AllProgramVars;
std::string ProgramVar::verboseStr() const {
std::string Ret = std::to_string(K) + "_";
if (IsConstant)
Ret += "Cons:";
return Ret + VarName + "(" + VScope->getStr() + ")";
}
ProgramVar *ProgramVar::makeCopy(BoundsKey NK) const {
return new ProgramVar(NK, this->VarName, this->VScope, this->IsConstant,
this->ConstantVal);
}
ProgramVar *ProgramVar::createNewConstantVar(BoundsKey VK,
uint64_t Value) {
return new ProgramVar(VK, Value);
}
ProgramVar *ProgramVar::createNewProgramVar(BoundsKey VK, std::string VName,
const ProgramVarScope *PVS) {
return new ProgramVar(VK, VName, PVS);
}