From e723f6b291f8f3c2f6dcbc10c9f5f98bf79f87d9 Mon Sep 17 00:00:00 2001 From: Xidorn Quan Date: Wed, 18 Apr 2018 19:39:51 +1000 Subject: [PATCH] Bug 1454831 - Generate nsCSSPropertyID.h from data file directly. r=emilio MozReview-Commit-ID: CcX2uzgjWFo --HG-- rename : layout/style/nsCSSPropertyID.h => layout/style/nsCSSPropertyID.h.in extra : rebase_source : 441f22dbe019f6ebc879fd21c0dc3d0f6f9cb63a --- layout/style/GenerateCSSPropertyID.py | 35 +++++++++++++++++ layout/style/ServoBindings.toml | 3 -- layout/style/moz.build | 10 ++++- ...nsCSSPropertyID.h => nsCSSPropertyID.h.in} | 39 ++++--------------- servo/components/style/build_gecko.rs | 39 +------------------ 5 files changed, 53 insertions(+), 73 deletions(-) create mode 100644 layout/style/GenerateCSSPropertyID.py rename layout/style/{nsCSSPropertyID.h => nsCSSPropertyID.h.in} (64%) diff --git a/layout/style/GenerateCSSPropertyID.py b/layout/style/GenerateCSSPropertyID.py new file mode 100644 index 000000000000..74ef47abd32e --- /dev/null +++ b/layout/style/GenerateCSSPropertyID.py @@ -0,0 +1,35 @@ +# 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/. + +import string + +def generate(output, template, dataFile): + with open(template, "r") as f: + template = string.Template(f.read()) + with open(dataFile, "r") as f: + data = eval(f.read()) + + longhand_count = 0 + shorthand_count = 0 + alias_count = 0 + property_ids = [] + for name, method, id, flags, pref, prototype in data: + if prototype != "alias": + if prototype == "longhand": + assert shorthand_count == 0 + longhand_count += 1 + else: + assert alias_count == 0 + shorthand_count += 1 + property_ids.append("eCSSProperty_{}".format(id)) + else: + alias_count += 1 + property_ids.append("eCSSPropertyAlias_{}".format(id[0])) + + output.write("/* THIS IS AN AUTOGENERATED FILE. DO NOT EDIT */\n\n") + output.write(template.substitute({ + "property_ids": "\n".join(" {},".format(p) for p in property_ids), + "longhand_count": property_ids[longhand_count], + "shorthand_count": property_ids[longhand_count + shorthand_count], + })) diff --git a/layout/style/ServoBindings.toml b/layout/style/ServoBindings.toml index ac1543e7770f..ca8c811ad791 100644 --- a/layout/style/ServoBindings.toml +++ b/layout/style/ServoBindings.toml @@ -190,9 +190,6 @@ rusty-enums = [ "mozilla::StyleGeometryBox", "mozilla::SystemColor", ] -constified-enum-variants = [ - { enum = "nsCSSPropertyID", variants = ["eCSSProperty_COUNT.*"] }, -] whitelist-vars = [ "NS_AUTHOR_SPECIFIED_.*", "NS_THEME_.*", diff --git a/layout/style/moz.build b/layout/style/moz.build index 86b4b40ec34e..4f593a120ef4 100644 --- a/layout/style/moz.build +++ b/layout/style/moz.build @@ -23,6 +23,7 @@ DIRS += ['xbl-marquee'] TEST_DIRS += ['test'] EXPORTS += [ + '!nsCSSPropertyID.h', 'AnimationCommon.h', 'CounterStyleManager.h', 'nsAnimationManager.h', @@ -35,7 +36,6 @@ EXPORTS += [ 'nsCSSKeywords.h', 'nsCSSParser.h', 'nsCSSPropAliasList.h', - 'nsCSSPropertyID.h', 'nsCSSPropertyIDSet.h', 'nsCSSPropList.h', 'nsCSSProps.h', @@ -276,10 +276,18 @@ CONTENT_ACCESSIBLE_FILES += [ ] GENERATED_FILES += [ + 'nsCSSPropertyID.h', 'ServoCSSPropList.h', 'ServoCSSPropList.py', ] +prop_id = GENERATED_FILES['nsCSSPropertyID.h'] +prop_id.script = 'GenerateCSSPropertyID.py:generate' +prop_id.inputs = [ + 'nsCSSPropertyID.h.in', + '!ServoCssPropList.py', +] + servo_props = GENERATED_FILES['ServoCSSPropList.h'] servo_props.script = 'GenerateServoCSSPropList.py:generate_header' servo_props.inputs = [ diff --git a/layout/style/nsCSSPropertyID.h b/layout/style/nsCSSPropertyID.h.in similarity index 64% rename from layout/style/nsCSSPropertyID.h rename to layout/style/nsCSSPropertyID.h.in index e795938c9a3e..cd36a69c5990 100644 --- a/layout/style/nsCSSPropertyID.h +++ b/layout/style/nsCSSPropertyID.h.in @@ -21,30 +21,7 @@ enum nsCSSPropertyID { eCSSProperty_UNKNOWN = -1, - #define CSS_PROP_LONGHAND(name_, id_, ...) eCSSProperty_##id_, - #include "mozilla/ServoCSSPropList.h" - #undef CSS_PROP_LONGHAND - - eCSSProperty_COUNT_no_shorthands, - // Make the count continue where it left off: - eCSSProperty_COUNT_DUMMY = eCSSProperty_COUNT_no_shorthands - 1, - - #define CSS_PROP_SHORTHAND(name_, id_, ...) eCSSProperty_##id_, - #include "mozilla/ServoCSSPropList.h" - #undef CSS_PROP_SHORTHAND - - eCSSProperty_COUNT, - // Make the count continue where it left off: - eCSSProperty_COUNT_DUMMY2 = eCSSProperty_COUNT - 1, - - #define CSS_PROP_ALIAS(aliasname_, aliasid_, id_, method_, pref_) \ - eCSSPropertyAlias_##aliasid_, - #include "mozilla/ServoCSSPropList.h" - #undef CSS_PROP_ALIAS - - eCSSProperty_COUNT_with_aliases, - // Make the count continue where it left off: - eCSSProperty_COUNT_DUMMY3 = eCSSProperty_COUNT_with_aliases - 1, +$property_ids // Some of the values below could probably overlap with each other // if we had a need for them to do so. @@ -54,17 +31,17 @@ enum nsCSSPropertyID { eCSSPropertyExtra_no_properties, eCSSPropertyExtra_all_properties, - // Extra dummy values for nsCSSParser internal use. - eCSSPropertyExtra_x_none_value, - eCSSPropertyExtra_x_auto_value, - // Extra value to represent custom properties (--*). eCSSPropertyExtra_variable, - - // Extra value for use in the DOM API's - eCSSProperty_DOM }; +const nsCSSPropertyID + eCSSProperty_COUNT_no_shorthands = $longhand_count; +const nsCSSPropertyID + eCSSProperty_COUNT = $shorthand_count; +const nsCSSPropertyID + eCSSProperty_COUNT_with_aliases = eCSSPropertyExtra_no_properties; + namespace mozilla { template<> diff --git a/servo/components/style/build_gecko.rs b/servo/components/style/build_gecko.rs index 2d1db9e70373..b5dd2ed4126f 100644 --- a/servo/components/style/build_gecko.rs +++ b/servo/components/style/build_gecko.rs @@ -33,8 +33,7 @@ mod common { #[cfg(feature = "bindgen")] mod bindings { use bindgen::{Builder, CodegenConfig}; - use bindgen::callbacks::{EnumVariantCustomBehavior, EnumVariantValue, ParseCallbacks}; - use regex::{Regex, RegexSet}; + use regex::Regex; use std::cmp; use std::collections::{HashMap, HashSet}; use std::env; @@ -416,27 +415,6 @@ mod bindings { } fn generate_structs() { - #[derive(Debug)] - struct Callbacks(HashMap); - impl ParseCallbacks for Callbacks { - fn enum_variant_behavior( - &self, - enum_name: Option<&str>, - variant_name: &str, - _variant_value: EnumVariantValue, - ) -> Option { - enum_name - .and_then(|enum_name| self.0.get(enum_name)) - .and_then(|regex| { - if regex.is_match(variant_name) { - Some(EnumVariantCustomBehavior::Constify) - } else { - None - } - }) - } - } - let builder = Builder::get_initial_builder() .enable_cxx_namespaces() .with_codegen_config(CodegenConfig { @@ -452,21 +430,6 @@ mod bindings { .handle_str_items("whitelist-vars", |b, item| b.whitelist_var(item)) .handle_str_items("whitelist-types", |b, item| b.whitelist_type(item)) .handle_str_items("opaque-types", |b, item| b.opaque_type(item)) - .handle_list("constified-enum-variants", |builder, iter| { - let mut map = HashMap::new(); - for item in iter { - let item = item.as_table().unwrap(); - let name = item["enum"].as_str().unwrap(); - let variants = item["variants"] - .as_array() - .unwrap() - .as_slice() - .iter() - .map(|item| item.as_str().unwrap()); - map.insert(name.into(), RegexSet::new(variants).unwrap()); - } - builder.parse_callbacks(Box::new(Callbacks(map))) - }) .handle_table_items("mapped-generic-types", |builder, item| { let generic = item["generic"].as_bool().unwrap(); let gecko = item["gecko"].as_str().unwrap();