DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Implementation

Mercurial (56e7b9127e89)

VCS Links

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef nsGkAtoms_h___
#define nsGkAtoms_h___

#include "nsAtom.h"

// Static atoms are structured carefully to satisfy a lot of constraints.
//
// - We have ~2300 static atoms.
//
// - We want them to be constexpr so they end up in .rodata, and thus shared
//   between processes, minimizing memory usage.
//
// - We need them to be in an array, so we can iterate over them (for
//   registration and lookups).
//
// - Each static atom has a string literal associated with it. We can't use a
//   pointer to the string literal because then the atoms won't end up in
//   .rodata. Therefore the string literals and the atoms must be arranged in a
//   way such that a numeric index can be used instead. This numeric index
//   (nsStaticAtom::mStringOffset) must be computable at compile-time to keep
//   the static atom constexpr. It should also not be too large (a uint32_t is
//   reasonable).
//
// - Each static atom stores the hash value of its associated string literal;
//   it's used in various ways. The hash value must be computed at
//   compile-time, to keep the static atom constexpr.
//
// - As well as accessing each static atom via array indexing, we need an
//   individual pointer, e.g. nsGkAtoms::foo. Ideally this would be constexpr
//   so it doesn't take up any space in memory. Unfortunately MSVC's constexpr
//   support is buggy and so this isn't possible yet. See bug 1449787.
//
// - The array of static atoms can't be in a .h file, because it's a huge
//   constexpr expression, which would blow out compile times. But the
//   individual pointers for the static atoms must be in a .h file so they are
//   public.
//
// nsGkAtoms below defines static atoms in a way that satisfies these
// constraints. It uses nsGkAtomList.h, which defines the names and values of
// the atoms.
//
// nsGkAtomList.h is generated by StaticAtoms.py and has entries that look
// like this:
//
//   GK_ATOM(one, "one", 0x01234567, nsStaticAtom, Atom)
//   GK_ATOM(two, "two", 0x12345678, nsICSSPseudoElement, PseudoElementAtom)
//   GK_ATOM(three, "three", 0x23456789, nsICSSAnonBoxPseudo, InheritingAnonBoxAtom)
//
// After macro expansion, the atom definitions look like the following:
//
//   ====> nsGkAtoms.h <====
//
//   namespace mozilla {
//   namespace detail {
//
//   struct GkAtoms
//   {
//     // The declaration of each atom's string.
//     const char16_t one_string[sizeof("one")];
//     const char16_t two_string[sizeof("two")];
//     const char16_t three_string[sizeof("three")];
//
//     // The enum value for each atom.
//     enum class Atoms {
//       one_,
//       two_,
//       three_,
//       AtomsCount
//     };
//
//     const nsStaticAtom mAtoms[static_cast<size_t>(Atoms::AtomsCount)];
//   };
//
//   } // namespace detail
//   } // namespace mozilla
//
//   // This class holds the pointers to the individual atoms.
//   class nsGkAtoms
//   {
//   private:
//     // This is a useful handle to the array of atoms, used below and also
//     // possibly by Rust code.
//     static const nsStaticAtom* const sAtoms;
//
//     // The number of atoms, used below.
//     static constexpr size_t sAtomsLen =
//       static_cast<size_t>(detail::MyAtoms::Atoms::AtomsCount);
//
//   public:
//     // These types are not `nsStaticAtom* const`, etc. -- even though these
//     // atoms are immutable -- because they are often passed to functions with
//     // `nsAtom*` parameters, i.e. that can be passed both dynamic and
//     // static.
//     static nsStaticAtom* one;
//     static nsICSSPseudoElement* two;
//     static nsICSSAnonBoxPseudo* three;
//   };
//
//   ====> nsGkAtoms.cpp <====
//
//   namespace mozilla {
//   namespace detail {
//
//   // Need to suppress some MSVC warning weirdness with WrappingMultiply().
//   MOZ_PUSH_DISABLE_INTEGRAL_CONSTANT_OVERFLOW_WARNING
//   // Because this is `constexpr` it ends up in read-only memory where it can
//   // be shared between processes.
//   static constexpr GkAtoms gGkAtoms = {
//     // The initialization of each atom's string.
//     u"one",
//     u"two",
//     u"three",
//     {
//       // The initialization of the atoms themselves.
//       nsStaticAtom(
//         u"one", 3,
//         0x01234567,
//         offsetof(GkAtoms, mAtoms[static_cast<size_t>(GkAtoms::Atoms::one)]) -
//         offsetof(GkAtoms, one_string)),
//       nsStaticAtom(
//         u"two", 3,
//         0x12345678,
//         offsetof(GkAtoms, mAtoms[static_cast<size_t>(GkAtoms::Atoms::two)]) -
//         offsetof(GkAtoms, two_string)),
//       nsStaticAtom(
//         u"three", 3,
//         0x23456789,
//         offsetof(GkAtoms, mAtoms[static_cast<size_t>(GkAtoms::Atoms::three)]) -
//         offsetof(GkAtoms, three_string)),
//     }
//   };
//   MOZ_POP_DISABLE_INTEGRAL_CONSTANT_OVERFLOW_WARNING
//
//   } // namespace detail
//   } // namespace mozilla
//
//   const nsStaticAtom* const nsGkAtoms::sAtoms =
//     mozilla::detail::gGkAtoms.mAtoms;
//
//   // Definition of the pointer to the static atom.
//   nsStaticAtom* nsGkAtoms::one =
//     const_cast<nsStaticAtom*>(static_cast<const nsStaticAtom*>(
//       &detail::gGkAtoms.mAtoms[
//         static_cast<size_t>(detail::GkAtoms::Atoms::one)]);
//   nsICSSPseudoElement* nsGkAtoms::two =
//     const_cast<nsICSSPseudoElement*>(static_cast<const nsICSSPseudoElement*>(
//       &detail::gGkAtoms.mAtoms[
//         static_cast<size_t>(detail::GkAtoms::Atoms::two)]);
//   nsICSSAnonBoxPseudo* nsGkAtoms::three =
//     const_cast<nsICSSAnonBoxPseudo*>(static_cast<const nsICSSAnonBoxPseudo*>(
//       &detail::gGkAtoms.mAtoms[
//         static_cast<size_t>(detail::GkAtoms::Atoms::three)]);

// Trivial subclasses of nsStaticAtom so that function signatures can require
// an atom from a specific atom list.
#define DEFINE_STATIC_ATOM_SUBCLASS(name_)                   \
  class name_ : public nsStaticAtom                          \
  {                                                          \
  public:                                                    \
    constexpr name_(const char16_t* aStr, uint32_t aLength,  \
                    uint32_t aHash, uint32_t aOffset)        \
      : nsStaticAtom(aStr, aLength, aHash, aOffset) {}       \
  };

DEFINE_STATIC_ATOM_SUBCLASS(nsICSSAnonBoxPseudo)
DEFINE_STATIC_ATOM_SUBCLASS(nsICSSPseudoElement)

#undef DEFINE_STATIC_ATOM_SUBCLASS

namespace mozilla {
namespace detail {

// This `detail` class contains the atom strings and the atom objects.
// Because they are together in a class, the mStringOffset field of the
// atoms will be small and can be initialized at compile time.
//
// A `detail` namespace is used because the things within it aren't directly
// referenced by external users of these static atoms.
struct GkAtoms
{
  // The declaration of each atom's string.
  #define GK_ATOM(name_, value_, hash_, type_, atom_type_) \
    const char16_t name_##_string[sizeof(value_)];
  #include "nsGkAtomList.h"
  #undef GK_ATOM

  // The enum value for each atom.
  enum class Atoms {
    #define GK_ATOM(name_, value_, hash_, type_, atom_type_) \
      name_,
    #include "nsGkAtomList.h"
    #undef GK_ATOM
    AtomsCount
  };

  const nsStaticAtom mAtoms[static_cast<size_t>(Atoms::AtomsCount)];
};

} // namespace detail
} // namespace mozilla

class nsGkAtoms
{
private:
  static const nsStaticAtom* const sAtoms;
  static constexpr size_t sAtomsLen =
    static_cast<size_t>(mozilla::detail::GkAtoms::Atoms::AtomsCount);

public:
  static void RegisterStaticAtoms();

  static nsStaticAtom* GetAtomByIndex(size_t aIndex)
  {
    MOZ_ASSERT(aIndex < sAtomsLen);
    return const_cast<nsStaticAtom*>(&sAtoms[aIndex]);
  }

  // The declaration of the pointer to each static atom.
  //
  // XXX: Eventually this should be combined with its definition and the
  // pointer should be made `constexpr`. See bug 1449787.
  #define GK_ATOM(name_, value_, hash_, type_, atom_type_) \
    static type_* name_;
  #include "nsGkAtomList.h"
  #undef GK_ATOM
};

#endif /* nsGkAtoms_h___ */