1fe6060f1SDimitry Andric //===--------- compiler.h - Compiler abstraction support --------*- C++ -*-===// 2fe6060f1SDimitry Andric // 3fe6060f1SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4fe6060f1SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 5fe6060f1SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6fe6060f1SDimitry Andric // 7fe6060f1SDimitry Andric //===----------------------------------------------------------------------===// 8fe6060f1SDimitry Andric // 9fe6060f1SDimitry Andric // This file is a part of the ORC runtime support library. 10fe6060f1SDimitry Andric // 11fe6060f1SDimitry Andric // Most functionality in this file was swiped from llvm/Support/Compiler.h. 12fe6060f1SDimitry Andric // 13fe6060f1SDimitry Andric //===----------------------------------------------------------------------===// 14fe6060f1SDimitry Andric 15fe6060f1SDimitry Andric #ifndef ORC_RT_COMPILER_H 16fe6060f1SDimitry Andric #define ORC_RT_COMPILER_H 17fe6060f1SDimitry Andric 18*bdd1243dSDimitry Andric #if defined(_WIN32) 19*bdd1243dSDimitry Andric #define ORC_RT_INTERFACE extern "C" 20*bdd1243dSDimitry Andric #define ORC_RT_HIDDEN 21*bdd1243dSDimitry Andric #define ORC_RT_IMPORT extern "C" __declspec(dllimport) 22*bdd1243dSDimitry Andric #else 23fe6060f1SDimitry Andric #define ORC_RT_INTERFACE extern "C" __attribute__((visibility("default"))) 24fe6060f1SDimitry Andric #define ORC_RT_HIDDEN __attribute__((visibility("hidden"))) 25*bdd1243dSDimitry Andric #define ORC_RT_IMPORT extern "C" 26*bdd1243dSDimitry Andric #endif 27fe6060f1SDimitry Andric 28fe6060f1SDimitry Andric #ifndef __has_builtin 29fe6060f1SDimitry Andric # define __has_builtin(x) 0 30fe6060f1SDimitry Andric #endif 31fe6060f1SDimitry Andric 32fe6060f1SDimitry Andric // Only use __has_cpp_attribute in C++ mode. GCC defines __has_cpp_attribute in 33fe6060f1SDimitry Andric // C mode, but the :: in __has_cpp_attribute(scoped::attribute) is invalid. 34fe6060f1SDimitry Andric #ifndef ORC_RT_HAS_CPP_ATTRIBUTE 35fe6060f1SDimitry Andric #if defined(__cplusplus) && defined(__has_cpp_attribute) 36fe6060f1SDimitry Andric #define ORC_RT_HAS_CPP_ATTRIBUTE(x) __has_cpp_attribute(x) 37fe6060f1SDimitry Andric #else 38fe6060f1SDimitry Andric #define ORC_RT_HAS_CPP_ATTRIBUTE(x) 0 39fe6060f1SDimitry Andric #endif 40fe6060f1SDimitry Andric #endif 41fe6060f1SDimitry Andric 42fe6060f1SDimitry Andric // Use the 'nodiscard' attribute in C++17 or newer mode. 43fe6060f1SDimitry Andric #if defined(__cplusplus) && __cplusplus > 201402L && \ 44fe6060f1SDimitry Andric ORC_RT_HAS_CPP_ATTRIBUTE(nodiscard) 45fe6060f1SDimitry Andric #define ORC_RT_NODISCARD [[nodiscard]] 46fe6060f1SDimitry Andric #elif ORC_RT_HAS_CPP_ATTRIBUTE(clang::warn_unused_result) 47fe6060f1SDimitry Andric #define ORC_RT_NODISCARD [[clang::warn_unused_result]] 48fe6060f1SDimitry Andric // Clang in C++14 mode claims that it has the 'nodiscard' attribute, but also 49fe6060f1SDimitry Andric // warns in the pedantic mode that 'nodiscard' is a C++17 extension (PR33518). 50fe6060f1SDimitry Andric // Use the 'nodiscard' attribute in C++14 mode only with GCC. 51fe6060f1SDimitry Andric // TODO: remove this workaround when PR33518 is resolved. 52fe6060f1SDimitry Andric #elif defined(__GNUC__) && ORC_RT_HAS_CPP_ATTRIBUTE(nodiscard) 53fe6060f1SDimitry Andric #define ORC_RT_NODISCARD [[nodiscard]] 54fe6060f1SDimitry Andric #else 55fe6060f1SDimitry Andric #define ORC_RT_NODISCARD 56fe6060f1SDimitry Andric #endif 57fe6060f1SDimitry Andric 58fe6060f1SDimitry Andric #if __has_builtin(__builtin_expect) 59fe6060f1SDimitry Andric #define ORC_RT_LIKELY(EXPR) __builtin_expect((bool)(EXPR), true) 60fe6060f1SDimitry Andric #define ORC_RT_UNLIKELY(EXPR) __builtin_expect((bool)(EXPR), false) 61fe6060f1SDimitry Andric #else 62fe6060f1SDimitry Andric #define ORC_RT_LIKELY(EXPR) (EXPR) 63fe6060f1SDimitry Andric #define ORC_RT_UNLIKELY(EXPR) (EXPR) 64fe6060f1SDimitry Andric #endif 65fe6060f1SDimitry Andric 66*bdd1243dSDimitry Andric #if defined(__APPLE__) 67fe6060f1SDimitry Andric #define ORC_RT_WEAK_IMPORT __attribute__((weak_import)) 68*bdd1243dSDimitry Andric #elif defined(_WIN32) 69*bdd1243dSDimitry Andric #define ORC_RT_WEAK_IMPORT 70fe6060f1SDimitry Andric #else 71fe6060f1SDimitry Andric #define ORC_RT_WEAK_IMPORT __attribute__((weak)) 72fe6060f1SDimitry Andric #endif 73fe6060f1SDimitry Andric 74fe6060f1SDimitry Andric #endif // ORC_RT_COMPILER_H 75