11cb0ef41Sopenharmony_ci// Copyright 2021 the V8 project authors. All rights reserved.
21cb0ef41Sopenharmony_ci// Use of this source code is governed by a BSD-style license that can be
31cb0ef41Sopenharmony_ci// found in the LICENSE file.
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ci#include "src/torque/cpp-builder.h"
61cb0ef41Sopenharmony_ci
71cb0ef41Sopenharmony_cinamespace v8 {
81cb0ef41Sopenharmony_cinamespace internal {
91cb0ef41Sopenharmony_cinamespace torque {
101cb0ef41Sopenharmony_cinamespace cpp {
111cb0ef41Sopenharmony_ci
121cb0ef41Sopenharmony_civoid Function::PrintDeclarationHeader(std::ostream& stream,
131cb0ef41Sopenharmony_ci                                      int indentation) const {
141cb0ef41Sopenharmony_ci  if (!description_.empty()) {
151cb0ef41Sopenharmony_ci    stream << std::string(indentation, ' ') << "// " << description_ << "\n";
161cb0ef41Sopenharmony_ci  }
171cb0ef41Sopenharmony_ci  stream << std::string(indentation, ' ') << "// " << pos_ << "\n";
181cb0ef41Sopenharmony_ci  stream << std::string(indentation, ' ');
191cb0ef41Sopenharmony_ci  if (IsExport()) stream << "V8_EXPORT_PRIVATE ";
201cb0ef41Sopenharmony_ci  if (IsV8Inline())
211cb0ef41Sopenharmony_ci    stream << "V8_INLINE ";
221cb0ef41Sopenharmony_ci  else if (IsInline())
231cb0ef41Sopenharmony_ci    stream << "inline ";
241cb0ef41Sopenharmony_ci  if (IsStatic()) stream << "static ";
251cb0ef41Sopenharmony_ci  if (IsConstexpr()) stream << "constexpr ";
261cb0ef41Sopenharmony_ci  stream << return_type_ << " " << name_ << "(";
271cb0ef41Sopenharmony_ci  bool first = true;
281cb0ef41Sopenharmony_ci  for (const auto& p : parameters_) {
291cb0ef41Sopenharmony_ci    if (!first) stream << ", ";
301cb0ef41Sopenharmony_ci    stream << p.type;
311cb0ef41Sopenharmony_ci    if (!p.name.empty()) stream << " " << p.name;
321cb0ef41Sopenharmony_ci    if (!p.default_value.empty()) stream << " = " << p.default_value;
331cb0ef41Sopenharmony_ci    first = false;
341cb0ef41Sopenharmony_ci  }
351cb0ef41Sopenharmony_ci  stream << ")";
361cb0ef41Sopenharmony_ci  if (IsConst()) stream << " const";
371cb0ef41Sopenharmony_ci}
381cb0ef41Sopenharmony_ci
391cb0ef41Sopenharmony_civoid Function::PrintDeclaration(std::ostream& stream, int indentation) const {
401cb0ef41Sopenharmony_ci  if (indentation == kAutomaticIndentation) {
411cb0ef41Sopenharmony_ci    indentation = owning_class_ ? 2 : 0;
421cb0ef41Sopenharmony_ci  }
431cb0ef41Sopenharmony_ci  PrintDeclarationHeader(stream, indentation);
441cb0ef41Sopenharmony_ci  stream << ";\n";
451cb0ef41Sopenharmony_ci}
461cb0ef41Sopenharmony_ci
471cb0ef41Sopenharmony_civoid Function::PrintDefinition(
481cb0ef41Sopenharmony_ci    std::ostream& stream, const std::function<void(std::ostream&)>& builder,
491cb0ef41Sopenharmony_ci    int indentation) const {
501cb0ef41Sopenharmony_ci  PrintBeginDefinition(stream, indentation);
511cb0ef41Sopenharmony_ci  if (builder) {
521cb0ef41Sopenharmony_ci    builder(stream);
531cb0ef41Sopenharmony_ci  }
541cb0ef41Sopenharmony_ci  PrintEndDefinition(stream, indentation);
551cb0ef41Sopenharmony_ci}
561cb0ef41Sopenharmony_ci
571cb0ef41Sopenharmony_civoid Function::PrintInlineDefinition(
581cb0ef41Sopenharmony_ci    std::ostream& stream, const std::function<void(std::ostream&)>& builder,
591cb0ef41Sopenharmony_ci    int indentation) const {
601cb0ef41Sopenharmony_ci  PrintDeclarationHeader(stream, indentation);
611cb0ef41Sopenharmony_ci  stream << " {\n";
621cb0ef41Sopenharmony_ci  if (builder) {
631cb0ef41Sopenharmony_ci    builder(stream);
641cb0ef41Sopenharmony_ci  }
651cb0ef41Sopenharmony_ci  PrintEndDefinition(stream, indentation);
661cb0ef41Sopenharmony_ci}
671cb0ef41Sopenharmony_ci
681cb0ef41Sopenharmony_civoid Function::PrintBeginDefinition(std::ostream& stream,
691cb0ef41Sopenharmony_ci                                    int indentation) const {
701cb0ef41Sopenharmony_ci  stream << std::string(indentation, ' ') << "// " << pos_ << "\n";
711cb0ef41Sopenharmony_ci  std::string scope;
721cb0ef41Sopenharmony_ci  if (owning_class_) {
731cb0ef41Sopenharmony_ci    scope = owning_class_->GetName();
741cb0ef41Sopenharmony_ci    const auto class_template_parameters =
751cb0ef41Sopenharmony_ci        owning_class_->GetTemplateParameters();
761cb0ef41Sopenharmony_ci    if (!class_template_parameters.empty()) {
771cb0ef41Sopenharmony_ci      stream << std::string(indentation, ' ');
781cb0ef41Sopenharmony_ci      stream << "template<";
791cb0ef41Sopenharmony_ci      scope += "<";
801cb0ef41Sopenharmony_ci      bool first = true;
811cb0ef41Sopenharmony_ci      for (const auto& p : class_template_parameters) {
821cb0ef41Sopenharmony_ci        if (!first) {
831cb0ef41Sopenharmony_ci          stream << ", ";
841cb0ef41Sopenharmony_ci          scope += ", ";
851cb0ef41Sopenharmony_ci        }
861cb0ef41Sopenharmony_ci        if (p.type.empty()) {
871cb0ef41Sopenharmony_ci          stream << "class " << p.name;
881cb0ef41Sopenharmony_ci        } else {
891cb0ef41Sopenharmony_ci          stream << p.type << " " << p.name;
901cb0ef41Sopenharmony_ci        }
911cb0ef41Sopenharmony_ci        scope += p.name;
921cb0ef41Sopenharmony_ci        first = false;
931cb0ef41Sopenharmony_ci      }
941cb0ef41Sopenharmony_ci      stream << ">\n";
951cb0ef41Sopenharmony_ci      scope += ">";
961cb0ef41Sopenharmony_ci    }
971cb0ef41Sopenharmony_ci    scope += "::";
981cb0ef41Sopenharmony_ci  }
991cb0ef41Sopenharmony_ci  stream << std::string(indentation, ' ') << return_type_ << " " << scope
1001cb0ef41Sopenharmony_ci         << name_ << "(";
1011cb0ef41Sopenharmony_ci  bool first = true;
1021cb0ef41Sopenharmony_ci  for (const auto& p : parameters_) {
1031cb0ef41Sopenharmony_ci    if (!first) stream << ", ";
1041cb0ef41Sopenharmony_ci    stream << p.type;
1051cb0ef41Sopenharmony_ci    if (!p.name.empty()) stream << " " << p.name;
1061cb0ef41Sopenharmony_ci    first = false;
1071cb0ef41Sopenharmony_ci  }
1081cb0ef41Sopenharmony_ci  stream << ")";
1091cb0ef41Sopenharmony_ci  if (IsConst()) {
1101cb0ef41Sopenharmony_ci    stream << " const";
1111cb0ef41Sopenharmony_ci  }
1121cb0ef41Sopenharmony_ci  stream << " {\n";
1131cb0ef41Sopenharmony_ci}
1141cb0ef41Sopenharmony_ci
1151cb0ef41Sopenharmony_civoid Function::PrintEndDefinition(std::ostream& stream, int indentation) const {
1161cb0ef41Sopenharmony_ci  stream << std::string(indentation, ' ');
1171cb0ef41Sopenharmony_ci  stream << "}\n\n";
1181cb0ef41Sopenharmony_ci}
1191cb0ef41Sopenharmony_ci
1201cb0ef41Sopenharmony_civoid File::BeginIncludeGuard(const std::string& name) {
1211cb0ef41Sopenharmony_ci  s() << "#ifndef " << name
1221cb0ef41Sopenharmony_ci      << "\n"
1231cb0ef41Sopenharmony_ci         "#define "
1241cb0ef41Sopenharmony_ci      << name << "\n\n";
1251cb0ef41Sopenharmony_ci}
1261cb0ef41Sopenharmony_ci
1271cb0ef41Sopenharmony_civoid File::EndIncludeGuard(const std::string& name) {
1281cb0ef41Sopenharmony_ci  s() << "#endif // " << name << "\n";
1291cb0ef41Sopenharmony_ci}
1301cb0ef41Sopenharmony_ci
1311cb0ef41Sopenharmony_civoid File::BeginNamespace(std::string name) {
1321cb0ef41Sopenharmony_ci  DCHECK(!name.empty());
1331cb0ef41Sopenharmony_ci  DCHECK_EQ(name.find(':'), std::string::npos);
1341cb0ef41Sopenharmony_ci  s() << "namespace " << name << " {\n";
1351cb0ef41Sopenharmony_ci  namespaces_.push(std::move(name));
1361cb0ef41Sopenharmony_ci}
1371cb0ef41Sopenharmony_ci
1381cb0ef41Sopenharmony_civoid File::BeginNamespace(std::string name0, std::string name1) {
1391cb0ef41Sopenharmony_ci  BeginNamespace(name0);
1401cb0ef41Sopenharmony_ci  BeginNamespace(name1);
1411cb0ef41Sopenharmony_ci}
1421cb0ef41Sopenharmony_ci
1431cb0ef41Sopenharmony_civoid File::EndNamespace(const std::string& name) {
1441cb0ef41Sopenharmony_ci  DCHECK(!namespaces_.empty());
1451cb0ef41Sopenharmony_ci  DCHECK_EQ(namespaces_.top(), name);
1461cb0ef41Sopenharmony_ci  s() << "} // namespace " << namespaces_.top() << "\n";
1471cb0ef41Sopenharmony_ci  namespaces_.pop();
1481cb0ef41Sopenharmony_ci}
1491cb0ef41Sopenharmony_ci
1501cb0ef41Sopenharmony_civoid File::EndNamespace(const std::string& name0, const std::string& name1) {
1511cb0ef41Sopenharmony_ci  EndNamespace(name1);
1521cb0ef41Sopenharmony_ci  EndNamespace(name0);
1531cb0ef41Sopenharmony_ci}
1541cb0ef41Sopenharmony_ci
1551cb0ef41Sopenharmony_ci}  // namespace cpp
1561cb0ef41Sopenharmony_ci}  // namespace torque
1571cb0ef41Sopenharmony_ci}  // namespace internal
1581cb0ef41Sopenharmony_ci}  // namespace v8
159