5 #include "src/string-constants.h" 7 #include "src/base/functional.h" 9 #include "src/objects.h" 10 #include "src/objects/string-inl.h" 15 Handle<String> StringConstantBase::AllocateStringConstant(
16 Isolate* isolate)
const {
17 if (!flattened_.is_null()) {
21 Handle<String> result;
23 case StringConstantKind::kStringLiteral: {
24 result =
static_cast<const StringLiteral*
>(
this)->str();
27 case StringConstantKind::kNumberToStringConstant: {
28 auto num_constant =
static_cast<const NumberToStringConstant*
>(
this);
29 Handle<Object> num_obj =
30 isolate->factory()->NewNumber(num_constant->num());
31 result = isolate->factory()->NumberToString(num_obj);
34 case StringConstantKind::kStringCons: {
36 static_cast<const StringCons*
>(
this)->lhs()->AllocateStringConstant(
39 static_cast<const StringCons*
>(
this)->rhs()->AllocateStringConstant(
41 result = isolate->factory()->NewConsString(lhs, rhs).ToHandleChecked();
52 bool StringConstantBase::operator==(
const StringConstantBase& other)
const {
53 if (kind() != other.kind())
return false;
56 case StringConstantKind::kStringLiteral: {
57 return static_cast<const StringLiteral*
>(
this) ==
58 static_cast<const StringLiteral*>(&other);
60 case StringConstantKind::kNumberToStringConstant: {
61 return static_cast<const NumberToStringConstant*
>(
this) ==
62 static_cast<const NumberToStringConstant*>(&other);
64 case StringConstantKind::kStringCons: {
65 return static_cast<const StringCons*
>(
this) ==
66 static_cast<const StringCons*>(&other);
72 size_t hash_value(StringConstantBase
const& base) {
73 switch (base.kind()) {
74 case StringConstantKind::kStringLiteral: {
75 return hash_value(*static_cast<const StringLiteral*>(&base));
77 case StringConstantKind::kNumberToStringConstant: {
78 return hash_value(*static_cast<const NumberToStringConstant*>(&base));
80 case StringConstantKind::kStringCons: {
81 return hash_value(*static_cast<const StringCons*>(&base));
87 bool operator==(StringLiteral
const& lhs, StringLiteral
const& rhs) {
88 return lhs.str().address() == rhs.str().address();
91 bool operator!=(StringLiteral
const& lhs, StringLiteral
const& rhs) {
95 size_t hash_value(StringLiteral
const& p) {
96 return base::hash_combine(p.str().address());
99 std::ostream& operator<<(std::ostream& os, StringLiteral
const& p) {
100 return os << Brief(*p.str());
103 bool operator==(NumberToStringConstant
const& lhs,
104 NumberToStringConstant
const& rhs) {
105 return lhs.num() == rhs.num();
108 bool operator!=(NumberToStringConstant
const& lhs,
109 NumberToStringConstant
const& rhs) {
110 return !(lhs == rhs);
113 size_t hash_value(NumberToStringConstant
const& p) {
114 return base::hash_combine(p.num());
117 std::ostream& operator<<(std::ostream& os, NumberToStringConstant
const& p) {
118 return os << p.num();
121 bool operator==(StringCons
const& lhs, StringCons
const& rhs) {
123 return *(lhs.lhs()) == *(rhs.lhs()) && *(lhs.rhs()) == *(rhs.rhs());
126 bool operator!=(StringCons
const& lhs, StringCons
const& rhs) {
127 return !(lhs == rhs);
130 size_t hash_value(StringCons
const& p) {
131 return base::hash_combine(*(p.lhs()), *(p.rhs()));
134 std::ostream& operator<<(std::ostream& os,
const StringConstantBase* base) {
135 os <<
"DelayedStringConstant: ";
136 switch (base->kind()) {
137 case StringConstantKind::kStringLiteral: {
138 os << *static_cast<const StringLiteral*>(base);
141 case StringConstantKind::kNumberToStringConstant: {
142 os << *static_cast<const NumberToStringConstant*>(base);
145 case StringConstantKind::kStringCons: {
146 os << *static_cast<const StringCons*>(base);
153 std::ostream& operator<<(std::ostream& os, StringCons
const& p) {
154 return os << p.lhs() <<
", " << p.rhs();
157 size_t StringConstantBase::GetMaxStringConstantLength()
const {
159 case StringConstantKind::kStringLiteral: {
160 return static_cast<const StringLiteral*
>(
this)
161 ->GetMaxStringConstantLength();
163 case StringConstantKind::kNumberToStringConstant: {
164 return static_cast<const NumberToStringConstant*
>(
this)
165 ->GetMaxStringConstantLength();
167 case StringConstantKind::kStringCons: {
168 return static_cast<const StringCons*
>(
this)->GetMaxStringConstantLength();
174 size_t StringLiteral::GetMaxStringConstantLength()
const {
return length_; }
176 size_t NumberToStringConstant::GetMaxStringConstantLength()
const {
177 return kBase10MaximalLength + 1;
180 size_t StringCons::GetMaxStringConstantLength()
const {
181 return lhs()->GetMaxStringConstantLength() +
182 rhs()->GetMaxStringConstantLength();