Refactor geckolib atoms regen script

This commit is contained in:
Ravi Shankar 2016-09-28 15:02:47 +05:30 committed by Manish Goregaokar
parent c834e57f4d
commit fa06b922c7
2 changed files with 73 additions and 49 deletions

View file

@ -8,18 +8,27 @@ import re
import os import os
PRELUDE = """
/* 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/. */
/* Autogenerated file, DO NOT EDIT DIRECTLY */
"""[1:]
def gnu_symbolify(source, ident): def gnu_symbolify(source, ident):
return "_ZN" + str(len(source.CLASS)) + source.CLASS + str(len(ident)) + ident + "E" return "_ZN{}{}{}{}E".format(len(source.CLASS), source.CLASS, len(ident), ident)
def msvc64_symbolify(source, ident): def msvc64_symbolify(source, ident):
return "?" + ident + "@" + source.CLASS + "@@2PEAV" + source.TYPE + "@@EA" return "?{}@{}@@2PEAV{}@@EA".format(ident, source.CLASS, source.TYPE)
def msvc32_symbolify(source, ident): def msvc32_symbolify(source, ident):
# Prepend "\x01" to avoid LLVM prefixing the mangled name with "_". # Prepend "\x01" to avoid LLVM prefixing the mangled name with "_".
# See https://github.com/rust-lang/rust/issues/36097 # See https://github.com/rust-lang/rust/issues/36097
return "\\x01?" + ident + "@" + source.CLASS + "@@2PAV" + source.TYPE + "@@A" return "\\x01?{}@{}@@2PAV{}@@A".format(ident, source.CLASS, source.TYPE)
class GkAtomSource: class GkAtomSource:
@ -92,56 +101,71 @@ def collect_atoms(objdir):
atoms.append(Atom(source, result.group(1), result.group(2))) atoms.append(Atom(source, result.group(1), result.group(2)))
return atoms return atoms
PRELUDE = """
/* 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/. */
/* Autogenerated file, DO NOT EDIT DIRECTLY */ IMPORTS = ("\nuse gecko_bindings::structs::nsIAtom;"
"""[1:] "\nuse string_cache::Atom;\n\n")
ATOM_TEMPLATE = (" #[link_name = \"{link_name}\"]\n"
" pub static {name}: *mut {type};")
UNSAFE_STATIC = ("#[inline(always)]\n"
"pub fn unsafe_atom_from_static(ptr: *mut nsIAtom) -> Atom {\n"
" unsafe { Atom::from_static(ptr) }\n"
"}\n\n")
CFG_IF = '''
cfg_if! {{
if #[cfg(not(target_env = "msvc"))] {{
extern {{
{gnu}
}}
}} else if #[cfg(target_pointer_width = "64")] {{
extern {{
{msvc64}
}}
}} else {{
extern {{
{msvc32}
}}
}}
}}
'''
RULE_TEMPLATE = ('("{atom}") => '
'{{ $crate::string_cache::atom_macro::unsafe_atom_from_static'
'($crate::string_cache::atom_macro::{name} as *mut _) }};')
MACRO = '''
#[macro_export]
macro_rules! atom {{
{}
}}
'''
def write_atom_macro(atoms, file_name): def write_atom_macro(atoms, file_name):
ATOM_TEMPLATE = """ def get_symbols(func):
#[link_name = "{link_name}"] return '\n'.join([ATOM_TEMPLATE.format(name=atom.ident,
pub static {name}: *mut {type}; link_name=func(atom),
"""[1:] type=atom.type()) for atom in atoms])
def write_items(f, func):
f.write(" extern {\n")
for atom in atoms:
f.write(ATOM_TEMPLATE.format(name=atom.ident,
link_name=func(atom),
type=atom.type()))
f.write(" }\n")
with open(file_name, "wb") as f: with open(file_name, "wb") as f:
f.write(PRELUDE) f.write(PRELUDE)
f.write("use gecko_bindings::structs::nsIAtom;\n\n") f.write(IMPORTS)
f.write("use string_cache::Atom;\n\n")
for source in SOURCES: for source in SOURCES:
if source.TYPE != "nsIAtom": if source.TYPE != "nsIAtom":
f.write("pub enum {} {{}}\n\n".format(source.TYPE)) f.write("pub enum {} {{}}\n\n".format(source.TYPE))
f.write("""
#[inline(always)] pub fn unsafe_atom_from_static(ptr: *mut nsIAtom) -> Atom { f.write(UNSAFE_STATIC)
unsafe { Atom::from_static(ptr) }
}\n\n gnu_symbols = get_symbols(Atom.gnu_symbol)
""") msvc32_symbols = get_symbols(Atom.msvc32_symbol)
f.write("cfg_if! {\n") msvc64_symbols = get_symbols(Atom.msvc64_symbol)
f.write(" if #[cfg(not(target_env = \"msvc\"))] {\n") f.write(CFG_IF.format(gnu=gnu_symbols, msvc32=msvc32_symbols, msvc64=msvc64_symbols))
write_items(f, Atom.gnu_symbol)
f.write(" } else if #[cfg(target_pointer_width = \"64\")] {\n") macro_rules = [RULE_TEMPLATE.format(atom=atom.value, name=atom.ident) for atom in atoms]
write_items(f, Atom.msvc64_symbol) f.write(MACRO.format('\n'.join(macro_rules)))
f.write(" } else {\n")
write_items(f, Atom.msvc32_symbol)
f.write(" }\n")
f.write("}\n\n")
f.write("#[macro_export]\n")
f.write("macro_rules! atom {\n")
f.writelines(['("%s") => { $crate::string_cache::atom_macro::unsafe_atom_from_static(\
$crate::string_cache::atom_macro::%s as *mut _) };\n'
% (atom.value, atom.ident) for atom in atoms])
f.write("}\n")
PSEUDO_ELEMENT_HEADER = """ PSEUDO_ELEMENT_HEADER = """

View file

@ -3,21 +3,21 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/* Autogenerated file, DO NOT EDIT DIRECTLY */ /* Autogenerated file, DO NOT EDIT DIRECTLY */
use gecko_bindings::structs::nsIAtom;
use gecko_bindings::structs::nsIAtom;
use string_cache::Atom; use string_cache::Atom;
pub enum nsICSSPseudoElement {} pub enum nsICSSPseudoElement {}
pub enum nsICSSAnonBoxPseudo {} pub enum nsICSSAnonBoxPseudo {}
#[inline(always)]
#[inline(always)] pub fn unsafe_atom_from_static(ptr: *mut nsIAtom) -> Atom { pub fn unsafe_atom_from_static(ptr: *mut nsIAtom) -> Atom {
unsafe { Atom::from_static(ptr) } unsafe { Atom::from_static(ptr) }
} }
cfg_if! { cfg_if! {
if #[cfg(not(target_env = "msvc"))] { if #[cfg(not(target_env = "msvc"))] {
extern { extern {
#[link_name = "_ZN9nsGkAtoms6_emptyE"] #[link_name = "_ZN9nsGkAtoms6_emptyE"]