Commit 1813e3d5 authored by Daniel Clifford's avatar Daniel Clifford Committed by Commit Bot

[torque] Simplify handling of VisitResults

Only pass around the unadulterated value identifier in the VisitResult class
until the very last moment before code generation, at which point the
declaration that was used to originally define the value is used to generate the
correct final source code string in the context of a l-value or r-value.

Bug: v8:7793
Change-Id: Ifd0c0d245b2eb65c7f3ddb1ad4c87ee235c54a82
Reviewed-on: https://chromium-review.googlesource.com/1125063
Commit-Queue: Daniel Clifford <danno@chromium.org>
Reviewed-by: 's avatarTobias Tebbi <tebbi@chromium.org>
Cr-Commit-Position: refs/heads/master@{#54229}
parent 887dbc83
...@@ -84,11 +84,8 @@ class Value : public Declarable { ...@@ -84,11 +84,8 @@ class Value : public Declarable {
public: public:
const std::string& name() const { return name_; } const std::string& name() const { return name_; }
virtual bool IsConst() const { return true; } virtual bool IsConst() const { return true; }
virtual std::string GetValueForDeclaration() const = 0; virtual std::string value() const = 0;
virtual std::string GetValueForRead() const { virtual std::string RValue() const { return value(); }
return GetValueForDeclaration();
}
virtual std::string GetValueForWrite() const { UNREACHABLE(); }
DECLARE_DECLARABLE_BOILERPLATE(Value, value); DECLARE_DECLARABLE_BOILERPLATE(Value, value);
const Type* type() const { return type_; } const Type* type() const { return type_; }
...@@ -104,7 +101,7 @@ class Value : public Declarable { ...@@ -104,7 +101,7 @@ class Value : public Declarable {
class Parameter : public Value { class Parameter : public Value {
public: public:
DECLARE_DECLARABLE_BOILERPLATE(Parameter, parameter); DECLARE_DECLARABLE_BOILERPLATE(Parameter, parameter);
std::string GetValueForDeclaration() const override { return var_name_; } std::string value() const override { return var_name_; }
private: private:
friend class Declarations; friend class Declarations;
...@@ -119,19 +116,14 @@ class Variable : public Value { ...@@ -119,19 +116,14 @@ class Variable : public Value {
public: public:
DECLARE_DECLARABLE_BOILERPLATE(Variable, variable); DECLARE_DECLARABLE_BOILERPLATE(Variable, variable);
bool IsConst() const override { return false; } bool IsConst() const override { return false; }
std::string GetValueForDeclaration() const override { return value_; } std::string value() const override { return value_; }
std::string GetValueForRead() const override { std::string RValue() const override {
if (!IsDefined()) { if (!IsDefined()) {
ReportError("Reading uninitialized variable."); ReportError("Reading uninitialized variable.");
} }
if (type()->IsConstexpr()) { std::string result = "(*" + value() + ")";
return std::string("*") + value_; if (!type()->IsConstexpr()) result += ".value()";
} else { return result;
return value_ + "->value()";
}
}
std::string GetValueForWrite() const override {
return std::string("*") + value_;
} }
void Define() { void Define() {
if (defined_ && type()->IsConstexpr()) { if (defined_ && type()->IsConstexpr()) {
...@@ -183,7 +175,7 @@ class Label : public Declarable { ...@@ -183,7 +175,7 @@ class Label : public Declarable {
class Constant : public Value { class Constant : public Value {
public: public:
DECLARE_DECLARABLE_BOILERPLATE(Constant, constant); DECLARE_DECLARABLE_BOILERPLATE(Constant, constant);
std::string GetValueForDeclaration() const override { return value_; } std::string value() const override { return value_; }
private: private:
friend class Declarations; friend class Declarations;
......
This diff is collapsed.
...@@ -58,7 +58,7 @@ class ImplementationVisitor : public FileVisitor { ...@@ -58,7 +58,7 @@ class ImplementationVisitor : public FileVisitor {
s << "\"" << value->name() << "\" is used before it is defined"; s << "\"" << value->name() << "\" is used before it is defined";
ReportError(s.str()); ReportError(s.str());
} }
return VisitResult({value->type(), value->GetValueForRead()}); return VisitResult(value->type(), value);
} }
VisitResult GenerateFetchFromLocation(FieldAccessExpression* expr, VisitResult GenerateFetchFromLocation(FieldAccessExpression* expr,
LocationReference reference) { LocationReference reference) {
......
...@@ -248,6 +248,15 @@ bool operator<(const Type& a, const Type& b) { ...@@ -248,6 +248,15 @@ bool operator<(const Type& a, const Type& b) {
return a.MangledName() < b.MangledName(); return a.MangledName() < b.MangledName();
} }
VisitResult::VisitResult(const Type* type, const Value* declarable)
: type_(type), value_(declarable->value()), declarable_(declarable) {}
std::string VisitResult::LValue() const { return std::string("*") + value_; }
std::string VisitResult::RValue() const {
return (declarable_) ? (*declarable_)->RValue() : value_;
}
} // namespace torque } // namespace torque
} // namespace internal } // namespace internal
} // namespace v8 } // namespace v8
...@@ -33,6 +33,7 @@ static const char* const CONST_INT32_TYPE_STRING = "constexpr int32"; ...@@ -33,6 +33,7 @@ static const char* const CONST_INT32_TYPE_STRING = "constexpr int32";
static const char* const CONST_FLOAT64_TYPE_STRING = "constexpr float64"; static const char* const CONST_FLOAT64_TYPE_STRING = "constexpr float64";
class Label; class Label;
class Value;
class TypeBase { class TypeBase {
public: public:
...@@ -292,14 +293,20 @@ inline std::ostream& operator<<(std::ostream& os, const Type& t) { ...@@ -292,14 +293,20 @@ inline std::ostream& operator<<(std::ostream& os, const Type& t) {
class VisitResult { class VisitResult {
public: public:
VisitResult() {} VisitResult() {}
VisitResult(const Type* type, const std::string& variable) VisitResult(const Type* type, const std::string& value)
: type_(type), variable_(variable) {} : type_(type), value_(value), declarable_{} {}
VisitResult(const Type* type, const Value* declarable);
const Type* type() const { return type_; } const Type* type() const { return type_; }
const std::string& variable() const { return variable_; } // const std::string& variable() const { return variable_; }
base::Optional<const Value*> declarable() const { return declarable_; }
std::string value() const { return value_; }
std::string LValue() const;
std::string RValue() const;
private: private:
const Type* type_; const Type* type_;
std::string variable_; std::string value_;
base::Optional<const Value*> declarable_;
}; };
class VisitResultVector : public std::vector<VisitResult> { class VisitResultVector : public std::vector<VisitResult> {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment