-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathcpp_var_decl.h
More file actions
142 lines (115 loc) · 3.2 KB
/
Copy pathcpp_var_decl.h
File metadata and controls
142 lines (115 loc) · 3.2 KB
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
// Copyright (C) 2022 Satya Das and CppParser contributors
// SPDX-License-Identifier: MIT
#ifndef FD128B15_4F2F_4742_A4B5_D3C91033195C
#define FD128B15_4F2F_4742_A4B5_D3C91033195C
#include "cppast/cpp_entity.h"
#include <cassert>
#include <variant>
#include <vector>
namespace cppast {
enum class CppVarInitializeType
{
USING_EQUAL,
DIRECT_CONSTRUCTOR_CALL
};
class CppExpression;
using CppCallArgs = std::vector<std::unique_ptr<CppExpression>>;
using CppArraySizes = CppCallArgs;
enum class CppConstructorCallStyle : bool
{
USING_PARENTHESES,
USING_BRACES
};
struct CppConstructorCallInfo
{
CppCallArgs args;
CppConstructorCallStyle style;
};
using CppVarInitInfo = std::variant< // Variable can be initialized using different ways.
std::unique_ptr<CppExpression>, // Variable is initialized using '='.
CppConstructorCallInfo // Variable is initialized using direct constructor call.
>;
class CppVarDecl
{
public:
CppVarDecl(std::string name)
: name_(std::move(name))
{
}
CppVarDecl(std::string name, CppVarInitInfo initInfo)
: name_(std::move(name))
, initInfo_(std::move(initInfo))
{
}
CppVarDecl(std::string name, std::unique_ptr<CppExpression> assignExpr)
: CppVarDecl(std::move(name), CppVarInitInfo(std::move(assignExpr)))
{
}
CppVarDecl(std::string name, CppCallArgs constructorParams, CppConstructorCallStyle style)
: CppVarDecl(std::move(name), CppConstructorCallInfo {std::move(constructorParams), style})
{
}
void initialize(CppVarInitInfo initInfo)
{
initInfo_ = std::move(initInfo);
}
const std::string& name() const
{
return name_;
}
bool isInitialized() const
{
return initInfo_.has_value();
}
std::optional<CppVarInitializeType> initializeType() const
{
if (!isInitialized())
return std::nullopt;
if (initInfo_.value().index() == 0)
return CppVarInitializeType::USING_EQUAL;
return CppVarInitializeType::DIRECT_CONSTRUCTOR_CALL;
}
/**
* @brief Gets the value assigned to variable.
*
* @return value assigned to variable.
* @note The returned value can be nullptr.
*/
const CppExpression* assignValue() const
{
if (initializeType() != CppVarInitializeType::USING_EQUAL)
return nullptr;
return std::get<std::unique_ptr<CppExpression>>(initInfo_.value()).get();
}
CppConstructorCallStyle directConstructorCallStyle() const
{
return std::get<CppConstructorCallInfo>(initInfo_.value()).style;
}
const CppCallArgs& constructorCallArgs() const
{
return std::get<CppConstructorCallInfo>(initInfo_.value()).args;
}
const CppExpression* bitField() const
{
return bitField_.get();
}
void bitField(std::unique_ptr<CppExpression> bitFieldArg)
{
bitField_ = std::move(bitFieldArg);
}
const CppArraySizes& arraySizes() const
{
return arraySizes_;
}
void addArraySize(CppExpression* arraySize)
{
arraySizes_.emplace_back(arraySize);
}
private:
std::string name_;
std::optional<CppVarInitInfo> initInfo_;
std::unique_ptr<CppExpression> bitField_;
CppArraySizes arraySizes_;
};
} // namespace cppast
#endif /* FD128B15_4F2F_4742_A4B5_D3C91033195C */