| 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 |
* Whereas implementers use MOZ_EXPORT to declare and define library symbols, |
| 54 |
* users use MOZ_IMPORT_API and MOZ_IMPORT_DATA to access them. Most often the |
| 55 |
* implementer of the library will expose an API macro which expands to either |
| 56 |
* the export or import version of the macro, depending upon the compilation |
| 57 |
* mode. |
| 58 |
*/ |
| 59 |
#ifdef _WIN32 |
| 60 |
# if defined(__MWERKS__) |
| 61 |
# define MOZ_IMPORT_API /* nothing */ |
| 62 |
# else |
| 63 |
# define MOZ_IMPORT_API __declspec(dllimport) |
| 64 |
# endif |
| 65 |
#else |
| 66 |
# define MOZ_IMPORT_API MOZ_EXPORT |
| 67 |
#endif |
| 68 |
|
| 69 |
#if defined(_WIN32) && !defined(__MWERKS__) |
| 70 |
# define MOZ_IMPORT_DATA __declspec(dllimport) |
| 71 |
#else |
| 72 |
# define MOZ_IMPORT_DATA MOZ_EXPORT |
| 73 |
#endif |
| 74 |
|
| 75 |
/* |
| 76 |
* Consistent with the above comment, the MFBT_API and MFBT_DATA macros expose |
| 77 |
* export mfbt declarations when building mfbt, and they expose import mfbt |
| 78 |
* declarations when using mfbt. |
| 79 |
*/ |
| 80 |
#if defined(IMPL_MFBT) || \ |
| 81 |
(defined(JS_STANDALONE) && !defined(MOZ_MEMORY) && \ |
| 82 |
(defined(EXPORT_JS_API) || defined(STATIC_EXPORTABLE_JS_API))) |
| 83 |
# define MFBT_API MOZ_EXPORT |
| 84 |
# define MFBT_DATA MOZ_EXPORT |
| 85 |
#else |
| 86 |
# if defined(JS_STANDALONE) && !defined(MOZ_MEMORY) && defined(STATIC_JS_API) |
| 87 |
# define MFBT_API |
| 88 |
# define MFBT_DATA |
| 89 |
# else |
| 90 |
/* |
| 91 |
* On linux mozglue is linked in the program and we link libxul.so with |
| 92 |
* -z,defs. Normally that causes the linker to reject undefined references in |
| 93 |
* libxul.so, but as a loophole it allows undefined references to weak |
| 94 |
* symbols. We add the weak attribute to the import version of the MFBT API |
| 95 |
* macros to exploit this. |
| 96 |
*/ |
| 97 |
# if defined(MOZ_GLUE_IN_PROGRAM) |
| 98 |
# define MFBT_API __attribute__((weak)) MOZ_IMPORT_API |
| 99 |
# define MFBT_DATA __attribute__((weak)) MOZ_IMPORT_DATA |
| 100 |
# else |
| 101 |
# define MFBT_API MOZ_IMPORT_API |
| 102 |
# define MFBT_DATA MOZ_IMPORT_DATA |
| 103 |
# endif |
| 104 |
# endif |
| 105 |
#endif |
| 106 |
|
| 107 |
/* |
| 108 |
* C symbols in C++ code must be declared immediately within |extern "C"| |
| 109 |
* blocks. However, in C code, they need not be declared specially. This |
| 110 |
* difference is abstracted behind the MOZ_BEGIN_EXTERN_C and MOZ_END_EXTERN_C |
| 111 |
* macros, so that the user need not know whether he is being used in C or C++ |
| 112 |
* code. |
| 113 |
* |
| 114 |
* MOZ_BEGIN_EXTERN_C |
| 115 |
* |
| 116 |
* extern MOZ_EXPORT int MostRandomNumber(void); |
| 117 |
* ...other declarations... |
| 118 |
* |
| 119 |
* MOZ_END_EXTERN_C |
| 120 |
* |
| 121 |
* This said, it is preferable to just use |extern "C"| in C++ header files for |
| 122 |
* its greater clarity. |
| 123 |
*/ |
| 124 |
#ifdef __cplusplus |
| 125 |
# define MOZ_BEGIN_EXTERN_C extern "C" { |
| 126 |
# define MOZ_END_EXTERN_C } |
| 127 |
#else |
| 128 |
# define MOZ_BEGIN_EXTERN_C |
| 129 |
# define MOZ_END_EXTERN_C |
| 130 |
#endif |
| 131 |
|
| 132 |
/* |
| 133 |
* GCC's typeof is available when decltype is not. |
| 134 |
*/ |
| 135 |
#if defined(__GNUC__) && defined(__cplusplus) && \ |
| 136 |
!defined(__GXX_EXPERIMENTAL_CXX0X__) && __cplusplus < 201103L |
| 137 |
# define decltype __typeof__ |
| 138 |
#endif |
| 139 |
|
| 140 |
#endif /* mozilla_Types_h */ |