Bug 1890277: part 2) Add `require-trusted-types-for` directive to CSP parser, guarded...
[gecko.git] / mfbt / Types.h
blob47a9be2cc48d609ba2cae8bc3620747a4abc06a8
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)
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
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.
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
69 #if defined(_WIN32) && !defined(__MWERKS__)
70 # define MOZ_IMPORT_DATA __declspec(dllimport)
71 #else
72 # define MOZ_IMPORT_DATA MOZ_EXPORT
73 #endif
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.
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
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.
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
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.
114 * MOZ_BEGIN_EXTERN_C
116 * extern MOZ_EXPORT int MostRandomNumber(void);
117 * ...other declarations...
119 * MOZ_END_EXTERN_C
121 * This said, it is preferable to just use |extern "C"| in C++ header files for
122 * its greater clarity.
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
133 * GCC's typeof is available when decltype is not.
135 #if defined(__GNUC__) && defined(__cplusplus) && \
136 !defined(__GXX_EXPERIMENTAL_CXX0X__) && __cplusplus < 201103L
137 # define decltype __typeof__
138 #endif
140 #endif /* mozilla_Types_h */