summaryrefslogtreecommitdiff
path: root/deps/v8/src/string-constants.h
blob: b7134849db372e955de4c66af183c18e59ba66d1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// Copyright 2018 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef V8_STRING_CONSTANTS_H_
#define V8_STRING_CONSTANTS_H_

#include "src/objects/string.h"
#include "src/zone/zone.h"

namespace v8 {
namespace internal {

enum class StringConstantKind {
  kStringLiteral,
  kNumberToStringConstant,
  kStringCons
};

class StringConstantBase : public ZoneObject {
 public:
  explicit StringConstantBase(StringConstantKind kind) : kind_(kind) {}

  StringConstantKind kind() const { return kind_; }
  Handle<String> AllocateStringConstant(Isolate* isolate) const;

  size_t GetMaxStringConstantLength() const;

  bool operator==(const StringConstantBase& other) const;

 private:
  void Memoize(Handle<String> flattened) const { flattened_ = flattened; }

  StringConstantKind kind_;
  mutable Handle<String> flattened_ = Handle<String>::null();
};

size_t hash_value(StringConstantBase const& base);

class StringLiteral final : public StringConstantBase {
 public:
  explicit StringLiteral(Handle<String> str, size_t length)
      : StringConstantBase(StringConstantKind::kStringLiteral),
        str_(str),
        length_(length) {}

  Handle<String> str() const { return str_; }

  size_t GetMaxStringConstantLength() const;

 private:
  Handle<String> str_;
  size_t length_;  // We store this separately to avoid accessing the heap.
};

bool operator==(StringLiteral const& lhs, StringLiteral const& rhs);
bool operator!=(StringLiteral const& lhs, StringLiteral const& rhs);

size_t hash_value(StringLiteral const& parameters);

std::ostream& operator<<(std::ostream& os, StringLiteral const& parameters);

class NumberToStringConstant final : public StringConstantBase {
 public:
  explicit NumberToStringConstant(double num)
      : StringConstantBase(StringConstantKind::kNumberToStringConstant),
        num_(num) {}

  double num() const { return num_; }

  size_t GetMaxStringConstantLength() const;

 private:
  double num_;
};

bool operator==(NumberToStringConstant const& lhs,
                NumberToStringConstant const& rhs);
bool operator!=(NumberToStringConstant const& lhs,
                NumberToStringConstant const& rhs);

size_t hash_value(NumberToStringConstant const& parameters);

std::ostream& operator<<(std::ostream& os,
                         NumberToStringConstant const& parameters);

class StringCons final : public StringConstantBase {
 public:
  explicit StringCons(const StringConstantBase* lhs,
                      const StringConstantBase* rhs)
      : StringConstantBase(StringConstantKind::kStringCons),
        lhs_(lhs),
        rhs_(rhs) {}

  const StringConstantBase* lhs() const { return lhs_; }
  const StringConstantBase* rhs() const { return rhs_; }

  size_t GetMaxStringConstantLength() const;

 private:
  const StringConstantBase* lhs_;
  const StringConstantBase* rhs_;
};

bool operator==(StringCons const& lhs, StringCons const& rhs);
bool operator!=(StringCons const& lhs, StringCons const& rhs);

size_t hash_value(StringCons const& parameters);

std::ostream& operator<<(std::ostream& os, StringCons const& parameters);

}  // namespace internal
}  // namespace v8

#endif  // V8_STRING_CONSTANTS_H_