1 // Copyright 2018 the V8 project authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef V8_CODEGEN_STRING_CONSTANTS_H_ 6 #define V8_CODEGEN_STRING_CONSTANTS_H_ 7 8 #include "src/handles/handles.h" 9 #include "src/objects/string.h" 10 #include "src/zone/zone.h" 11 12 namespace v8 { 13 namespace internal { 14 15 enum class StringConstantKind { 16 kStringLiteral, 17 kNumberToStringConstant, 18 kStringCons 19 }; 20 21 class StringConstantBase : public ZoneObject { 22 public: StringConstantBase(StringConstantKind kind)23 explicit StringConstantBase(StringConstantKind kind) : kind_(kind) {} 24 kind()25 StringConstantKind kind() const { return kind_; } 26 Handle<String> AllocateStringConstant(Isolate* isolate) const; 27 28 size_t GetMaxStringConstantLength() const; 29 30 bool operator==(const StringConstantBase& other) const; 31 32 private: Memoize(Handle<String> flattened)33 void Memoize(Handle<String> flattened) const { flattened_ = flattened; } 34 35 StringConstantKind kind_; 36 mutable Handle<String> flattened_ = Handle<String>::null(); 37 }; 38 39 size_t hash_value(StringConstantBase const& base); 40 41 class StringLiteral final : public StringConstantBase { 42 public: StringLiteral(Handle<String> str,size_t length)43 explicit StringLiteral(Handle<String> str, size_t length) 44 : StringConstantBase(StringConstantKind::kStringLiteral), 45 str_(str), 46 length_(length) {} 47 str()48 Handle<String> str() const { return str_; } 49 50 size_t GetMaxStringConstantLength() const; 51 52 private: 53 Handle<String> str_; 54 size_t length_; // We store this separately to avoid accessing the heap. 55 }; 56 57 bool operator==(StringLiteral const& lhs, StringLiteral const& rhs); 58 bool operator!=(StringLiteral const& lhs, StringLiteral const& rhs); 59 60 size_t hash_value(StringLiteral const& parameters); 61 62 std::ostream& operator<<(std::ostream& os, StringLiteral const& parameters); 63 64 class NumberToStringConstant final : public StringConstantBase { 65 public: NumberToStringConstant(double num)66 explicit NumberToStringConstant(double num) 67 : StringConstantBase(StringConstantKind::kNumberToStringConstant), 68 num_(num) {} 69 num()70 double num() const { return num_; } 71 72 size_t GetMaxStringConstantLength() const; 73 74 private: 75 double num_; 76 }; 77 78 bool operator==(NumberToStringConstant const& lhs, 79 NumberToStringConstant const& rhs); 80 bool operator!=(NumberToStringConstant const& lhs, 81 NumberToStringConstant const& rhs); 82 83 size_t hash_value(NumberToStringConstant const& parameters); 84 85 std::ostream& operator<<(std::ostream& os, 86 NumberToStringConstant const& parameters); 87 88 class StringCons final : public StringConstantBase { 89 public: StringCons(const StringConstantBase * lhs,const StringConstantBase * rhs)90 explicit StringCons(const StringConstantBase* lhs, 91 const StringConstantBase* rhs) 92 : StringConstantBase(StringConstantKind::kStringCons), 93 lhs_(lhs), 94 rhs_(rhs) {} 95 lhs()96 const StringConstantBase* lhs() const { return lhs_; } rhs()97 const StringConstantBase* rhs() const { return rhs_; } 98 99 size_t GetMaxStringConstantLength() const; 100 101 private: 102 const StringConstantBase* lhs_; 103 const StringConstantBase* rhs_; 104 }; 105 106 bool operator==(StringCons const& lhs, StringCons const& rhs); 107 bool operator!=(StringCons const& lhs, StringCons const& rhs); 108 109 size_t hash_value(StringCons const& parameters); 110 111 std::ostream& operator<<(std::ostream& os, StringCons const& parameters); 112 113 } // namespace internal 114 } // namespace v8 115 116 #endif // V8_CODEGEN_STRING_CONSTANTS_H_ 117