Compiler.h revision 212904
1//===-- llvm/Support/Compiler.h - Compiler abstraction support --*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines several macros, based on the current compiler.  This allows
11// use of compiler-specific features in a way that remains portable.
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_SUPPORT_COMPILER_H
16#define LLVM_SUPPORT_COMPILER_H
17
18/// LLVM_LIBRARY_VISIBILITY - If a class marked with this attribute is linked
19/// into a shared library, then the class should be private to the library and
20/// not accessible from outside it.  Can also be used to mark variables and
21/// functions, making them private to any shared library they are linked into.
22
23/// LLVM_GLOBAL_VISIBILITY - If a class marked with this attribute is linked
24/// into a shared library, then the class will be accessible from outside the
25/// the library.  Can also be used to mark variables and functions, making them
26/// accessible from outside any shared library they are linked into.
27#if defined(__MINGW32__) || defined(__CYGWIN__)
28#define LLVM_LIBRARY_VISIBILITY
29#define LLVM_GLOBAL_VISIBILITY __declspec(dllexport)
30#elif (__GNUC__ >= 4)
31#define LLVM_LIBRARY_VISIBILITY __attribute__ ((visibility("hidden")))
32#define LLVM_GLOBAL_VISIBILITY __attribute__ ((visibility("default")))
33#else
34#define LLVM_LIBRARY_VISIBILITY
35#define LLVM_GLOBAL_VISIBILITY
36#endif
37
38#if (__GNUC__ >= 4 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 1))
39#define ATTRIBUTE_USED __attribute__((__used__))
40#else
41#define ATTRIBUTE_USED
42#endif
43
44#if (__GNUC__ >= 4 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 1))
45#define ATTRIBUTE_UNUSED __attribute__((__unused__))
46#else
47#define ATTRIBUTE_UNUSED
48#endif
49
50#ifdef __GNUC__ // aka 'ATTRIBUTE_CONST' but following LLVM Conventions.
51#define ATTRIBUTE_READNONE __attribute__((__const__))
52#else
53#define ATTRIBUTE_READNONE
54#endif
55
56#ifdef __GNUC__  // aka 'ATTRIBUTE_PURE' but following LLVM Conventions.
57#define ATTRIBUTE_READONLY __attribute__((__pure__))
58#else
59#define ATTRIBUTE_READONLY
60#endif
61
62#if (__GNUC__ >= 4)
63#define BUILTIN_EXPECT(EXPR, VALUE) __builtin_expect((EXPR), (VALUE))
64#else
65#define BUILTIN_EXPECT(EXPR, VALUE) (EXPR)
66#endif
67
68// C++ doesn't support 'extern template' of template specializations.  GCC does,
69// but requires __extension__ before it.  In the header, use this:
70//   EXTERN_TEMPLATE_INSTANTIATION(class foo<bar>);
71// in the .cpp file, use this:
72//   TEMPLATE_INSTANTIATION(class foo<bar>);
73#ifdef __GNUC__
74#define EXTERN_TEMPLATE_INSTANTIATION(X) __extension__ extern template X
75#define TEMPLATE_INSTANTIATION(X) template X
76#else
77#define EXTERN_TEMPLATE_INSTANTIATION(X)
78#define TEMPLATE_INSTANTIATION(X)
79#endif
80
81// DISABLE_INLINE - On compilers where we have a directive to do so, mark a
82// method "not for inlining".
83#if (__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4))
84#define DISABLE_INLINE __attribute__((noinline))
85#elif defined(_MSC_VER)
86#define DISABLE_INLINE __declspec(noinline)
87#else
88#define DISABLE_INLINE
89#endif
90
91// ALWAYS_INLINE - On compilers where we have a directive to do so, mark a
92// method "always inline" because it is performance sensitive.
93// GCC 3.4 supported this but is buggy in various cases and produces
94// unimplemented errors, just use it in GCC 4.0 and later.
95#if __GNUC__ > 3
96#define ALWAYS_INLINE __attribute__((always_inline))
97#else
98// TODO: No idea how to do this with MSVC.
99#define ALWAYS_INLINE
100#endif
101
102
103#ifdef __GNUC__
104#define NORETURN __attribute__((noreturn))
105#elif defined(_MSC_VER)
106#define NORETURN __declspec(noreturn)
107#else
108#define NORETURN
109#endif
110
111#endif
112