Types.h (4423B)
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 20 /* Implement compiler and linker macros needed for APIs. */ 21 22 /* 23 * MOZ_EXPORT is used to declare and define a symbol or type which is externally 24 * visible to users of the current library. It encapsulates various decorations 25 * needed to properly export the method's symbol. 26 * 27 * api.h: 28 * extern MOZ_EXPORT int MeaningOfLife(void); 29 * extern MOZ_EXPORT int LuggageCombination; 30 * 31 * api.c: 32 * int MeaningOfLife(void) { return 42; } 33 * int LuggageCombination = 12345; 34 * 35 * If you are merely sharing a method across files, just use plain |extern|. 36 * These macros are designed for use by library interfaces -- not for normal 37 * methods or data used cross-file. 38 */ 39 #if defined(WIN32) 40 # define MOZ_EXPORT __declspec(dllexport) 41 #else /* Unix */ 42 # ifdef HAVE_VISIBILITY_ATTRIBUTE 43 # define MOZ_EXPORT __attribute__((visibility("default"))) 44 # elif defined(__SUNPRO_C) || defined(__SUNPRO_CC) 45 # define MOZ_EXPORT __global 46 # else 47 # define MOZ_EXPORT /* nothing */ 48 # endif 49 #endif 50 51 /* 52 * Whereas implementers use MOZ_EXPORT to declare and define library symbols, 53 * users use MOZ_IMPORT_API and MOZ_IMPORT_DATA to access them. Most often the 54 * implementer of the library will expose an API macro which expands to either 55 * the export or import version of the macro, depending upon the compilation 56 * mode. 57 */ 58 #ifdef _WIN32 59 # if defined(__MWERKS__) 60 # define MOZ_IMPORT_API /* nothing */ 61 # else 62 # define MOZ_IMPORT_API __declspec(dllimport) 63 # endif 64 #else 65 # define MOZ_IMPORT_API MOZ_EXPORT 66 #endif 67 68 #if defined(_WIN32) && !defined(__MWERKS__) 69 # define MOZ_IMPORT_DATA __declspec(dllimport) 70 #else 71 # define MOZ_IMPORT_DATA MOZ_EXPORT 72 #endif 73 74 /* 75 * Consistent with the above comment, the MFBT_API and MFBT_DATA macros expose 76 * export mfbt declarations when building mfbt, and they expose import mfbt 77 * declarations when using mfbt. 78 */ 79 #if defined(IMPL_MFBT) || \ 80 (defined(JS_STANDALONE) && !defined(MOZ_MEMORY) && \ 81 (defined(EXPORT_JS_API) || defined(STATIC_EXPORTABLE_JS_API))) 82 # define MFBT_API MOZ_EXPORT 83 # define MFBT_DATA MOZ_EXPORT 84 #else 85 # if defined(JS_STANDALONE) && !defined(MOZ_MEMORY) && defined(STATIC_JS_API) 86 # define MFBT_API 87 # define MFBT_DATA 88 # else 89 /* 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. 95 */ 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 104 #endif 105 106 /* 107 * C symbols in C++ code must be declared immediately within |extern "C"| 108 * blocks. However, in C code, they need not be declared specially. This 109 * difference is abstracted behind the MOZ_BEGIN_EXTERN_C and MOZ_END_EXTERN_C 110 * macros, so that the user need not know whether he is being used in C or C++ 111 * code. 112 * 113 * MOZ_BEGIN_EXTERN_C 114 * 115 * extern MOZ_EXPORT int MostRandomNumber(void); 116 * ...other declarations... 117 * 118 * MOZ_END_EXTERN_C 119 * 120 * This said, it is preferable to just use |extern "C"| in C++ header files for 121 * its greater clarity. 122 */ 123 #ifdef __cplusplus 124 # define MOZ_BEGIN_EXTERN_C extern "C" { 125 # define MOZ_END_EXTERN_C } 126 #else 127 # define MOZ_BEGIN_EXTERN_C 128 # define MOZ_END_EXTERN_C 129 #endif 130 131 /* 132 * GCC's typeof is available when decltype is not. 133 */ 134 #if defined(__GNUC__) && defined(__cplusplus) && \ 135 !defined(__GXX_EXPERIMENTAL_CXX0X__) && __cplusplus < 201103L 136 # define decltype __typeof__ 137 #endif 138 139 #endif /* mozilla_Types_h */