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_STRING_CONSTANTS_H_
6#define V8_STRING_CONSTANTS_H_
7
8#include "src/handles.h"
9#include "src/objects/string.h"
10#include "src/zone/zone.h"
11
12namespace v8 {
13namespace internal {
14
15enum class StringConstantKind {
16 kStringLiteral,
17 kNumberToStringConstant,
18 kStringCons
19};
20
21class StringConstantBase : public ZoneObject {
22 public:
23 explicit StringConstantBase(StringConstantKind kind) : kind_(kind) {}
24
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:
33 void Memoize(Handle<String> flattened) const { flattened_ = flattened; }
34
35 StringConstantKind kind_;
36 mutable Handle<String> flattened_ = Handle<String>::null();
37};
38
39size_t hash_value(StringConstantBase const& base);
40
41class StringLiteral final : public StringConstantBase {
42 public:
43 explicit StringLiteral(Handle<String> str, size_t length)
44 : StringConstantBase(StringConstantKind::kStringLiteral),
45 str_(str),
46 length_(length) {}
47
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
57bool operator==(StringLiteral const& lhs, StringLiteral const& rhs);
58bool operator!=(StringLiteral const& lhs, StringLiteral const& rhs);
59
60size_t hash_value(StringLiteral const& parameters);
61
62std::ostream& operator<<(std::ostream& os, StringLiteral const& parameters);
63
64class NumberToStringConstant final : public StringConstantBase {
65 public:
66 explicit NumberToStringConstant(double num)
67 : StringConstantBase(StringConstantKind::kNumberToStringConstant),
68 num_(num) {}
69
70 double num() const { return num_; }
71
72 size_t GetMaxStringConstantLength() const;
73
74 private:
75 double num_;
76};
77
78bool operator==(NumberToStringConstant const& lhs,
79 NumberToStringConstant const& rhs);
80bool operator!=(NumberToStringConstant const& lhs,
81 NumberToStringConstant const& rhs);
82
83size_t hash_value(NumberToStringConstant const& parameters);
84
85std::ostream& operator<<(std::ostream& os,
86 NumberToStringConstant const& parameters);
87
88class StringCons final : public StringConstantBase {
89 public:
90 explicit StringCons(const StringConstantBase* lhs,
91 const StringConstantBase* rhs)
92 : StringConstantBase(StringConstantKind::kStringCons),
93 lhs_(lhs),
94 rhs_(rhs) {}
95
96 const StringConstantBase* lhs() const { return lhs_; }
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
106bool operator==(StringCons const& lhs, StringCons const& rhs);
107bool operator!=(StringCons const& lhs, StringCons const& rhs);
108
109size_t hash_value(StringCons const& parameters);
110
111std::ostream& operator<<(std::ostream& os, StringCons const& parameters);
112
113} // namespace internal
114} // namespace v8
115
116#endif // V8_STRING_CONSTANTS_H_
117