1*4bdff4beSrobert// -*- C++ -*-
2*4bdff4beSrobert//===----------------------------------------------------------------------===//
3*4bdff4beSrobert//
4*4bdff4beSrobert// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5*4bdff4beSrobert// See https://llvm.org/LICENSE.txt for license information.
6*4bdff4beSrobert// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7*4bdff4beSrobert//
8*4bdff4beSrobert//===----------------------------------------------------------------------===//
9*4bdff4beSrobert
10*4bdff4beSrobert#ifndef _LIBCPP_SOURCE_LOCATION
11*4bdff4beSrobert#define _LIBCPP_SOURCE_LOCATION
12*4bdff4beSrobert
13*4bdff4beSrobert/* source_location synopsis
14*4bdff4beSrobert
15*4bdff4beSrobertnamespace std {
16*4bdff4beSrobert  struct source_location {
17*4bdff4beSrobert    static consteval source_location current() noexcept;
18*4bdff4beSrobert    constexpr source_location() noexcept;
19*4bdff4beSrobert
20*4bdff4beSrobert    constexpr uint_least32_t line() const noexcept;
21*4bdff4beSrobert    constexpr uint_least32_t column() const noexcept;
22*4bdff4beSrobert    constexpr const char* file_name() const noexcept;
23*4bdff4beSrobert    constexpr const char* function_name() const noexcept;
24*4bdff4beSrobert  };
25*4bdff4beSrobert}
26*4bdff4beSrobert*/
27*4bdff4beSrobert
28*4bdff4beSrobert#include <__config>
29*4bdff4beSrobert#include <cstdint>
30*4bdff4beSrobert#include <version>
31*4bdff4beSrobert
32*4bdff4beSrobert#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
33*4bdff4beSrobert#  pragma GCC system_header
34*4bdff4beSrobert#endif
35*4bdff4beSrobert
36*4bdff4beSrobert_LIBCPP_BEGIN_NAMESPACE_STD
37*4bdff4beSrobert
38*4bdff4beSrobert#if _LIBCPP_STD_VER >= 20 && __has_builtin(__builtin_source_location) &&                                               \
39*4bdff4beSrobert    !(defined(_LIBCPP_APPLE_CLANG_VER) && _LIBCPP_APPLE_CLANG_VER <= 1403)
40*4bdff4beSrobert
41*4bdff4beSrobertclass source_location {
42*4bdff4beSrobert  // The names source_location::__impl, _M_file_name, _M_function_name, _M_line, and _M_column
43*4bdff4beSrobert  // are hard-coded in the compiler and must not be changed here.
44*4bdff4beSrobert  struct __impl {
45*4bdff4beSrobert    const char* _M_file_name;
46*4bdff4beSrobert    const char* _M_function_name;
47*4bdff4beSrobert    unsigned _M_line;
48*4bdff4beSrobert    unsigned _M_column;
49*4bdff4beSrobert  };
50*4bdff4beSrobert  const __impl* __ptr_ = nullptr;
51*4bdff4beSrobert  // GCC returns the type 'const void*' from the builtin, while clang returns
52*4bdff4beSrobert  // `const __impl*`. Per C++ [expr.const], casts from void* are not permitted
53*4bdff4beSrobert  // in constant evaluation, so we don't want to use `void*` as the argument
54*4bdff4beSrobert  // type unless the builtin returned that, anyhow, and the invalid cast is
55*4bdff4beSrobert  // unavoidable.
56*4bdff4beSrobert  using __bsl_ty = decltype(__builtin_source_location());
57*4bdff4beSrobert
58*4bdff4beSrobertpublic:
59*4bdff4beSrobert  // The defaulted __ptr argument is necessary so that the builtin is evaluated
60*4bdff4beSrobert  // in the context of the caller. An explicit value should never be provided.
61*4bdff4beSrobert  static consteval source_location current(__bsl_ty __ptr = __builtin_source_location()) noexcept {
62*4bdff4beSrobert    source_location __sl;
63*4bdff4beSrobert    __sl.__ptr_ = static_cast<const __impl*>(__ptr);
64*4bdff4beSrobert    return __sl;
65*4bdff4beSrobert  }
66*4bdff4beSrobert  _LIBCPP_HIDE_FROM_ABI constexpr source_location() noexcept = default;
67*4bdff4beSrobert
68*4bdff4beSrobert  _LIBCPP_HIDE_FROM_ABI constexpr uint_least32_t line() const noexcept {
69*4bdff4beSrobert    return __ptr_ != nullptr ? __ptr_->_M_line : 0;
70*4bdff4beSrobert  }
71*4bdff4beSrobert  _LIBCPP_HIDE_FROM_ABI constexpr uint_least32_t column() const noexcept {
72*4bdff4beSrobert    return __ptr_ != nullptr ? __ptr_->_M_column : 0;
73*4bdff4beSrobert  }
74*4bdff4beSrobert  _LIBCPP_HIDE_FROM_ABI constexpr const char* file_name() const noexcept {
75*4bdff4beSrobert    return __ptr_ != nullptr ? __ptr_->_M_file_name : "";
76*4bdff4beSrobert  }
77*4bdff4beSrobert  _LIBCPP_HIDE_FROM_ABI constexpr const char* function_name() const noexcept {
78*4bdff4beSrobert    return __ptr_ != nullptr ? __ptr_->_M_function_name : "";
79*4bdff4beSrobert  }
80*4bdff4beSrobert};
81*4bdff4beSrobert
82*4bdff4beSrobert#endif // _LIBCPP_STD_VER >= 20 && __has_builtin(__builtin_source_location) && !(defined(_LIBCPP_APPLE_CLANG_VER) &&
83*4bdff4beSrobert       // _LIBCPP_APPLE_CLANG_VER <= 1403)
84*4bdff4beSrobert
85*4bdff4beSrobert_LIBCPP_END_NAMESPACE_STD
86*4bdff4beSrobert
87*4bdff4beSrobert#endif // _LIBCPP_SOURCE_LOCATION
88