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
This commit is contained in:
Xidorn Quan 2018-04-18 19:39:51 +10:00
parent b6bd79a52e
commit e723f6b291
5 changed files with 53 additions and 73 deletions

View File

@ -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],
}))

View File

@ -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_.*",

View File

@ -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 = [

View File

@ -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<>

View File

@ -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<String, RegexSet>);
impl ParseCallbacks for Callbacks {
fn enum_variant_behavior(
&self,
enum_name: Option<&str>,
variant_name: &str,
_variant_value: EnumVariantValue,
) -> Option<EnumVariantCustomBehavior> {
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();