summaryrefslogtreecommitdiff
path: root/buildscripts/idl
diff options
context:
space:
mode:
authorSara Golemon <sara.golemon@mongodb.com>2019-01-03 19:09:36 +0000
committerSara Golemon <sara.golemon@mongodb.com>2019-01-04 22:08:15 +0000
commit28e7a275a1d6b529bee593b58c60dfc9cd507d09 (patch)
treebcf7c7771598049e156b2f03e7bc7e0d9c68685a /buildscripts/idl
parent1450cb71819f64f17d45c41057df80dae7beba79 (diff)
downloadmongo-28e7a275a1d6b529bee593b58c60dfc9cd507d09.tar.gz
SERVER-38826 Export yaml constant values as C++ constexprs
Diffstat (limited to 'buildscripts/idl')
-rw-r--r--buildscripts/idl/idl/ast.py1
-rw-r--r--buildscripts/idl/idl/binder.py2
-rw-r--r--buildscripts/idl/idl/generator.py28
3 files changed, 28 insertions, 3 deletions
diff --git a/buildscripts/idl/idl/ast.py b/buildscripts/idl/idl/ast.py
index 391851708e2..3e36defa105 100644
--- a/buildscripts/idl/idl/ast.py
+++ b/buildscripts/idl/idl/ast.py
@@ -118,6 +118,7 @@ class Expression(common.SourceLocation):
"""Construct an Expression."""
self.expr = None # type: unicode
self.validate_constexpr = True # type: bool
+ self.export = False # type: bool
super(Expression, self).__init__(file_name, line, column)
diff --git a/buildscripts/idl/idl/binder.py b/buildscripts/idl/idl/binder.py
index 7abdd733cfd..ba37881aebf 100644
--- a/buildscripts/idl/idl/binder.py
+++ b/buildscripts/idl/idl/binder.py
@@ -526,9 +526,11 @@ def _bind_expression(expr, allow_literal_string=True):
if expr.literal is None:
node.expr = expr.expr
node.validate_constexpr = expr.is_constexpr
+ node.export = expr.is_constexpr
return node
node.validate_constexpr = False
+ node.export = True
# bool
if (expr.literal == "true") or (expr.literal == "false"):
diff --git a/buildscripts/idl/idl/generator.py b/buildscripts/idl/idl/generator.py
index 96b9c34bc67..bca7da40eba 100644
--- a/buildscripts/idl/idl/generator.py
+++ b/buildscripts/idl/idl/generator.py
@@ -33,6 +33,7 @@ from __future__ import absolute_import, print_function, unicode_literals
from abc import ABCMeta, abstractmethod
import io
import os
+import re
import string
import sys
import textwrap
@@ -225,6 +226,12 @@ def _gen_field_usage_constant(field):
return "k%sBit" % (common.title_case(field.cpp_name))
+def _get_constant(name):
+ # type: (unicode) -> unicode
+ """Transform an arbitrary label to a constant name."""
+ return 'k' + re.sub(r'([^a-zA-Z0-9_]+)', '_', common.title_case(name))
+
+
class _FastFieldUsageChecker(_FieldUsageCheckerBase):
"""
Check for duplicate fields, and required fields as needed.
@@ -736,7 +743,19 @@ class _CppHeaderFileWriter(_CppFileWriterBase):
self.write_empty_line()
- def gen_extern_declaration(self, vartype, varname, condition):
+ def _gen_exported_constexpr(self, name, suffix, expr, condition):
+ # type: (unicode, unicode, ast.Expression, ast.Condition) -> None
+ """Generate exports for default initializer."""
+ if not (name and expr and expr.export):
+ return
+
+ with self._condition(condition, preprocessor_only=True):
+ self._writer.write_line('constexpr auto %s%s = %s;' % (_get_constant(name), suffix,
+ expr.expr))
+
+ self.write_empty_line()
+
+ def _gen_extern_declaration(self, vartype, varname, condition):
# type: (unicode, unicode, ast.Condition) -> None
"""Generate externs for storage declaration."""
if (vartype is None) or (varname is None):
@@ -886,9 +905,12 @@ class _CppHeaderFileWriter(_CppFileWriterBase):
self.write_empty_line()
for scp in spec.server_parameters:
- self.gen_extern_declaration(scp.cpp_vartype, scp.cpp_varname, scp.condition)
+ self._gen_exported_constexpr(scp.name, 'Default', scp.default, scp.condition)
+ self._gen_extern_declaration(scp.cpp_vartype, scp.cpp_varname, scp.condition)
+
for opt in spec.configs:
- self.gen_extern_declaration(opt.cpp_vartype, opt.cpp_varname, opt.condition)
+ self._gen_exported_constexpr(opt.name, 'Default', opt.default, opt.condition)
+ self._gen_extern_declaration(opt.cpp_vartype, opt.cpp_varname, opt.condition)
class _CppSourceFileWriter(_CppFileWriterBase):