#include "src/torque/implementation-visitor.h"
#include <algorithm>
#include <iomanip>
#include <optional>
#include <string>
#include "src/common/globals.h"
#include "src/numbers/integer-literal-inl.h"
#include "src/torque/cc-generator.h"
#include "src/torque/cfg.h"
#include "src/torque/constants.h"
#include "src/torque/cpp-builder.h"
#include "src/torque/csa-generator.h"
#include "src/torque/declaration-visitor.h"
#include "src/torque/global-context.h"
#include "src/torque/kythe-data.h"
#include "src/torque/parameter-difference.h"
#include "src/torque/server-data.h"
#include "src/torque/source-positions.h"
#include "src/torque/type-inference.h"
#include "src/torque/type-visitor.h"
#include "src/torque/types.h"
#include "src/torque/utils.h"
namespace v8::internal::torque {
uint64_t next_unique_binding_index = …;
usingnamespacestd::string_literals;
namespace {
const char* BuiltinIncludesMarker = …;
}
VisitResult ImplementationVisitor::Visit(Expression* expr) { … }
const Type* ImplementationVisitor::Visit(Statement* stmt) { … }
void ImplementationVisitor::BeginGeneratedFiles() { … }
void ImplementationVisitor::EndGeneratedFiles() { … }
void ImplementationVisitor::BeginDebugMacrosFile() { … }
void ImplementationVisitor::EndDebugMacrosFile() { … }
void ImplementationVisitor::Visit(NamespaceConstant* decl) { … }
void ImplementationVisitor::Visit(TypeAlias* alias) { … }
class ImplementationVisitor::MacroInliningScope { … };
VisitResult ImplementationVisitor::InlineMacro(
Macro* macro, std::optional<LocationReference> this_reference,
const std::vector<VisitResult>& arguments,
const std::vector<Block*> label_blocks) { … }
void ImplementationVisitor::VisitMacroCommon(Macro* macro) { … }
void ImplementationVisitor::Visit(TorqueMacro* macro) { … }
void ImplementationVisitor::Visit(Method* method) { … }
namespace {
std::string AddParameter(size_t i, Builtin* builtin,
Stack<std::string>* parameters,
Stack<const Type*>* parameter_types,
BlockBindings<LocalValue>* parameter_bindings,
bool mark_as_used) { … }
}
void ImplementationVisitor::Visit(Builtin* builtin) { … }
const Type* ImplementationVisitor::Visit(VarDeclarationStatement* stmt) { … }
const Type* ImplementationVisitor::Visit(
VarDeclarationStatement* stmt, BlockBindings<LocalValue>* block_bindings) { … }
const Type* ImplementationVisitor::Visit(TailCallStatement* stmt) { … }
VisitResult ImplementationVisitor::Visit(ConditionalExpression* expr) { … }
VisitResult ImplementationVisitor::Visit(LogicalOrExpression* expr) { … }
VisitResult ImplementationVisitor::Visit(LogicalAndExpression* expr) { … }
VisitResult ImplementationVisitor::Visit(IncrementDecrementExpression* expr) { … }
VisitResult ImplementationVisitor::Visit(AssignmentExpression* expr) { … }
VisitResult ImplementationVisitor::Visit(FloatingPointLiteralExpression* expr) { … }
VisitResult ImplementationVisitor::Visit(IntegerLiteralExpression* expr) { … }
VisitResult ImplementationVisitor::Visit(AssumeTypeImpossibleExpression* expr) { … }
VisitResult ImplementationVisitor::Visit(StringLiteralExpression* expr) { … }
VisitResult ImplementationVisitor::GetBuiltinCode(Builtin* builtin) { … }
VisitResult ImplementationVisitor::Visit(LocationExpression* expr) { … }
VisitResult ImplementationVisitor::Visit(FieldAccessExpression* expr) { … }
const Type* ImplementationVisitor::Visit(GotoStatement* stmt) { … }
const Type* ImplementationVisitor::Visit(IfStatement* stmt) { … }
const Type* ImplementationVisitor::Visit(WhileStatement* stmt) { … }
const Type* ImplementationVisitor::Visit(BlockStatement* block) { … }
const Type* ImplementationVisitor::Visit(DebugStatement* stmt) { … }
namespace {
std::string FormatAssertSource(const std::string& str) { … }
}
const Type* ImplementationVisitor::Visit(AssertStatement* stmt) { … }
const Type* ImplementationVisitor::Visit(ExpressionStatement* stmt) { … }
const Type* ImplementationVisitor::Visit(ReturnStatement* stmt) { … }
VisitResult ImplementationVisitor::Visit(TryLabelExpression* expr) { … }
VisitResult ImplementationVisitor::Visit(StatementExpression* expr) { … }
InitializerResults ImplementationVisitor::VisitInitializerResults(
const ClassType* class_type,
const std::vector<NameAndExpression>& initializers) { … }
LocationReference ImplementationVisitor::GenerateFieldReference(
VisitResult object, const Field& field, const ClassType* class_type,
bool treat_optional_as_indexed) { … }
LocationReference ImplementationVisitor::GenerateFieldReferenceForInit(
VisitResult object, const Field& field,
const LayoutForInitialization& layout) { … }
void ImplementationVisitor::InitializeClass(
const ClassType* class_type, VisitResult allocate_result,
const InitializerResults& initializer_results,
const LayoutForInitialization& layout) { … }
VisitResult ImplementationVisitor::GenerateArrayLength(
Expression* array_length, Namespace* nspace,
const std::map<std::string, LocalValue>& bindings) { … }
VisitResult ImplementationVisitor::GenerateArrayLength(VisitResult object,
const Field& field) { … }
VisitResult ImplementationVisitor::GenerateArrayLength(
const ClassType* class_type, const InitializerResults& initializer_results,
const Field& field) { … }
LayoutForInitialization ImplementationVisitor::GenerateLayoutForInitialization(
const ClassType* class_type,
const InitializerResults& initializer_results) { … }
VisitResult ImplementationVisitor::Visit(NewExpression* expr) { … }
const Type* ImplementationVisitor::Visit(BreakStatement* stmt) { … }
const Type* ImplementationVisitor::Visit(ContinueStatement* stmt) { … }
const Type* ImplementationVisitor::Visit(ForLoopStatement* stmt) { … }
VisitResult ImplementationVisitor::Visit(SpreadExpression* expr) { … }
void ImplementationVisitor::GenerateImplementation(const std::string& dir) { … }
cpp::Function ImplementationVisitor::GenerateMacroFunctionDeclaration(
Macro* macro) { … }
cpp::Function ImplementationVisitor::GenerateFunction(
cpp::Class* owner, const std::string& name, const Signature& signature,
const NameVector& parameter_names, bool pass_code_assembler_state,
std::vector<std::string>* generated_parameter_names) { … }
namespace {
void FailCallableLookup(
const std::string& reason, const QualifiedName& name,
const TypeVector& parameter_types,
const std::vector<Binding<LocalLabel>*>& labels,
const std::vector<Signature>& candidates,
const std::vector<std::pair<GenericCallable*, std::string>>
inapplicable_generics) { … }
Callable* GetOrCreateSpecialization(
const SpecializationKey<GenericCallable>& key) { … }
}
std::optional<Binding<LocalValue>*> ImplementationVisitor::TryLookupLocalValue(
const std::string& name) { … }
std::optional<Binding<LocalLabel>*> ImplementationVisitor::TryLookupLabel(
const std::string& name) { … }
Binding<LocalLabel>* ImplementationVisitor::LookupLabel(
const std::string& name) { … }
Block* ImplementationVisitor::LookupSimpleLabel(const std::string& name) { … }
bool ImplementationVisitor::TestLookupCallable(
const QualifiedName& name, const TypeVector& parameter_types) { … }
TypeArgumentInference ImplementationVisitor::InferSpecializationTypes(
GenericCallable* generic, const TypeVector& explicit_specialization_types,
const TypeVector& explicit_arguments) { … }
template <class Container>
Callable* ImplementationVisitor::LookupCallable(
const QualifiedName& name, const Container& declaration_container,
const TypeVector& parameter_types,
const std::vector<Binding<LocalLabel>*>& labels,
const TypeVector& specialization_types, bool silence_errors) { … }
template <class Container>
Callable* ImplementationVisitor::LookupCallable(
const QualifiedName& name, const Container& declaration_container,
const Arguments& arguments, const TypeVector& specialization_types) { … }
Method* ImplementationVisitor::LookupMethod(
const std::string& name, const AggregateType* receiver_type,
const Arguments& arguments, const TypeVector& specialization_types) { … }
const Type* ImplementationVisitor::GetCommonType(const Type* left,
const Type* right) { … }
VisitResult ImplementationVisitor::GenerateCopy(const VisitResult& to_copy) { … }
VisitResult ImplementationVisitor::Visit(StructExpression* expr) { … }
VisitResult ImplementationVisitor::GenerateSetBitField(
const Type* bitfield_struct_type, const BitField& bitfield,
VisitResult bitfield_struct, VisitResult value, bool starts_as_zero) { … }
LocationReference ImplementationVisitor::GetLocationReference(
Expression* location) { … }
LocationReference ImplementationVisitor::GetLocationReference(
FieldAccessExpression* expr) { … }
LocationReference ImplementationVisitor::GenerateFieldAccess(
LocationReference reference, const std::string& fieldname,
bool ignore_stuct_field_constness, std::optional<SourcePosition> pos) { … }
LocationReference ImplementationVisitor::GetLocationReference(
ElementAccessExpression* expr) { … }
LocationReference ImplementationVisitor::GenerateReferenceToItemInHeapSlice(
LocationReference slice, VisitResult index) { … }
LocationReference ImplementationVisitor::GetLocationReference(
IdentifierExpression* expr) { … }
LocationReference ImplementationVisitor::GetLocationReference(
DereferenceExpression* expr) { … }
VisitResult ImplementationVisitor::GenerateFetchFromLocation(
const LocationReference& reference) { … }
void ImplementationVisitor::GenerateAssignToLocation(
const LocationReference& reference, const VisitResult& assignment_value) { … }
VisitResult ImplementationVisitor::GeneratePointerCall(
Expression* callee, const Arguments& arguments, bool is_tailcall) { … }
void ImplementationVisitor::AddCallParameter(
Callable* callable, VisitResult parameter, const Type* parameter_type,
std::vector<VisitResult>* converted_arguments, StackRange* argument_range,
std::vector<std::string>* constexpr_arguments, bool inline_macro) { … }
namespace {
std::pair<std::string, std::string> GetClassInstanceTypeRange(
const ClassType* class_type) { … }
}
VisitResult ImplementationVisitor::GenerateCall(
Callable* callable, std::optional<LocationReference> this_reference,
Arguments arguments, const TypeVector& specialization_types,
bool is_tailcall) { … }
VisitResult ImplementationVisitor::GenerateCall(
const QualifiedName& callable_name, Arguments arguments,
const TypeVector& specialization_types, bool is_tailcall) { … }
VisitResult ImplementationVisitor::Visit(CallExpression* expr,
bool is_tailcall) { … }
VisitResult ImplementationVisitor::Visit(CallMethodExpression* expr) { … }
VisitResult ImplementationVisitor::Visit(IntrinsicCallExpression* expr) { … }
void ImplementationVisitor::GenerateBranch(const VisitResult& condition,
Block* true_block,
Block* false_block) { … }
VisitResult ImplementationVisitor::GenerateBoolConstant(bool constant) { … }
void ImplementationVisitor::GenerateExpressionBranch(Expression* expression,
Block* true_block,
Block* false_block) { … }
VisitResult ImplementationVisitor::GenerateImplicitConvert(
const Type* destination_type, VisitResult source) { … }
StackRange ImplementationVisitor::GenerateLabelGoto(
LocalLabel* label, std::optional<StackRange> arguments) { … }
std::vector<Binding<LocalLabel>*> ImplementationVisitor::LabelsFromIdentifiers(
const std::vector<Identifier*>& names) { … }
StackRange ImplementationVisitor::LowerParameter(
const Type* type, const std::string& parameter_name,
Stack<std::string>* lowered_parameters) { … }
void ImplementationVisitor::LowerLabelParameter(
const Type* type, const std::string& parameter_name,
std::vector<std::string>* lowered_parameters) { … }
std::string ImplementationVisitor::ExternalLabelName(
const std::string& label_name) { … }
std::string ImplementationVisitor::ExternalLabelParameterName(
const std::string& label_name, size_t i) { … }
std::string ImplementationVisitor::ExternalParameterName(
const std::string& name) { … }
bool IsCompatibleSignature(const Signature& sig, const TypeVector& types,
size_t label_count) { … }
std::optional<Block*> ImplementationVisitor::GetCatchBlock() { … }
void ImplementationVisitor::GenerateCatchBlock(
std::optional<Block*> catch_block) { … }
void ImplementationVisitor::VisitAllDeclarables() { … }
void ImplementationVisitor::Visit(Declarable* declarable,
std::optional<SourceId> file) { … }
std::string MachineTypeString(const Type* type) { … }
void ImplementationVisitor::GenerateBuiltinDefinitionsAndInterfaceDescriptors(
const std::string& output_directory) { … }
namespace {
enum class FieldSectionType : uint32_t { … };
bool IsPointerSection(FieldSectionType type) { … }
FieldSections;
std::string ToString(FieldSectionType type) { … }
class FieldOffsetsGenerator { … };
void GenerateClassExport(const ClassType* type, std::ostream& header,
std::ostream& inl_header) { … }
}
void ImplementationVisitor::GenerateVisitorLists(
const std::string& output_directory) { … }
void ImplementationVisitor::GenerateBitFields(
const std::string& output_directory) { … }
namespace {
class ClassFieldOffsetGenerator : public FieldOffsetsGenerator { … };
class CppClassGenerator { … };
std::optional<std::vector<Field>> GetOrderedUniqueIndexFields(
const ClassType& type) { … }
void CppClassGenerator::GenerateClass() { … }
void CppClassGenerator::GenerateCppObjectDefinitionAsserts() { … }
void CppClassGenerator::GenerateCppObjectLayoutDefinitionAsserts() { … }
SourcePosition CppClassGenerator::Position() { … }
void CppClassGenerator::GenerateClassConstructors() { … }
namespace {
std::string GenerateRuntimeTypeCheck(const Type* type,
const std::string& value) { … }
void GenerateBoundsDCheck(std::ostream& os, const std::string& index,
const ClassType* type, const Field& f) { … }
bool CanGenerateFieldAccessors(const Type* field_type) { … }
}
void CppClassGenerator::GenerateFieldAccessors(
const Field& class_field, std::vector<const Field*>& struct_fields) { … }
std::string CppClassGenerator::GetFieldOffsetForAccessor(const Field& f) { … }
std::string CppClassGenerator::GetTypeNameForAccessor(const Field& f) { … }
bool CppClassGenerator::CanContainHeapObjects(const Type* t) { … }
void CppClassGenerator::EmitLoadFieldStatement(
std::ostream& stream, const Field& class_field,
std::vector<const Field*>& struct_fields) { … }
void CppClassGenerator::EmitStoreFieldStatement(
std::ostream& stream, const Field& class_field,
std::vector<const Field*>& struct_fields) { … }
void GenerateStructLayoutDescription(std::ostream& header,
const StructType* type) { … }
}
void ImplementationVisitor::GenerateClassDefinitions(
const std::string& output_directory) { … }
namespace {
void GeneratePrintDefinitionsForClass(std::ostream& impl, const ClassType* type,
const std::string& gen_name,
const std::string& gen_name_T,
const std::string template_params) { … }
}
void ImplementationVisitor::GeneratePrintDefinitions(
const std::string& output_directory) { … }
std::optional<std::string> MatchSimpleBodyDescriptor(const ClassType* type) { … }
void ImplementationVisitor::GenerateBodyDescriptors(
const std::string& output_directory) { … }
namespace {
void GenerateFieldValueVerifier(const std::string& class_name, bool indexed,
std::string offset, const Field& leaf_field,
std::string indexed_field_size,
std::ostream& cc_contents, bool is_map) { … }
void GenerateClassFieldVerifier(const std::string& class_name,
const ClassType& class_type, const Field& f,
std::ostream& h_contents,
std::ostream& cc_contents) { … }
}
void ImplementationVisitor::GenerateClassVerifiers(
const std::string& output_directory) { … }
void ImplementationVisitor::GenerateEnumVerifiers(
const std::string& output_directory) { … }
void ImplementationVisitor::GenerateExportedMacrosAssembler(
const std::string& output_directory) { … }
namespace {
void CollectAllFields(const std::string& path, const Field& field,
std::vector<std::string>& result) { … }
}
void ImplementationVisitor::GenerateCSATypes(
const std::string& output_directory) { … }
void ReportAllUnusedMacros() { … }
}