|
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
|
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */ |
|
3 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
4 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
6 |
|
7 /* mfbt foundational types and macros. */ |
|
8 |
|
9 #ifndef mozilla_Types_h |
|
10 #define mozilla_Types_h |
|
11 |
|
12 /* |
|
13 * This header must be valid C and C++, includable by code embedding either |
|
14 * SpiderMonkey or Gecko. |
|
15 */ |
|
16 |
|
17 /* Expose all <stdint.h> types and size_t. */ |
|
18 #include <stddef.h> |
|
19 #include <stdint.h> |
|
20 |
|
21 /* Implement compiler and linker macros needed for APIs. */ |
|
22 |
|
23 /* |
|
24 * MOZ_EXPORT is used to declare and define a symbol or type which is externally |
|
25 * visible to users of the current library. It encapsulates various decorations |
|
26 * needed to properly export the method's symbol. |
|
27 * |
|
28 * api.h: |
|
29 * extern MOZ_EXPORT int MeaningOfLife(void); |
|
30 * extern MOZ_EXPORT int LuggageCombination; |
|
31 * |
|
32 * api.c: |
|
33 * int MeaningOfLife(void) { return 42; } |
|
34 * int LuggageCombination = 12345; |
|
35 * |
|
36 * If you are merely sharing a method across files, just use plain |extern|. |
|
37 * These macros are designed for use by library interfaces -- not for normal |
|
38 * methods or data used cross-file. |
|
39 */ |
|
40 #if defined(WIN32) |
|
41 # define MOZ_EXPORT __declspec(dllexport) |
|
42 #else /* Unix */ |
|
43 # ifdef HAVE_VISIBILITY_ATTRIBUTE |
|
44 # define MOZ_EXPORT __attribute__((visibility("default"))) |
|
45 # elif defined(__SUNPRO_C) || defined(__SUNPRO_CC) |
|
46 # define MOZ_EXPORT __global |
|
47 # else |
|
48 # define MOZ_EXPORT /* nothing */ |
|
49 # endif |
|
50 #endif |
|
51 |
|
52 |
|
53 /* |
|
54 * Whereas implementers use MOZ_EXPORT to declare and define library symbols, |
|
55 * users use MOZ_IMPORT_API and MOZ_IMPORT_DATA to access them. Most often the |
|
56 * implementer of the library will expose an API macro which expands to either |
|
57 * the export or import version of the macro, depending upon the compilation |
|
58 * mode. |
|
59 */ |
|
60 #ifdef _WIN32 |
|
61 # if defined(__MWERKS__) |
|
62 # define MOZ_IMPORT_API /* nothing */ |
|
63 # else |
|
64 # define MOZ_IMPORT_API __declspec(dllimport) |
|
65 # endif |
|
66 #else |
|
67 # define MOZ_IMPORT_API MOZ_EXPORT |
|
68 #endif |
|
69 |
|
70 #if defined(_WIN32) && !defined(__MWERKS__) |
|
71 # define MOZ_IMPORT_DATA __declspec(dllimport) |
|
72 #else |
|
73 # define MOZ_IMPORT_DATA MOZ_EXPORT |
|
74 #endif |
|
75 |
|
76 /* |
|
77 * Consistent with the above comment, the MFBT_API and MFBT_DATA macros expose |
|
78 * export mfbt declarations when building mfbt, and they expose import mfbt |
|
79 * declarations when using mfbt. |
|
80 */ |
|
81 #if defined(IMPL_MFBT) |
|
82 # define MFBT_API MOZ_EXPORT |
|
83 # define MFBT_DATA MOZ_EXPORT |
|
84 #else |
|
85 /* |
|
86 * On linux mozglue is linked in the program and we link libxul.so with |
|
87 * -z,defs. Normally that causes the linker to reject undefined references in |
|
88 * libxul.so, but as a loophole it allows undefined references to weak |
|
89 * symbols. We add the weak attribute to the import version of the MFBT API |
|
90 * macros to exploit this. |
|
91 */ |
|
92 # if defined(MOZ_GLUE_IN_PROGRAM) |
|
93 # define MFBT_API __attribute__((weak)) MOZ_IMPORT_API |
|
94 # define MFBT_DATA __attribute__((weak)) MOZ_IMPORT_DATA |
|
95 # else |
|
96 # define MFBT_API MOZ_IMPORT_API |
|
97 # define MFBT_DATA MOZ_IMPORT_DATA |
|
98 # endif |
|
99 #endif |
|
100 |
|
101 /* |
|
102 * C symbols in C++ code must be declared immediately within |extern "C"| |
|
103 * blocks. However, in C code, they need not be declared specially. This |
|
104 * difference is abstracted behind the MOZ_BEGIN_EXTERN_C and MOZ_END_EXTERN_C |
|
105 * macros, so that the user need not know whether he is being used in C or C++ |
|
106 * code. |
|
107 * |
|
108 * MOZ_BEGIN_EXTERN_C |
|
109 * |
|
110 * extern MOZ_EXPORT int MostRandomNumber(void); |
|
111 * ...other declarations... |
|
112 * |
|
113 * MOZ_END_EXTERN_C |
|
114 * |
|
115 * This said, it is preferable to just use |extern "C"| in C++ header files for |
|
116 * its greater clarity. |
|
117 */ |
|
118 #ifdef __cplusplus |
|
119 # define MOZ_BEGIN_EXTERN_C extern "C" { |
|
120 # define MOZ_END_EXTERN_C } |
|
121 #else |
|
122 # define MOZ_BEGIN_EXTERN_C |
|
123 # define MOZ_END_EXTERN_C |
|
124 #endif |
|
125 |
|
126 /* |
|
127 * GCC's typeof is available when decltype is not. |
|
128 */ |
|
129 #if defined(__GNUC__) && defined(__cplusplus) && \ |
|
130 !defined(__GXX_EXPERIMENTAL_CXX0X__) && __cplusplus < 201103L |
|
131 # define decltype __typeof__ |
|
132 #endif |
|
133 |
|
134 #endif /* mozilla_Types_h */ |