// Copyright 2014 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.

#include "src/objects/property.h"

#include "src/base/macros.h"
#include "src/handles/handles-inl.h"
#include "src/objects/field-type.h"
#include "src/objects/name-inl.h"
#include "src/objects/objects-inl.h"
#include "src/objects/smi.h"
#include "src/utils/ostreams.h"

namespace v8 {
namespace internal {

std::ostream& operator<<(std::ostream& os,
                         const Representation& representation) {
  switch (representation.kind()) {
    case Representation::kNone:
      return os << "none";
    case Representation::kSmi:
      return os << "smi";
    case Representation::kDouble:
      return os << "double";
    case Representation::kHeapObject:
      return os << "heap-object";
    case Representation::kTagged:
      return os << "tagged";
    case Representation::kWasmValue:
      return os << "wasm-value";
    case Representation::kNumRepresentations:
      UNREACHABLE();
  }
  UNREACHABLE();
}

std::ostream& operator<<(std::ostream& os,
                         const PropertyAttributes& attributes) {
  os << "[";
  os << (((attributes & READ_ONLY) == 0) ? "W" : "_");    // writable
  os << (((attributes & DONT_ENUM) == 0) ? "E" : "_");    // enumerable
  os << (((attributes & DONT_DELETE) == 0) ? "C" : "_");  // configurable
  os << "]";
  return os;
}

std::ostream& operator<<(std::ostream& os, PropertyConstness constness) {
  switch (constness) {
    case PropertyConstness::kMutable:
      return os << "mutable";
    case PropertyConstness::kConst:
      return os << "const";
  }
  UNREACHABLE();
}

Descriptor::Descriptor() : details_(Smi::zero()) {}

Descriptor::Descriptor(DirectHandle<Name> key,
                       const MaybeObjectDirectHandle& value, PropertyKind kind,
                       PropertyAttributes attributes, PropertyLocation location,
                       PropertyConstness constness,
                       Representation representation, int field_offset,
                       bool in_object)
    : key_(key),
      value_(value),
      details_(kind, attributes, location, constness, representation,
               field_offset, in_object) {
  DCHECK(IsUniqueName(*key));
  DCHECK_IMPLIES(key->IsAnyPrivate(), !details_.IsEnumerable());
}

Descriptor::Descriptor(DirectHandle<Name> key,
                       const MaybeObjectDirectHandle& value,
                       PropertyDetails details)
    : key_(key), value_(value), details_(details) {
  DCHECK(IsUniqueName(*key));
  DCHECK_IMPLIES(key->IsAnyPrivate(), !details_.IsEnumerable());
}

Descriptor Descriptor::DataField(Isolate* isolate, DirectHandle<Name> key,
                                 int field_offset,
                                 PropertyAttributes attributes,
                                 Representation representation,
                                 bool in_object) {
  return DataField(key, field_offset, attributes, PropertyConstness::kMutable,
                   representation,
                   MaybeObjectDirectHandle(FieldType::Any(isolate)), in_object);
}

Descriptor Descriptor::DataField(
    DirectHandle<Name> key, int field_offset, PropertyAttributes attributes,
    PropertyConstness constness, Representation representation,
    const MaybeObjectDirectHandle& wrapped_field_type, bool in_object) {
  DCHECK(IsSmi(*wrapped_field_type) || IsWeak(*wrapped_field_type));
  PropertyDetails details(PropertyKind::kData, attributes,
                          PropertyLocation::kField, constness, representation,
                          field_offset, in_object);
  return Descriptor(key, wrapped_field_type, details);
}

Descriptor Descriptor::DataConstant(DirectHandle<Name> key,
                                    DirectHandle<Object> value,
                                    PropertyAttributes attributes) {
  auto [representation, constness] =
      Object::OptimalRepresentation(*value, PropertyConstness::kConst);
  return Descriptor(key, MaybeObjectDirectHandle(value), PropertyKind::kData,
                    attributes, PropertyLocation::kDescriptor, constness,
                    representation, 0, false);
}

Descriptor Descriptor::AccessorConstant(DirectHandle<Name> key,
                                        DirectHandle<Object> foreign,
                                        PropertyAttributes attributes) {
  return Descriptor(key, MaybeObjectDirectHandle(foreign),
                    PropertyKind::kAccessor, attributes,
                    PropertyLocation::kDescriptor, PropertyConstness::kConst,
                    Representation::Tagged(), 0, false);
}

// Outputs PropertyDetails as a dictionary details.
void PropertyDetails::PrintAsSlowTo(std::ostream& os, bool print_dict_index) {
  os << "(";
  if (constness() == PropertyConstness::kConst) os << "const ";
  os << (kind() == PropertyKind::kData ? "data" : "accessor");
  if (print_dict_index) {
    os << ", dict_index: " << dictionary_index();
  }
  os << ", attrs: " << attributes() << ")";
}

// Outputs PropertyDetails as a descriptor array details.
void PropertyDetails::PrintAsFastTo(std::ostream& os, PrintMode mode) {
  os << "(";
  if (constness() == PropertyConstness::kConst) os << "const ";
  os << (kind() == PropertyKind::kData ? "data" : "accessor");
  if (location() == PropertyLocation::kField) {
    os << " field";
    if (mode & kPrintOffsetInWords) {
      os << " " << field_offset();
    }
    if (mode & kPrintRepresentation) {
      os << ":" << representation().Mnemonic();
    }
    os << ", " << (is_in_object() ? "in-obj" : "ooo");
  } else {
    os << " descriptor";
  }
  if (mode & kPrintPointer) {
    os << ", p: " << pointer();
  }
  if (mode & kPrintAttributes) {
    os << ", attrs: " << attributes();
  }
  os << ")";
}

#ifdef OBJECT_PRINT
void PropertyDetails::Print(bool dictionary_mode) {
  StdoutStream os;
  if (dictionary_mode) {
    PrintAsSlowTo(os, true);
  } else {
    PrintAsFastTo(os, PrintMode::kPrintFull);
  }
  os << "\n" << std::flush;
}
#endif

}  // namespace internal
}  // namespace v8
