mfbt/Types.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mfbt/Types.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,134 @@
     1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* vim: set ts=8 sts=2 et sw=2 tw=80: */
     1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.9 +
    1.10 +/* mfbt foundational types and macros. */
    1.11 +
    1.12 +#ifndef mozilla_Types_h
    1.13 +#define mozilla_Types_h
    1.14 +
    1.15 +/*
    1.16 + * This header must be valid C and C++, includable by code embedding either
    1.17 + * SpiderMonkey or Gecko.
    1.18 + */
    1.19 +
    1.20 +/* Expose all <stdint.h> types and size_t. */
    1.21 +#include <stddef.h>
    1.22 +#include <stdint.h>
    1.23 +
    1.24 +/* Implement compiler and linker macros needed for APIs. */
    1.25 +
    1.26 +/*
    1.27 + * MOZ_EXPORT is used to declare and define a symbol or type which is externally
    1.28 + * visible to users of the current library.  It encapsulates various decorations
    1.29 + * needed to properly export the method's symbol.
    1.30 + *
    1.31 + *   api.h:
    1.32 + *     extern MOZ_EXPORT int MeaningOfLife(void);
    1.33 + *     extern MOZ_EXPORT int LuggageCombination;
    1.34 + *
    1.35 + *   api.c:
    1.36 + *     int MeaningOfLife(void) { return 42; }
    1.37 + *     int LuggageCombination = 12345;
    1.38 + *
    1.39 + * If you are merely sharing a method across files, just use plain |extern|.
    1.40 + * These macros are designed for use by library interfaces -- not for normal
    1.41 + * methods or data used cross-file.
    1.42 + */
    1.43 +#if defined(WIN32)
    1.44 +#  define MOZ_EXPORT   __declspec(dllexport)
    1.45 +#else /* Unix */
    1.46 +#  ifdef HAVE_VISIBILITY_ATTRIBUTE
    1.47 +#    define MOZ_EXPORT       __attribute__((visibility("default")))
    1.48 +#  elif defined(__SUNPRO_C) || defined(__SUNPRO_CC)
    1.49 +#    define MOZ_EXPORT      __global
    1.50 +#  else
    1.51 +#    define MOZ_EXPORT /* nothing */
    1.52 +#  endif
    1.53 +#endif
    1.54 +
    1.55 +
    1.56 +/*
    1.57 + * Whereas implementers use MOZ_EXPORT to declare and define library symbols,
    1.58 + * users use MOZ_IMPORT_API and MOZ_IMPORT_DATA to access them.  Most often the
    1.59 + * implementer of the library will expose an API macro which expands to either
    1.60 + * the export or import version of the macro, depending upon the compilation
    1.61 + * mode.
    1.62 + */
    1.63 +#ifdef _WIN32
    1.64 +#  if defined(__MWERKS__)
    1.65 +#    define MOZ_IMPORT_API /* nothing */
    1.66 +#  else
    1.67 +#    define MOZ_IMPORT_API __declspec(dllimport)
    1.68 +#  endif
    1.69 +#else
    1.70 +#  define MOZ_IMPORT_API MOZ_EXPORT
    1.71 +#endif
    1.72 +
    1.73 +#if defined(_WIN32) && !defined(__MWERKS__)
    1.74 +#  define MOZ_IMPORT_DATA  __declspec(dllimport)
    1.75 +#else
    1.76 +#  define MOZ_IMPORT_DATA  MOZ_EXPORT
    1.77 +#endif
    1.78 +
    1.79 +/*
    1.80 + * Consistent with the above comment, the MFBT_API and MFBT_DATA macros expose
    1.81 + * export mfbt declarations when building mfbt, and they expose import mfbt
    1.82 + * declarations when using mfbt.
    1.83 + */
    1.84 +#if defined(IMPL_MFBT)
    1.85 +#  define MFBT_API     MOZ_EXPORT
    1.86 +#  define MFBT_DATA    MOZ_EXPORT
    1.87 +#else
    1.88 +  /*
    1.89 +   * On linux mozglue is linked in the program and we link libxul.so with
    1.90 +   * -z,defs. Normally that causes the linker to reject undefined references in
    1.91 +   * libxul.so, but as a loophole it allows undefined references to weak
    1.92 +   * symbols. We add the weak attribute to the import version of the MFBT API
    1.93 +   * macros to exploit this.
    1.94 +   */
    1.95 +#  if defined(MOZ_GLUE_IN_PROGRAM)
    1.96 +#    define MFBT_API   __attribute__((weak)) MOZ_IMPORT_API
    1.97 +#    define MFBT_DATA  __attribute__((weak)) MOZ_IMPORT_DATA
    1.98 +#  else
    1.99 +#    define MFBT_API   MOZ_IMPORT_API
   1.100 +#    define MFBT_DATA  MOZ_IMPORT_DATA
   1.101 +#  endif
   1.102 +#endif
   1.103 +
   1.104 +/*
   1.105 + * C symbols in C++ code must be declared immediately within |extern "C"|
   1.106 + * blocks.  However, in C code, they need not be declared specially.  This
   1.107 + * difference is abstracted behind the MOZ_BEGIN_EXTERN_C and MOZ_END_EXTERN_C
   1.108 + * macros, so that the user need not know whether he is being used in C or C++
   1.109 + * code.
   1.110 + *
   1.111 + *   MOZ_BEGIN_EXTERN_C
   1.112 + *
   1.113 + *   extern MOZ_EXPORT int MostRandomNumber(void);
   1.114 + *   ...other declarations...
   1.115 + *
   1.116 + *   MOZ_END_EXTERN_C
   1.117 + *
   1.118 + * This said, it is preferable to just use |extern "C"| in C++ header files for
   1.119 + * its greater clarity.
   1.120 + */
   1.121 +#ifdef __cplusplus
   1.122 +#  define MOZ_BEGIN_EXTERN_C    extern "C" {
   1.123 +#  define MOZ_END_EXTERN_C      }
   1.124 +#else
   1.125 +#  define MOZ_BEGIN_EXTERN_C
   1.126 +#  define MOZ_END_EXTERN_C
   1.127 +#endif
   1.128 +
   1.129 +/*
   1.130 + * GCC's typeof is available when decltype is not.
   1.131 + */
   1.132 +#if defined(__GNUC__) && defined(__cplusplus) && \
   1.133 +  !defined(__GXX_EXPERIMENTAL_CXX0X__) && __cplusplus < 201103L
   1.134 +#  define decltype __typeof__
   1.135 +#endif
   1.136 +
   1.137 +#endif /* mozilla_Types_h */

mercurial