Backed out changeset 3786755b89c0 (bug 920495) for landing with the wrong bug # in...
[gecko.git] / mfbt / Types.h
blob5340b2b60013bc92b6f509c67ba29e0e2ca5aa2e
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/. */
7 /* mfbt foundational types and macros. */
9 #ifndef mozilla_Types_h
10 #define mozilla_Types_h
13 * This header must be valid C and C++, includable by code embedding either
14 * SpiderMonkey or Gecko.
17 /* Expose all <stdint.h> types and size_t. */
18 #include <stddef.h>
19 #include <stdint.h>
21 /* Implement compiler and linker macros needed for APIs. */
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.
28 * api.h:
29 * extern MOZ_EXPORT int MeaningOfLife(void);
30 * extern MOZ_EXPORT int LuggageCombination;
32 * api.c:
33 * int MeaningOfLife(void) { return 42; }
34 * int LuggageCombination = 12345;
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.
40 #if defined(WIN32) || defined(XP_OS2)
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
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.
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 #elif defined(XP_OS2)
67 # define MOZ_IMPORT_API __declspec(dllimport)
68 #else
69 # define MOZ_IMPORT_API MOZ_EXPORT
70 #endif
72 #if defined(_WIN32) && !defined(__MWERKS__)
73 # define MOZ_IMPORT_DATA __declspec(dllimport)
74 #elif defined(XP_OS2)
75 # define MOZ_IMPORT_DATA __declspec(dllimport)
76 #else
77 # define MOZ_IMPORT_DATA MOZ_EXPORT
78 #endif
81 * Consistent with the above comment, the MFBT_API and MFBT_DATA macros expose
82 * export mfbt declarations when building mfbt, and they expose import mfbt
83 * declarations when using mfbt.
85 #if defined(IMPL_MFBT)
86 # define MFBT_API MOZ_EXPORT
87 # define MFBT_DATA MOZ_EXPORT
88 #else
90 * On linux mozglue is linked in the program and we link libxul.so with
91 * -z,defs. Normally that causes the linker to reject undefined references in
92 * libxul.so, but as a loophole it allows undefined references to weak
93 * symbols. We add the weak attribute to the import version of the MFBT API
94 * macros to exploit this.
96 # if defined(MOZ_GLUE_IN_PROGRAM)
97 # define MFBT_API __attribute__((weak)) MOZ_IMPORT_API
98 # define MFBT_DATA __attribute__((weak)) MOZ_IMPORT_DATA
99 # else
100 # define MFBT_API MOZ_IMPORT_API
101 # define MFBT_DATA MOZ_IMPORT_DATA
102 # endif
103 #endif
106 * C symbols in C++ code must be declared immediately within |extern "C"|
107 * blocks. However, in C code, they need not be declared specially. This
108 * difference is abstracted behind the MOZ_BEGIN_EXTERN_C and MOZ_END_EXTERN_C
109 * macros, so that the user need not know whether he is being used in C or C++
110 * code.
112 * MOZ_BEGIN_EXTERN_C
114 * extern MOZ_EXPORT int MostRandomNumber(void);
115 * ...other declarations...
117 * MOZ_END_EXTERN_C
119 * This said, it is preferable to just use |extern "C"| in C++ header files for
120 * its greater clarity.
122 #ifdef __cplusplus
123 # define MOZ_BEGIN_EXTERN_C extern "C" {
124 # define MOZ_END_EXTERN_C }
125 #else
126 # define MOZ_BEGIN_EXTERN_C
127 # define MOZ_END_EXTERN_C
128 #endif
131 * GCC's typeof is available when decltype is not.
133 #if defined(__GNUC__) && defined(__cplusplus) && \
134 !defined(__GXX_EXPERIMENTAL_CXX0X__) && __cplusplus < 201103L
135 # define decltype __typeof__
136 #endif
138 #endif /* mozilla_Types_h */