11cb0ef41Sopenharmony_ci// Copyright 2020 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#ifndef INCLUDE_SOURCE_LOCATION_H_
61cb0ef41Sopenharmony_ci#define INCLUDE_SOURCE_LOCATION_H_
71cb0ef41Sopenharmony_ci
81cb0ef41Sopenharmony_ci#include <cstddef>
91cb0ef41Sopenharmony_ci#include <string>
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_ci#include "v8config.h"  // NOLINT(build/include_directory)
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_ci#if defined(__has_builtin)
141cb0ef41Sopenharmony_ci#define V8_SUPPORTS_SOURCE_LOCATION                                      \
151cb0ef41Sopenharmony_ci  (__has_builtin(__builtin_FUNCTION) && __has_builtin(__builtin_FILE) && \
161cb0ef41Sopenharmony_ci   __has_builtin(__builtin_LINE))  // NOLINT
171cb0ef41Sopenharmony_ci#elif defined(V8_CC_GNU) && __GNUC__ >= 7
181cb0ef41Sopenharmony_ci#define V8_SUPPORTS_SOURCE_LOCATION 1
191cb0ef41Sopenharmony_ci#elif defined(V8_CC_INTEL) && __ICC >= 1800
201cb0ef41Sopenharmony_ci#define V8_SUPPORTS_SOURCE_LOCATION 1
211cb0ef41Sopenharmony_ci#else
221cb0ef41Sopenharmony_ci#define V8_SUPPORTS_SOURCE_LOCATION 0
231cb0ef41Sopenharmony_ci#endif
241cb0ef41Sopenharmony_ci
251cb0ef41Sopenharmony_cinamespace v8 {
261cb0ef41Sopenharmony_ci
271cb0ef41Sopenharmony_ci/**
281cb0ef41Sopenharmony_ci * Encapsulates source location information. Mimics C++20's
291cb0ef41Sopenharmony_ci * `std::source_location`.
301cb0ef41Sopenharmony_ci */
311cb0ef41Sopenharmony_ciclass V8_EXPORT SourceLocation final {
321cb0ef41Sopenharmony_ci public:
331cb0ef41Sopenharmony_ci  /**
341cb0ef41Sopenharmony_ci   * Construct source location information corresponding to the location of the
351cb0ef41Sopenharmony_ci   * call site.
361cb0ef41Sopenharmony_ci   */
371cb0ef41Sopenharmony_ci#if V8_SUPPORTS_SOURCE_LOCATION
381cb0ef41Sopenharmony_ci  static constexpr SourceLocation Current(
391cb0ef41Sopenharmony_ci      const char* function = __builtin_FUNCTION(),
401cb0ef41Sopenharmony_ci      const char* file = __builtin_FILE(), size_t line = __builtin_LINE()) {
411cb0ef41Sopenharmony_ci    return SourceLocation(function, file, line);
421cb0ef41Sopenharmony_ci  }
431cb0ef41Sopenharmony_ci#else
441cb0ef41Sopenharmony_ci  static constexpr SourceLocation Current() { return SourceLocation(); }
451cb0ef41Sopenharmony_ci#endif  // V8_SUPPORTS_SOURCE_LOCATION
461cb0ef41Sopenharmony_ci
471cb0ef41Sopenharmony_ci  /**
481cb0ef41Sopenharmony_ci   * Constructs unspecified source location information.
491cb0ef41Sopenharmony_ci   */
501cb0ef41Sopenharmony_ci  constexpr SourceLocation() = default;
511cb0ef41Sopenharmony_ci
521cb0ef41Sopenharmony_ci  /**
531cb0ef41Sopenharmony_ci   * Returns the name of the function associated with the position represented
541cb0ef41Sopenharmony_ci   * by this object, if any.
551cb0ef41Sopenharmony_ci   *
561cb0ef41Sopenharmony_ci   * \returns the function name as cstring.
571cb0ef41Sopenharmony_ci   */
581cb0ef41Sopenharmony_ci  constexpr const char* Function() const { return function_; }
591cb0ef41Sopenharmony_ci
601cb0ef41Sopenharmony_ci  /**
611cb0ef41Sopenharmony_ci   * Returns the name of the current source file represented by this object.
621cb0ef41Sopenharmony_ci   *
631cb0ef41Sopenharmony_ci   * \returns the file name as cstring.
641cb0ef41Sopenharmony_ci   */
651cb0ef41Sopenharmony_ci  constexpr const char* FileName() const { return file_; }
661cb0ef41Sopenharmony_ci
671cb0ef41Sopenharmony_ci  /**
681cb0ef41Sopenharmony_ci   * Returns the line number represented by this object.
691cb0ef41Sopenharmony_ci   *
701cb0ef41Sopenharmony_ci   * \returns the line number.
711cb0ef41Sopenharmony_ci   */
721cb0ef41Sopenharmony_ci  constexpr size_t Line() const { return line_; }
731cb0ef41Sopenharmony_ci
741cb0ef41Sopenharmony_ci  /**
751cb0ef41Sopenharmony_ci   * Returns a human-readable string representing this object.
761cb0ef41Sopenharmony_ci   *
771cb0ef41Sopenharmony_ci   * \returns a human-readable string representing source location information.
781cb0ef41Sopenharmony_ci   */
791cb0ef41Sopenharmony_ci  std::string ToString() const;
801cb0ef41Sopenharmony_ci
811cb0ef41Sopenharmony_ci private:
821cb0ef41Sopenharmony_ci  constexpr SourceLocation(const char* function, const char* file, size_t line)
831cb0ef41Sopenharmony_ci      : function_(function), file_(file), line_(line) {}
841cb0ef41Sopenharmony_ci
851cb0ef41Sopenharmony_ci  const char* function_ = nullptr;
861cb0ef41Sopenharmony_ci  const char* file_ = nullptr;
871cb0ef41Sopenharmony_ci  size_t line_ = 0u;
881cb0ef41Sopenharmony_ci};
891cb0ef41Sopenharmony_ci
901cb0ef41Sopenharmony_ci}  // namespace v8
911cb0ef41Sopenharmony_ci
921cb0ef41Sopenharmony_ci#endif  // INCLUDE_SOURCE_LOCATION_H_
93