MCPcopy Create free account
hub / github.com/Samsung/UTopia / getValueAsReal

Method getValueAsReal

lib/constantanalysis/ASTValue.cpp:169–208  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

167}
168
169std::string ASTValue::getValueAsReal(const Expr &E,
170 const ASTContext &Ctx) const {
171
172 Expr::EvalResult Result;
173 if (!E.EvaluateAsRValue(Result, Ctx)) {
174
175 auto *RE = E.IgnoreCasts();
176 if (!RE || !isa<DeclRefExpr>(RE))
177 return "";
178
179 auto &DRE = *dyn_cast<DeclRefExpr>(RE);
180 RE = getValueExpr(DRE, Ctx);
181 if (!RE || !RE->EvaluateAsRValue(Result, Ctx)) {
182 return "";
183 }
184 }
185
186 auto T = E.getType();
187 assert(T.getTypePtrOrNull() && "Unexpected Program State");
188
189 auto &Val = Result.Val;
190 auto Value = Val.getAsString(Ctx, T);
191
192 if (T->isFloatingType()) {
193 try {
194 Value = std::to_string(std::stod(Value));
195 }
196 // TODO: Try to handle this exception
197 catch (std::exception &E) {
198 return "";
199 }
200 } else if (T->isBooleanType()) {
201 if (Value == "true")
202 Value = "1";
203 if (Value == "false")
204 Value = "0";
205 }
206
207 return Value;
208}
209
210std::string ASTValue::getValueAsString(const Expr &E,
211 const ASTContext &Ctx) const {

Callers

nothing calls this directly

Calls 2

getTypeMethod · 0.45
getAsStringMethod · 0.45

Tested by

no test coverage detected