108e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org// Copyright 2014 the V8 project authors. All rights reserved.
208e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org// Redistribution and use in source and binary forms, with or without
308e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org// modification, are permitted provided that the following conditions are
408e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org// met:
508e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org//
608e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org//     * Redistributions of source code must retain the above copyright
708e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org//       notice, this list of conditions and the following disclaimer.
808e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org//     * Redistributions in binary form must reproduce the above
908e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org//       copyright notice, this list of conditions and the following
1008e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org//       disclaimer in the documentation and/or other materials provided
1108e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org//       with the distribution.
1208e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org//     * Neither the name of Google Inc. nor the names of its
1308e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org//       contributors may be used to endorse or promote products derived
1408e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org//       from this software without specific prior written permission.
1508e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org//
1608e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1708e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1808e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
1908e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2008e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2108e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2208e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2308e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2408e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2508e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2608e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2708e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org
2808e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org#ifndef V8_AST_VALUE_FACTORY_H_
2908e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org#define V8_AST_VALUE_FACTORY_H_
3008e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org
3108e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org#include "src/api.h"
3208e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org#include "src/hashmap.h"
3308e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org#include "src/utils.h"
3408e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org
3508e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org// AstString, AstValue and AstValueFactory are for storing strings and values
3608e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org// independent of the V8 heap and internalizing them later. During parsing,
3708e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org// AstStrings and AstValues are created and stored outside the heap, in
3808e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org// AstValueFactory. After parsing, the strings and values are internalized
3908e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org// (moved into the V8 heap).
4008e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.orgnamespace v8 {
4108e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.orgnamespace internal {
4208e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org
4308e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.orgclass AstString : public ZoneObject {
4408e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org public:
4508e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org  virtual ~AstString() {}
4608e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org
4708e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org  virtual int length() const = 0;
4808e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org  bool IsEmpty() const { return length() == 0; }
4908e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org
5008e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org  // Puts the string into the V8 heap.
5108e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org  virtual void Internalize(Isolate* isolate) = 0;
5208e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org
5308e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org  // This function can be called after internalizing.
5408e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org  V8_INLINE Handle<String> string() const {
55e3c177a423baa3c30225c4e422b6f6c76d38b951machenbach@chromium.org    DCHECK(!string_.is_null());
5608e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org    return string_;
5708e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org  }
5808e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org
5908e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org protected:
6008e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org  // This is null until the string is internalized.
6108e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org  Handle<String> string_;
6208e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org};
6308e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org
6408e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org
6508e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.orgclass AstRawString : public AstString {
6608e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org public:
67ada3a6017e603965f87fa34f6e2fa60379e8d697machenbach@chromium.org  virtual int length() const OVERRIDE {
6808e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org    if (is_one_byte_)
6908e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org      return literal_bytes_.length();
7008e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org    return literal_bytes_.length() / 2;
7108e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org  }
7208e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org
73ada3a6017e603965f87fa34f6e2fa60379e8d697machenbach@chromium.org  virtual void Internalize(Isolate* isolate) OVERRIDE;
7408e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org
7508e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org  bool AsArrayIndex(uint32_t* index) const;
7608e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org
7708e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org  // The string is not null-terminated, use length() to find out the length.
7808e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org  const unsigned char* raw_data() const {
7908e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org    return literal_bytes_.start();
8008e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org  }
8108e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org  bool is_one_byte() const { return is_one_byte_; }
8208e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org  bool IsOneByteEqualTo(const char* data) const;
8308e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org  uint16_t FirstCharacter() const {
8408e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org    if (is_one_byte_)
8508e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org      return literal_bytes_[0];
8608e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org    const uint16_t* c =
8708e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org        reinterpret_cast<const uint16_t*>(literal_bytes_.start());
8808e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org    return *c;
8908e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org  }
9008e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org
9108e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org  // For storing AstRawStrings in a hash map.
9208e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org  uint32_t hash() const {
9308e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org    return hash_;
9408e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org  }
9508e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org  static bool Compare(void* a, void* b);
9608e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org
9708e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org private:
9808e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org  friend class AstValueFactory;
9908e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org  friend class AstRawStringInternalizationKey;
10008e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org
10108e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org  AstRawString(bool is_one_byte, const Vector<const byte>& literal_bytes,
10208e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org            uint32_t hash)
10308e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org      : is_one_byte_(is_one_byte), literal_bytes_(literal_bytes), hash_(hash) {}
10408e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org
10508e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org  AstRawString()
10608e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org      : is_one_byte_(true),
10708e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org        hash_(0) {}
10808e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org
10908e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org  bool is_one_byte_;
11008e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org
11108e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org  // Points to memory owned by Zone.
11208e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org  Vector<const byte> literal_bytes_;
11308e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org  uint32_t hash_;
11408e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org};
11508e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org
11608e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org
11708e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.orgclass AstConsString : public AstString {
11808e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org public:
11908e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org  AstConsString(const AstString* left, const AstString* right)
12008e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org      : left_(left),
12108e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org        right_(right) {}
12208e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org
123ada3a6017e603965f87fa34f6e2fa60379e8d697machenbach@chromium.org  virtual int length() const OVERRIDE {
12408e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org    return left_->length() + right_->length();
12508e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org  }
12608e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org
127ada3a6017e603965f87fa34f6e2fa60379e8d697machenbach@chromium.org  virtual void Internalize(Isolate* isolate) OVERRIDE;
12808e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org
12908e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org private:
13008e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org  friend class AstValueFactory;
13108e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org
13208e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org  const AstString* left_;
13308e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org  const AstString* right_;
13408e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org};
13508e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org
13608e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org
13708e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org// AstValue is either a string, a number, a string array, a boolean, or a
13808e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org// special value (null, undefined, the hole).
13908e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.orgclass AstValue : public ZoneObject {
14008e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org public:
14108e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org  bool IsString() const {
14208e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org    return type_ == STRING;
14308e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org  }
14408e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org
14508e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org  bool IsNumber() const {
14608e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org    return type_ == NUMBER || type_ == SMI;
14708e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org  }
14808e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org
14908e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org  const AstRawString* AsString() const {
15008e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org    if (type_ == STRING)
15108e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org      return string_;
15208e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org    UNREACHABLE();
15308e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org    return 0;
15408e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org  }
15508e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org
15608e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org  double AsNumber() const {
15708e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org    if (type_ == NUMBER)
15808e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org      return number_;
15908e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org    if (type_ == SMI)
16008e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org      return smi_;
16108e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org    UNREACHABLE();
16208e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org    return 0;
16308e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org  }
16408e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org
16508e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org  bool EqualsString(const AstRawString* string) const {
16608e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org    return type_ == STRING && string_ == string;
16708e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org  }
16808e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org
16908e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org  bool IsPropertyName() const;
17008e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org
17108e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org  bool BooleanValue() const;
17208e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org
17308e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org  void Internalize(Isolate* isolate);
17408e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org
17508e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org  // Can be called after Internalize has been called.
17608e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org  V8_INLINE Handle<Object> value() const {
17708e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org    if (type_ == STRING) {
17808e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org      return string_->string();
17908e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org    }
180e3c177a423baa3c30225c4e422b6f6c76d38b951machenbach@chromium.org    DCHECK(!value_.is_null());
18108e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org    return value_;
18208e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org  }
18308e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org
18408e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org private:
18508e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org  friend class AstValueFactory;
18608e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org
18708e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org  enum Type {
18808e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org    STRING,
18908e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org    SYMBOL,
19008e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org    NUMBER,
19108e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org    SMI,
19208e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org    BOOLEAN,
19308e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org    STRING_ARRAY,
19408e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org    NULL_TYPE,
19508e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org    UNDEFINED,
19608e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org    THE_HOLE
19708e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org  };
19808e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org
19908e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org  explicit AstValue(const AstRawString* s) : type_(STRING) { string_ = s; }
20008e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org
20108e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org  explicit AstValue(const char* name) : type_(SYMBOL) { symbol_name_ = name; }
20208e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org
20308e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org  explicit AstValue(double n) : type_(NUMBER) { number_ = n; }
20408e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org
20508e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org  AstValue(Type t, int i) : type_(t) {
206e3c177a423baa3c30225c4e422b6f6c76d38b951machenbach@chromium.org    DCHECK(type_ == SMI);
20708e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org    smi_ = i;
20808e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org  }
20908e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org
21008e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org  explicit AstValue(bool b) : type_(BOOLEAN) { bool_ = b; }
21108e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org
21208e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org  explicit AstValue(ZoneList<const AstRawString*>* s) : type_(STRING_ARRAY) {
21308e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org    strings_ = s;
21408e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org  }
21508e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org
21608e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org  explicit AstValue(Type t) : type_(t) {
217e3c177a423baa3c30225c4e422b6f6c76d38b951machenbach@chromium.org    DCHECK(t == NULL_TYPE || t == UNDEFINED || t == THE_HOLE);
21808e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org  }
21908e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org
22008e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org  Type type_;
22108e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org
22208e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org  // Uninternalized value.
22308e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org  union {
22408e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org    const AstRawString* string_;
22508e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org    double number_;
22608e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org    int smi_;
22708e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org    bool bool_;
22808e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org    ZoneList<const AstRawString*>* strings_;
22908e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org    const char* symbol_name_;
23008e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org  };
23108e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org
23208e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org  // Internalized value (empty before internalized).
23308e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org  Handle<Object> value_;
23408e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org};
23508e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org
23608e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org
23708e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org// For generating string constants.
238a2c0c1516848536a514b3178d2c040b7df0ceb5bmachenbach@chromium.org#define STRING_CONSTANTS(F)                           \
239a2c0c1516848536a514b3178d2c040b7df0ceb5bmachenbach@chromium.org  F(anonymous_function, "(anonymous function)")       \
240a2c0c1516848536a514b3178d2c040b7df0ceb5bmachenbach@chromium.org  F(arguments, "arguments")                           \
241a2c0c1516848536a514b3178d2c040b7df0ceb5bmachenbach@chromium.org  F(constructor, "constructor")                       \
242a2c0c1516848536a514b3178d2c040b7df0ceb5bmachenbach@chromium.org  F(done, "done")                                     \
243a2c0c1516848536a514b3178d2c040b7df0ceb5bmachenbach@chromium.org  F(dot, ".")                                         \
244a2c0c1516848536a514b3178d2c040b7df0ceb5bmachenbach@chromium.org  F(dot_for, ".for")                                  \
245a2c0c1516848536a514b3178d2c040b7df0ceb5bmachenbach@chromium.org  F(dot_generator, ".generator")                      \
246a2c0c1516848536a514b3178d2c040b7df0ceb5bmachenbach@chromium.org  F(dot_generator_object, ".generator_object")        \
247a2c0c1516848536a514b3178d2c040b7df0ceb5bmachenbach@chromium.org  F(dot_iterator, ".iterator")                        \
248a2c0c1516848536a514b3178d2c040b7df0ceb5bmachenbach@chromium.org  F(dot_module, ".module")                            \
249a2c0c1516848536a514b3178d2c040b7df0ceb5bmachenbach@chromium.org  F(dot_result, ".result")                            \
250a2c0c1516848536a514b3178d2c040b7df0ceb5bmachenbach@chromium.org  F(empty, "")                                        \
251a2c0c1516848536a514b3178d2c040b7df0ceb5bmachenbach@chromium.org  F(eval, "eval")                                     \
25208e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org  F(initialize_const_global, "initializeConstGlobal") \
253a2c0c1516848536a514b3178d2c040b7df0ceb5bmachenbach@chromium.org  F(initialize_var_global, "initializeVarGlobal")     \
254a2c0c1516848536a514b3178d2c040b7df0ceb5bmachenbach@chromium.org  F(make_reference_error, "MakeReferenceError")       \
255a2c0c1516848536a514b3178d2c040b7df0ceb5bmachenbach@chromium.org  F(make_syntax_error, "MakeSyntaxError")             \
256a2c0c1516848536a514b3178d2c040b7df0ceb5bmachenbach@chromium.org  F(make_type_error, "MakeTypeError")                 \
257a2c0c1516848536a514b3178d2c040b7df0ceb5bmachenbach@chromium.org  F(module, "module")                                 \
258a2c0c1516848536a514b3178d2c040b7df0ceb5bmachenbach@chromium.org  F(native, "native")                                 \
259a2c0c1516848536a514b3178d2c040b7df0ceb5bmachenbach@chromium.org  F(next, "next")                                     \
260a2c0c1516848536a514b3178d2c040b7df0ceb5bmachenbach@chromium.org  F(proto, "__proto__")                               \
261a2c0c1516848536a514b3178d2c040b7df0ceb5bmachenbach@chromium.org  F(prototype, "prototype")                           \
262a2c0c1516848536a514b3178d2c040b7df0ceb5bmachenbach@chromium.org  F(this, "this")                                     \
263a2c0c1516848536a514b3178d2c040b7df0ceb5bmachenbach@chromium.org  F(use_asm, "use asm")                               \
264a2c0c1516848536a514b3178d2c040b7df0ceb5bmachenbach@chromium.org  F(use_strict, "use strict")                         \
26508e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org  F(value, "value")
26608e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org
26708e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org
26808e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.orgclass AstValueFactory {
26908e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org public:
27008e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org  AstValueFactory(Zone* zone, uint32_t hash_seed)
27108e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org      : string_table_(AstRawString::Compare),
27208e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org        zone_(zone),
27308e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org        isolate_(NULL),
27408e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org        hash_seed_(hash_seed) {
27508e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org#define F(name, str) \
27608e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org    name##_string_ = NULL;
27708e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org    STRING_CONSTANTS(F)
27808e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org#undef F
27908e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org  }
28008e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org
28108e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org  const AstRawString* GetOneByteString(Vector<const uint8_t> literal);
282248dd43badb99ffce44eae2d767cda3cefaad521machenbach@chromium.org  const AstRawString* GetOneByteString(const char* string) {
283248dd43badb99ffce44eae2d767cda3cefaad521machenbach@chromium.org    return GetOneByteString(Vector<const uint8_t>(
284248dd43badb99ffce44eae2d767cda3cefaad521machenbach@chromium.org        reinterpret_cast<const uint8_t*>(string), StrLength(string)));
285248dd43badb99ffce44eae2d767cda3cefaad521machenbach@chromium.org  }
28608e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org  const AstRawString* GetTwoByteString(Vector<const uint16_t> literal);
28708e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org  const AstRawString* GetString(Handle<String> literal);
28808e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org  const AstConsString* NewConsString(const AstString* left,
28908e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org                                     const AstString* right);
29008e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org
29108e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org  void Internalize(Isolate* isolate);
29208e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org  bool IsInternalized() {
29308e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org    return isolate_ != NULL;
29408e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org  }
29508e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org
29608e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org#define F(name, str) \
29708e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org  const AstRawString* name##_string() { \
29808e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org    if (name##_string_ == NULL) { \
29908e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org      const char* data = str; \
30008e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org      name##_string_ = GetOneByteString( \
30108e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org          Vector<const uint8_t>(reinterpret_cast<const uint8_t*>(data), \
30208e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org                                static_cast<int>(strlen(data)))); \
30308e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org    } \
30408e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org    return name##_string_; \
30508e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org  }
30608e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org  STRING_CONSTANTS(F)
30708e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org#undef F
30808e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org
30908e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org  const AstValue* NewString(const AstRawString* string);
31008e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org  // A JavaScript symbol (ECMA-262 edition 6).
31108e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org  const AstValue* NewSymbol(const char* name);
31208e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org  const AstValue* NewNumber(double number);
31308e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org  const AstValue* NewSmi(int number);
31408e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org  const AstValue* NewBoolean(bool b);
31508e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org  const AstValue* NewStringList(ZoneList<const AstRawString*>* strings);
31608e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org  const AstValue* NewNull();
31708e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org  const AstValue* NewUndefined();
31808e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org  const AstValue* NewTheHole();
31908e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org
32008e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org private:
32108e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org  const AstRawString* GetString(uint32_t hash, bool is_one_byte,
32208e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org                                Vector<const byte> literal_bytes);
32308e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org
32408e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org  // All strings are copied here, one after another (no NULLs inbetween).
32508e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org  HashMap string_table_;
32608e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org  // For keeping track of all AstValues and AstRawStrings we've created (so that
32708e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org  // they can be internalized later).
32808e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org  List<AstValue*> values_;
32908e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org  List<AstString*> strings_;
33008e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org  Zone* zone_;
33108e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org  Isolate* isolate_;
33208e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org
33308e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org  uint32_t hash_seed_;
33408e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org
33508e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org#define F(name, str) \
33608e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org  const AstRawString* name##_string_;
33708e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org  STRING_CONSTANTS(F)
33808e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org#undef F
33908e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org};
34008e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org
34108e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org} }  // namespace v8::internal
34208e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org
34308e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org#undef STRING_CONSTANTS
34408e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org
34508e7569a10f8edbb47b8fe70a6e160a4e0c9cd30machenbach@chromium.org#endif  // V8_AST_VALUE_FACTORY_H_
346