summaryrefslogtreecommitdiffstats
path: root/mesonbuild/ast
diff options
context:
space:
mode:
Diffstat (limited to 'mesonbuild/ast')
-rw-r--r--mesonbuild/ast/__init__.py34
-rw-r--r--mesonbuild/ast/interpreter.py447
-rw-r--r--mesonbuild/ast/introspection.py362
-rw-r--r--mesonbuild/ast/postprocess.py120
-rw-r--r--mesonbuild/ast/printer.py399
-rw-r--r--mesonbuild/ast/visitor.py146
6 files changed, 1508 insertions, 0 deletions
diff --git a/mesonbuild/ast/__init__.py b/mesonbuild/ast/__init__.py
new file mode 100644
index 0000000..d14620f
--- /dev/null
+++ b/mesonbuild/ast/__init__.py
@@ -0,0 +1,34 @@
+# Copyright 2019 The Meson development team
+
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+
+# http://www.apache.org/licenses/LICENSE-2.0
+
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# This class contains the basic functionality needed to run any interpreter
+# or an interpreter-based tool.
+
+__all__ = [
+ 'AstConditionLevel',
+ 'AstInterpreter',
+ 'AstIDGenerator',
+ 'AstIndentationGenerator',
+ 'AstJSONPrinter',
+ 'AstVisitor',
+ 'AstPrinter',
+ 'IntrospectionInterpreter',
+ 'BUILD_TARGET_FUNCTIONS',
+]
+
+from .interpreter import AstInterpreter
+from .introspection import IntrospectionInterpreter, BUILD_TARGET_FUNCTIONS
+from .visitor import AstVisitor
+from .postprocess import AstConditionLevel, AstIDGenerator, AstIndentationGenerator
+from .printer import AstPrinter, AstJSONPrinter
diff --git a/mesonbuild/ast/interpreter.py b/mesonbuild/ast/interpreter.py
new file mode 100644
index 0000000..7484e04
--- /dev/null
+++ b/mesonbuild/ast/interpreter.py
@@ -0,0 +1,447 @@
+# Copyright 2016 The Meson development team
+
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+
+# http://www.apache.org/licenses/LICENSE-2.0
+
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# This class contains the basic functionality needed to run any interpreter
+# or an interpreter-based tool.
+from __future__ import annotations
+
+import os
+import sys
+import typing as T
+
+from .. import mparser, mesonlib
+from .. import environment
+
+from ..interpreterbase import (
+ MesonInterpreterObject,
+ InterpreterBase,
+ InvalidArguments,
+ BreakRequest,
+ ContinueRequest,
+ default_resolve_key,
+)
+
+from ..interpreter import (
+ StringHolder,
+ BooleanHolder,
+ IntegerHolder,
+ ArrayHolder,
+ DictHolder,
+)
+
+from ..mparser import (
+ ArgumentNode,
+ ArithmeticNode,
+ ArrayNode,
+ AssignmentNode,
+ BaseNode,
+ ElementaryNode,
+ EmptyNode,
+ IdNode,
+ MethodNode,
+ NotNode,
+ PlusAssignmentNode,
+ TernaryNode,
+)
+
+if T.TYPE_CHECKING:
+ from .visitor import AstVisitor
+ from ..interpreter import Interpreter
+ from ..interpreterbase import TYPE_nkwargs, TYPE_nvar
+ from ..mparser import (
+ AndNode,
+ ComparisonNode,
+ ForeachClauseNode,
+ IfClauseNode,
+ IndexNode,
+ OrNode,
+ UMinusNode,
+ )
+
+class DontCareObject(MesonInterpreterObject):
+ pass
+
+class MockExecutable(MesonInterpreterObject):
+ pass
+
+class MockStaticLibrary(MesonInterpreterObject):
+ pass
+
+class MockSharedLibrary(MesonInterpreterObject):
+ pass
+
+class MockCustomTarget(MesonInterpreterObject):
+ pass
+
+class MockRunTarget(MesonInterpreterObject):
+ pass
+
+ADD_SOURCE = 0
+REMOVE_SOURCE = 1
+
+_T = T.TypeVar('_T')
+_V = T.TypeVar('_V')
+
+class AstInterpreter(InterpreterBase):
+ def __init__(self, source_root: str, subdir: str, subproject: str, visitors: T.Optional[T.List[AstVisitor]] = None):
+ super().__init__(source_root, subdir, subproject)
+ self.visitors = visitors if visitors is not None else []
+ self.processed_buildfiles = set() # type: T.Set[str]
+ self.assignments = {} # type: T.Dict[str, BaseNode]
+ self.assign_vals = {} # type: T.Dict[str, T.Any]
+ self.reverse_assignment = {} # type: T.Dict[str, BaseNode]
+ self.funcs.update({'project': self.func_do_nothing,
+ 'test': self.func_do_nothing,
+ 'benchmark': self.func_do_nothing,
+ 'install_headers': self.func_do_nothing,
+ 'install_man': self.func_do_nothing,
+ 'install_data': self.func_do_nothing,
+ 'install_subdir': self.func_do_nothing,
+ 'install_symlink': self.func_do_nothing,
+ 'install_emptydir': self.func_do_nothing,
+ 'configuration_data': self.func_do_nothing,
+ 'configure_file': self.func_do_nothing,
+ 'find_program': self.func_do_nothing,
+ 'include_directories': self.func_do_nothing,
+ 'add_global_arguments': self.func_do_nothing,
+ 'add_global_link_arguments': self.func_do_nothing,
+ 'add_project_arguments': self.func_do_nothing,
+ 'add_project_dependencies': self.func_do_nothing,
+ 'add_project_link_arguments': self.func_do_nothing,
+ 'message': self.func_do_nothing,
+ 'generator': self.func_do_nothing,
+ 'error': self.func_do_nothing,
+ 'run_command': self.func_do_nothing,
+ 'assert': self.func_do_nothing,
+ 'subproject': self.func_do_nothing,
+ 'dependency': self.func_do_nothing,
+ 'get_option': self.func_do_nothing,
+ 'join_paths': self.func_do_nothing,
+ 'environment': self.func_do_nothing,
+ 'import': self.func_do_nothing,
+ 'vcs_tag': self.func_do_nothing,
+ 'add_languages': self.func_do_nothing,
+ 'declare_dependency': self.func_do_nothing,
+ 'files': self.func_do_nothing,
+ 'executable': self.func_do_nothing,
+ 'static_library': self.func_do_nothing,
+ 'shared_library': self.func_do_nothing,
+ 'library': self.func_do_nothing,
+ 'build_target': self.func_do_nothing,
+ 'custom_target': self.func_do_nothing,
+ 'run_target': self.func_do_nothing,
+ 'subdir': self.func_subdir,
+ 'set_variable': self.func_do_nothing,
+ 'get_variable': self.func_do_nothing,
+ 'unset_variable': self.func_do_nothing,
+ 'is_disabler': self.func_do_nothing,
+ 'is_variable': self.func_do_nothing,
+ 'disabler': self.func_do_nothing,
+ 'gettext': self.func_do_nothing,
+ 'jar': self.func_do_nothing,
+ 'warning': self.func_do_nothing,
+ 'shared_module': self.func_do_nothing,
+ 'option': self.func_do_nothing,
+ 'both_libraries': self.func_do_nothing,
+ 'add_test_setup': self.func_do_nothing,
+ 'find_library': self.func_do_nothing,
+ 'subdir_done': self.func_do_nothing,
+ 'alias_target': self.func_do_nothing,
+ 'summary': self.func_do_nothing,
+ 'range': self.func_do_nothing,
+ 'structured_sources': self.func_do_nothing,
+ 'debug': self.func_do_nothing,
+ })
+
+ def _unholder_args(self, args: _T, kwargs: _V) -> T.Tuple[_T, _V]:
+ return args, kwargs
+
+ def _holderify(self, res: _T) -> _T:
+ return res
+
+ def func_do_nothing(self, node: BaseNode, args: T.List[TYPE_nvar], kwargs: T.Dict[str, TYPE_nvar]) -> bool:
+ return True
+
+ def load_root_meson_file(self) -> None:
+ super().load_root_meson_file()
+ for i in self.visitors:
+ self.ast.accept(i)
+
+ def func_subdir(self, node: BaseNode, args: T.List[TYPE_nvar], kwargs: T.Dict[str, TYPE_nvar]) -> None:
+ args = self.flatten_args(args)
+ if len(args) != 1 or not isinstance(args[0], str):
+ sys.stderr.write(f'Unable to evaluate subdir({args}) in AstInterpreter --> Skipping\n')
+ return
+
+ prev_subdir = self.subdir
+ subdir = os.path.join(prev_subdir, args[0])
+ absdir = os.path.join(self.source_root, subdir)
+ buildfilename = os.path.join(subdir, environment.build_filename)
+ absname = os.path.join(self.source_root, buildfilename)
+ symlinkless_dir = os.path.realpath(absdir)
+ build_file = os.path.join(symlinkless_dir, 'meson.build')
+ if build_file in self.processed_buildfiles:
+ sys.stderr.write('Trying to enter {} which has already been visited --> Skipping\n'.format(args[0]))
+ return
+ self.processed_buildfiles.add(build_file)
+
+ if not os.path.isfile(absname):
+ sys.stderr.write(f'Unable to find build file {buildfilename} --> Skipping\n')
+ return
+ with open(absname, encoding='utf-8') as f:
+ code = f.read()
+ assert isinstance(code, str)
+ try:
+ codeblock = mparser.Parser(code, absname).parse()
+ except mesonlib.MesonException as me:
+ me.file = absname
+ raise me
+
+ self.subdir = subdir
+ for i in self.visitors:
+ codeblock.accept(i)
+ self.evaluate_codeblock(codeblock)
+ self.subdir = prev_subdir
+
+ def method_call(self, node: BaseNode) -> bool:
+ return True
+
+ def evaluate_fstring(self, node: mparser.FormatStringNode) -> str:
+ assert isinstance(node, mparser.FormatStringNode)
+ return node.value
+
+ def evaluate_arraystatement(self, cur: mparser.ArrayNode) -> TYPE_nvar:
+ return self.reduce_arguments(cur.args)[0]
+
+ def evaluate_arithmeticstatement(self, cur: ArithmeticNode) -> int:
+ self.evaluate_statement(cur.left)
+ self.evaluate_statement(cur.right)
+ return 0
+
+ def evaluate_uminusstatement(self, cur: UMinusNode) -> int:
+ self.evaluate_statement(cur.value)
+ return 0
+
+ def evaluate_ternary(self, node: TernaryNode) -> None:
+ assert isinstance(node, TernaryNode)
+ self.evaluate_statement(node.condition)
+ self.evaluate_statement(node.trueblock)
+ self.evaluate_statement(node.falseblock)
+
+ def evaluate_dictstatement(self, node: mparser.DictNode) -> TYPE_nkwargs:
+ def resolve_key(node: mparser.BaseNode) -> str:
+ if isinstance(node, mparser.StringNode):
+ return node.value
+ return '__AST_UNKNOWN__'
+ arguments, kwargs = self.reduce_arguments(node.args, key_resolver=resolve_key)
+ assert not arguments
+ self.argument_depth += 1
+ for key, value in kwargs.items():
+ if isinstance(key, BaseNode):
+ self.evaluate_statement(key)
+ self.argument_depth -= 1
+ return {}
+
+ def evaluate_plusassign(self, node: PlusAssignmentNode) -> None:
+ assert isinstance(node, PlusAssignmentNode)
+ # Cheat by doing a reassignment
+ self.assignments[node.var_name] = node.value # Save a reference to the value node
+ if node.value.ast_id:
+ self.reverse_assignment[node.value.ast_id] = node
+ self.assign_vals[node.var_name] = self.evaluate_statement(node.value)
+
+ def evaluate_indexing(self, node: IndexNode) -> int:
+ return 0
+
+ def unknown_function_called(self, func_name: str) -> None:
+ pass
+
+ def reduce_arguments(
+ self,
+ args: mparser.ArgumentNode,
+ key_resolver: T.Callable[[mparser.BaseNode], str] = default_resolve_key,
+ duplicate_key_error: T.Optional[str] = None,
+ ) -> T.Tuple[T.List[TYPE_nvar], TYPE_nkwargs]:
+ if isinstance(args, ArgumentNode):
+ kwargs = {} # type: T.Dict[str, TYPE_nvar]
+ for key, val in args.kwargs.items():
+ kwargs[key_resolver(key)] = val
+ if args.incorrect_order():
+ raise InvalidArguments('All keyword arguments must be after positional arguments.')
+ return self.flatten_args(args.arguments), kwargs
+ else:
+ return self.flatten_args(args), {}
+
+ def evaluate_comparison(self, node: ComparisonNode) -> bool:
+ self.evaluate_statement(node.left)
+ self.evaluate_statement(node.right)
+ return False
+
+ def evaluate_andstatement(self, cur: AndNode) -> bool:
+ self.evaluate_statement(cur.left)
+ self.evaluate_statement(cur.right)
+ return False
+
+ def evaluate_orstatement(self, cur: OrNode) -> bool:
+ self.evaluate_statement(cur.left)
+ self.evaluate_statement(cur.right)
+ return False
+
+ def evaluate_notstatement(self, cur: NotNode) -> bool:
+ self.evaluate_statement(cur.value)
+ return False
+
+ def evaluate_foreach(self, node: ForeachClauseNode) -> None:
+ try:
+ self.evaluate_codeblock(node.block)
+ except ContinueRequest:
+ pass
+ except BreakRequest:
+ pass
+
+ def evaluate_if(self, node: IfClauseNode) -> None:
+ for i in node.ifs:
+ self.evaluate_codeblock(i.block)
+ if not isinstance(node.elseblock, EmptyNode):
+ self.evaluate_codeblock(node.elseblock)
+
+ def get_variable(self, varname: str) -> int:
+ return 0
+
+ def assignment(self, node: AssignmentNode) -> None:
+ assert isinstance(node, AssignmentNode)
+ self.assignments[node.var_name] = node.value # Save a reference to the value node
+ if node.value.ast_id:
+ self.reverse_assignment[node.value.ast_id] = node
+ self.assign_vals[node.var_name] = self.evaluate_statement(node.value) # Evaluate the value just in case
+
+ def resolve_node(self, node: BaseNode, include_unknown_args: bool = False, id_loop_detect: T.Optional[T.List[str]] = None) -> T.Optional[T.Any]:
+ def quick_resolve(n: BaseNode, loop_detect: T.Optional[T.List[str]] = None) -> T.Any:
+ if loop_detect is None:
+ loop_detect = []
+ if isinstance(n, IdNode):
+ assert isinstance(n.value, str)
+ if n.value in loop_detect or n.value not in self.assignments:
+ return []
+ return quick_resolve(self.assignments[n.value], loop_detect = loop_detect + [n.value])
+ elif isinstance(n, ElementaryNode):
+ return n.value
+ else:
+ return n
+
+ if id_loop_detect is None:
+ id_loop_detect = []
+ result = None
+
+ if not isinstance(node, BaseNode):
+ return None
+
+ assert node.ast_id
+ if node.ast_id in id_loop_detect:
+ return None # Loop detected
+ id_loop_detect += [node.ast_id]
+
+ # Try to evealuate the value of the node
+ if isinstance(node, IdNode):
+ result = quick_resolve(node)
+
+ elif isinstance(node, ElementaryNode):
+ result = node.value
+
+ elif isinstance(node, NotNode):
+ result = self.resolve_node(node.value, include_unknown_args, id_loop_detect)
+ if isinstance(result, bool):
+ result = not result
+
+ elif isinstance(node, ArrayNode):
+ result = node.args.arguments.copy()
+
+ elif isinstance(node, ArgumentNode):
+ result = node.arguments.copy()
+
+ elif isinstance(node, ArithmeticNode):
+ if node.operation != 'add':
+ return None # Only handle string and array concats
+ l = quick_resolve(node.left)
+ r = quick_resolve(node.right)
+ if isinstance(l, str) and isinstance(r, str):
+ result = l + r # String concatenation detected
+ else:
+ result = self.flatten_args(l, include_unknown_args, id_loop_detect) + self.flatten_args(r, include_unknown_args, id_loop_detect)
+
+ elif isinstance(node, MethodNode):
+ src = quick_resolve(node.source_object)
+ margs = self.flatten_args(node.args.arguments, include_unknown_args, id_loop_detect)
+ mkwargs = {} # type: T.Dict[str, TYPE_nvar]
+ try:
+ if isinstance(src, str):
+ result = StringHolder(src, T.cast('Interpreter', self)).method_call(node.name, margs, mkwargs)
+ elif isinstance(src, bool):
+ result = BooleanHolder(src, T.cast('Interpreter', self)).method_call(node.name, margs, mkwargs)
+ elif isinstance(src, int):
+ result = IntegerHolder(src, T.cast('Interpreter', self)).method_call(node.name, margs, mkwargs)
+ elif isinstance(src, list):
+ result = ArrayHolder(src, T.cast('Interpreter', self)).method_call(node.name, margs, mkwargs)
+ elif isinstance(src, dict):
+ result = DictHolder(src, T.cast('Interpreter', self)).method_call(node.name, margs, mkwargs)
+ except mesonlib.MesonException:
+ return None
+
+ # Ensure that the result is fully resolved (no more nodes)
+ if isinstance(result, BaseNode):
+ result = self.resolve_node(result, include_unknown_args, id_loop_detect)
+ elif isinstance(result, list):
+ new_res = [] # type: T.List[TYPE_nvar]
+ for i in result:
+ if isinstance(i, BaseNode):
+ resolved = self.resolve_node(i, include_unknown_args, id_loop_detect)
+ if resolved is not None:
+ new_res += self.flatten_args(resolved, include_unknown_args, id_loop_detect)
+ else:
+ new_res += [i]
+ result = new_res
+
+ return result
+
+ def flatten_args(self, args_raw: T.Union[TYPE_nvar, T.Sequence[TYPE_nvar]], include_unknown_args: bool = False, id_loop_detect: T.Optional[T.List[str]] = None) -> T.List[TYPE_nvar]:
+ # Make sure we are always dealing with lists
+ if isinstance(args_raw, list):
+ args = args_raw
+ else:
+ args = [args_raw]
+
+ flattend_args = [] # type: T.List[TYPE_nvar]
+
+ # Resolve the contents of args
+ for i in args:
+ if isinstance(i, BaseNode):
+ resolved = self.resolve_node(i, include_unknown_args, id_loop_detect)
+ if resolved is not None:
+ if not isinstance(resolved, list):
+ resolved = [resolved]
+ flattend_args += resolved
+ elif isinstance(i, (str, bool, int, float)) or include_unknown_args:
+ flattend_args += [i]
+ return flattend_args
+
+ def flatten_kwargs(self, kwargs: T.Dict[str, TYPE_nvar], include_unknown_args: bool = False) -> T.Dict[str, TYPE_nvar]:
+ flattend_kwargs = {}
+ for key, val in kwargs.items():
+ if isinstance(val, BaseNode):
+ resolved = self.resolve_node(val, include_unknown_args)
+ if resolved is not None:
+ flattend_kwargs[key] = resolved
+ elif isinstance(val, (str, bool, int, float)) or include_unknown_args:
+ flattend_kwargs[key] = val
+ return flattend_kwargs
diff --git a/mesonbuild/ast/introspection.py b/mesonbuild/ast/introspection.py
new file mode 100644
index 0000000..194c15b
--- /dev/null
+++ b/mesonbuild/ast/introspection.py
@@ -0,0 +1,362 @@
+# Copyright 2018 The Meson development team
+
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+
+# http://www.apache.org/licenses/LICENSE-2.0
+
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# This class contains the basic functionality needed to run any interpreter
+# or an interpreter-based tool
+
+from __future__ import annotations
+import argparse
+import copy
+import os
+import typing as T
+
+from .. import compilers, environment, mesonlib, optinterpreter
+from .. import coredata as cdata
+from ..build import Executable, Jar, SharedLibrary, SharedModule, StaticLibrary
+from ..compilers import detect_compiler_for
+from ..interpreterbase import InvalidArguments
+from ..mesonlib import MachineChoice, OptionKey
+from ..mparser import BaseNode, ArithmeticNode, ArrayNode, ElementaryNode, IdNode, FunctionNode, StringNode
+from .interpreter import AstInterpreter
+
+if T.TYPE_CHECKING:
+ from ..build import BuildTarget
+ from ..interpreterbase import TYPE_nvar
+ from .visitor import AstVisitor
+
+
+# TODO: it would be nice to not have to duplicate this
+BUILD_TARGET_FUNCTIONS = [
+ 'executable', 'jar', 'library', 'shared_library', 'shared_module',
+ 'static_library', 'both_libraries'
+]
+
+class IntrospectionHelper(argparse.Namespace):
+ # mimic an argparse namespace
+ def __init__(self, cross_file: str):
+ super().__init__()
+ self.cross_file = cross_file # type: str
+ self.native_file = None # type: str
+ self.cmd_line_options = {} # type: T.Dict[str, str]
+
+ def __eq__(self, other: object) -> bool:
+ return NotImplemented
+
+class IntrospectionInterpreter(AstInterpreter):
+ # Interpreter to detect the options without a build directory
+ # Most of the code is stolen from interpreter.Interpreter
+ def __init__(self,
+ source_root: str,
+ subdir: str,
+ backend: str,
+ visitors: T.Optional[T.List[AstVisitor]] = None,
+ cross_file: T.Optional[str] = None,
+ subproject: str = '',
+ subproject_dir: str = 'subprojects',
+ env: T.Optional[environment.Environment] = None):
+ visitors = visitors if visitors is not None else []
+ super().__init__(source_root, subdir, subproject, visitors=visitors)
+
+ options = IntrospectionHelper(cross_file)
+ self.cross_file = cross_file
+ if env is None:
+ self.environment = environment.Environment(source_root, None, options)
+ else:
+ self.environment = env
+ self.subproject_dir = subproject_dir
+ self.coredata = self.environment.get_coredata()
+ self.option_file = os.path.join(self.source_root, self.subdir, 'meson_options.txt')
+ self.backend = backend
+ self.default_options = {OptionKey('backend'): self.backend}
+ self.project_data = {} # type: T.Dict[str, T.Any]
+ self.targets = [] # type: T.List[T.Dict[str, T.Any]]
+ self.dependencies = [] # type: T.List[T.Dict[str, T.Any]]
+ self.project_node = None # type: BaseNode
+
+ self.funcs.update({
+ 'add_languages': self.func_add_languages,
+ 'dependency': self.func_dependency,
+ 'executable': self.func_executable,
+ 'jar': self.func_jar,
+ 'library': self.func_library,
+ 'project': self.func_project,
+ 'shared_library': self.func_shared_lib,
+ 'shared_module': self.func_shared_module,
+ 'static_library': self.func_static_lib,
+ 'both_libraries': self.func_both_lib,
+ })
+
+ def func_project(self, node: BaseNode, args: T.List[TYPE_nvar], kwargs: T.Dict[str, TYPE_nvar]) -> None:
+ if self.project_node:
+ raise InvalidArguments('Second call to project()')
+ self.project_node = node
+ if len(args) < 1:
+ raise InvalidArguments('Not enough arguments to project(). Needs at least the project name.')
+
+ proj_name = args[0]
+ proj_vers = kwargs.get('version', 'undefined')
+ proj_langs = self.flatten_args(args[1:])
+ if isinstance(proj_vers, ElementaryNode):
+ proj_vers = proj_vers.value
+ if not isinstance(proj_vers, str):
+ proj_vers = 'undefined'
+ self.project_data = {'descriptive_name': proj_name, 'version': proj_vers}
+
+ if os.path.exists(self.option_file):
+ oi = optinterpreter.OptionInterpreter(self.subproject)
+ oi.process(self.option_file)
+ self.coredata.update_project_options(oi.options)
+
+ def_opts = self.flatten_args(kwargs.get('default_options', []))
+ _project_default_options = mesonlib.stringlistify(def_opts)
+ self.project_default_options = cdata.create_options_dict(_project_default_options, self.subproject)
+ self.default_options.update(self.project_default_options)
+ self.coredata.set_default_options(self.default_options, self.subproject, self.environment)
+
+ if not self.is_subproject() and 'subproject_dir' in kwargs:
+ spdirname = kwargs['subproject_dir']
+ if isinstance(spdirname, StringNode):
+ assert isinstance(spdirname.value, str)
+ self.subproject_dir = spdirname.value
+ if not self.is_subproject():
+ self.project_data['subprojects'] = []
+ subprojects_dir = os.path.join(self.source_root, self.subproject_dir)
+ if os.path.isdir(subprojects_dir):
+ for i in os.listdir(subprojects_dir):
+ if os.path.isdir(os.path.join(subprojects_dir, i)):
+ self.do_subproject(i)
+
+ self.coredata.init_backend_options(self.backend)
+ options = {k: v for k, v in self.environment.options.items() if k.is_backend()}
+
+ self.coredata.set_options(options)
+ self._add_languages(proj_langs, True, MachineChoice.HOST)
+ self._add_languages(proj_langs, True, MachineChoice.BUILD)
+
+ def do_subproject(self, dirname: str) -> None:
+ subproject_dir_abs = os.path.join(self.environment.get_source_dir(), self.subproject_dir)
+ subpr = os.path.join(subproject_dir_abs, dirname)
+ try:
+ subi = IntrospectionInterpreter(subpr, '', self.backend, cross_file=self.cross_file, subproject=dirname, subproject_dir=self.subproject_dir, env=self.environment, visitors=self.visitors)
+ subi.analyze()
+ subi.project_data['name'] = dirname
+ self.project_data['subprojects'] += [subi.project_data]
+ except (mesonlib.MesonException, RuntimeError):
+ return
+
+ def func_add_languages(self, node: BaseNode, args: T.List[TYPE_nvar], kwargs: T.Dict[str, TYPE_nvar]) -> None:
+ kwargs = self.flatten_kwargs(kwargs)
+ required = kwargs.get('required', True)
+ if isinstance(required, cdata.UserFeatureOption):
+ required = required.is_enabled()
+ if 'native' in kwargs:
+ native = kwargs.get('native', False)
+ self._add_languages(args, required, MachineChoice.BUILD if native else MachineChoice.HOST)
+ else:
+ for for_machine in [MachineChoice.BUILD, MachineChoice.HOST]:
+ self._add_languages(args, required, for_machine)
+
+ def _add_languages(self, raw_langs: T.List[TYPE_nvar], required: bool, for_machine: MachineChoice) -> None:
+ langs = [] # type: T.List[str]
+ for l in self.flatten_args(raw_langs):
+ if isinstance(l, str):
+ langs.append(l)
+ elif isinstance(l, StringNode):
+ langs.append(l.value)
+
+ for lang in sorted(langs, key=compilers.sort_clink):
+ lang = lang.lower()
+ if lang not in self.coredata.compilers[for_machine]:
+ try:
+ comp = detect_compiler_for(self.environment, lang, for_machine)
+ except mesonlib.MesonException:
+ # do we even care about introspecting this language?
+ if required:
+ raise
+ else:
+ continue
+ if self.subproject:
+ options = {}
+ for k in comp.get_options():
+ v = copy.copy(self.coredata.options[k])
+ k = k.evolve(subproject=self.subproject)
+ options[k] = v
+ self.coredata.add_compiler_options(options, lang, for_machine, self.environment)
+
+ def func_dependency(self, node: BaseNode, args: T.List[TYPE_nvar], kwargs: T.Dict[str, TYPE_nvar]) -> None:
+ args = self.flatten_args(args)
+ kwargs = self.flatten_kwargs(kwargs)
+ if not args:
+ return
+ name = args[0]
+ has_fallback = 'fallback' in kwargs
+ required = kwargs.get('required', True)
+ version = kwargs.get('version', [])
+ if not isinstance(version, list):
+ version = [version]
+ if isinstance(required, ElementaryNode):
+ required = required.value
+ if not isinstance(required, bool):
+ required = False
+ self.dependencies += [{
+ 'name': name,
+ 'required': required,
+ 'version': version,
+ 'has_fallback': has_fallback,
+ 'conditional': node.condition_level > 0,
+ 'node': node
+ }]
+
+ def build_target(self, node: BaseNode, args: T.List[TYPE_nvar], kwargs_raw: T.Dict[str, TYPE_nvar], targetclass: T.Type[BuildTarget]) -> T.Optional[T.Dict[str, T.Any]]:
+ args = self.flatten_args(args)
+ if not args or not isinstance(args[0], str):
+ return None
+ name = args[0]
+ srcqueue = [node]
+ extra_queue = []
+
+ # Process the sources BEFORE flattening the kwargs, to preserve the original nodes
+ if 'sources' in kwargs_raw:
+ srcqueue += mesonlib.listify(kwargs_raw['sources'])
+
+ if 'extra_files' in kwargs_raw:
+ extra_queue += mesonlib.listify(kwargs_raw['extra_files'])
+
+ kwargs = self.flatten_kwargs(kwargs_raw, True)
+
+ def traverse_nodes(inqueue: T.List[BaseNode]) -> T.List[BaseNode]:
+ res = [] # type: T.List[BaseNode]
+ while inqueue:
+ curr = inqueue.pop(0)
+ arg_node = None
+ assert isinstance(curr, BaseNode)
+ if isinstance(curr, FunctionNode):
+ arg_node = curr.args
+ elif isinstance(curr, ArrayNode):
+ arg_node = curr.args
+ elif isinstance(curr, IdNode):
+ # Try to resolve the ID and append the node to the queue
+ assert isinstance(curr.value, str)
+ var_name = curr.value
+ if var_name in self.assignments:
+ tmp_node = self.assignments[var_name]
+ if isinstance(tmp_node, (ArrayNode, IdNode, FunctionNode)):
+ inqueue += [tmp_node]
+ elif isinstance(curr, ArithmeticNode):
+ inqueue += [curr.left, curr.right]
+ if arg_node is None:
+ continue
+ arg_nodes = arg_node.arguments.copy()
+ # Pop the first element if the function is a build target function
+ if isinstance(curr, FunctionNode) and curr.func_name in BUILD_TARGET_FUNCTIONS:
+ arg_nodes.pop(0)
+ elemetary_nodes = [x for x in arg_nodes if isinstance(x, (str, StringNode))]
+ inqueue += [x for x in arg_nodes if isinstance(x, (FunctionNode, ArrayNode, IdNode, ArithmeticNode))]
+ if elemetary_nodes:
+ res += [curr]
+ return res
+
+ source_nodes = traverse_nodes(srcqueue)
+ extraf_nodes = traverse_nodes(extra_queue)
+
+ # Make sure nothing can crash when creating the build class
+ kwargs_reduced = {k: v for k, v in kwargs.items() if k in targetclass.known_kwargs and k in {'install', 'build_by_default', 'build_always'}}
+ kwargs_reduced = {k: v.value if isinstance(v, ElementaryNode) else v for k, v in kwargs_reduced.items()}
+ kwargs_reduced = {k: v for k, v in kwargs_reduced.items() if not isinstance(v, BaseNode)}
+ for_machine = MachineChoice.HOST
+ objects = [] # type: T.List[T.Any]
+ empty_sources = [] # type: T.List[T.Any]
+ # Passing the unresolved sources list causes errors
+ target = targetclass(name, self.subdir, self.subproject, for_machine, empty_sources, [], objects,
+ self.environment, self.coredata.compilers[for_machine], kwargs_reduced)
+ target.process_compilers()
+ target.process_compilers_late([])
+
+ new_target = {
+ 'name': target.get_basename(),
+ 'id': target.get_id(),
+ 'type': target.get_typename(),
+ 'defined_in': os.path.normpath(os.path.join(self.source_root, self.subdir, environment.build_filename)),
+ 'subdir': self.subdir,
+ 'build_by_default': target.build_by_default,
+ 'installed': target.should_install(),
+ 'outputs': target.get_outputs(),
+ 'sources': source_nodes,
+ 'extra_files': extraf_nodes,
+ 'kwargs': kwargs,
+ 'node': node,
+ }
+
+ self.targets += [new_target]
+ return new_target
+
+ def build_library(self, node: BaseNode, args: T.List[TYPE_nvar], kwargs: T.Dict[str, TYPE_nvar]) -> T.Optional[T.Dict[str, T.Any]]:
+ default_library = self.coredata.get_option(OptionKey('default_library'))
+ if default_library == 'shared':
+ return self.build_target(node, args, kwargs, SharedLibrary)
+ elif default_library == 'static':
+ return self.build_target(node, args, kwargs, StaticLibrary)
+ elif default_library == 'both':
+ return self.build_target(node, args, kwargs, SharedLibrary)
+ return None
+
+ def func_executable(self, node: BaseNode, args: T.List[TYPE_nvar], kwargs: T.Dict[str, TYPE_nvar]) -> T.Optional[T.Dict[str, T.Any]]:
+ return self.build_target(node, args, kwargs, Executable)
+
+ def func_static_lib(self, node: BaseNode, args: T.List[TYPE_nvar], kwargs: T.Dict[str, TYPE_nvar]) -> T.Optional[T.Dict[str, T.Any]]:
+ return self.build_target(node, args, kwargs, StaticLibrary)
+
+ def func_shared_lib(self, node: BaseNode, args: T.List[TYPE_nvar], kwargs: T.Dict[str, TYPE_nvar]) -> T.Optional[T.Dict[str, T.Any]]:
+ return self.build_target(node, args, kwargs, SharedLibrary)
+
+ def func_both_lib(self, node: BaseNode, args: T.List[TYPE_nvar], kwargs: T.Dict[str, TYPE_nvar]) -> T.Optional[T.Dict[str, T.Any]]:
+ return self.build_target(node, args, kwargs, SharedLibrary)
+
+ def func_shared_module(self, node: BaseNode, args: T.List[TYPE_nvar], kwargs: T.Dict[str, TYPE_nvar]) -> T.Optional[T.Dict[str, T.Any]]:
+ return self.build_target(node, args, kwargs, SharedModule)
+
+ def func_library(self, node: BaseNode, args: T.List[TYPE_nvar], kwargs: T.Dict[str, TYPE_nvar]) -> T.Optional[T.Dict[str, T.Any]]:
+ return self.build_library(node, args, kwargs)
+
+ def func_jar(self, node: BaseNode, args: T.List[TYPE_nvar], kwargs: T.Dict[str, TYPE_nvar]) -> T.Optional[T.Dict[str, T.Any]]:
+ return self.build_target(node, args, kwargs, Jar)
+
+ def func_build_target(self, node: BaseNode, args: T.List[TYPE_nvar], kwargs: T.Dict[str, TYPE_nvar]) -> T.Optional[T.Dict[str, T.Any]]:
+ if 'target_type' not in kwargs:
+ return None
+ target_type = kwargs.pop('target_type')
+ if isinstance(target_type, ElementaryNode):
+ target_type = target_type.value
+ if target_type == 'executable':
+ return self.build_target(node, args, kwargs, Executable)
+ elif target_type == 'shared_library':
+ return self.build_target(node, args, kwargs, SharedLibrary)
+ elif target_type == 'static_library':
+ return self.build_target(node, args, kwargs, StaticLibrary)
+ elif target_type == 'both_libraries':
+ return self.build_target(node, args, kwargs, SharedLibrary)
+ elif target_type == 'library':
+ return self.build_library(node, args, kwargs)
+ elif target_type == 'jar':
+ return self.build_target(node, args, kwargs, Jar)
+ return None
+
+ def is_subproject(self) -> bool:
+ return self.subproject != ''
+
+ def analyze(self) -> None:
+ self.load_root_meson_file()
+ self.sanity_check_ast()
+ self.parse_project()
+ self.run()
diff --git a/mesonbuild/ast/postprocess.py b/mesonbuild/ast/postprocess.py
new file mode 100644
index 0000000..0c28af0
--- /dev/null
+++ b/mesonbuild/ast/postprocess.py
@@ -0,0 +1,120 @@
+# Copyright 2019 The Meson development team
+
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+
+# http://www.apache.org/licenses/LICENSE-2.0
+
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# This class contains the basic functionality needed to run any interpreter
+# or an interpreter-based tool
+from __future__ import annotations
+
+from . import AstVisitor
+import typing as T
+
+if T.TYPE_CHECKING:
+ from .. import mparser
+
+class AstIndentationGenerator(AstVisitor):
+ def __init__(self) -> None:
+ self.level = 0
+
+ def visit_default_func(self, node: mparser.BaseNode) -> None:
+ # Store the current level in the node
+ node.level = self.level
+
+ def visit_ArrayNode(self, node: mparser.ArrayNode) -> None:
+ self.visit_default_func(node)
+ self.level += 1
+ node.args.accept(self)
+ self.level -= 1
+
+ def visit_DictNode(self, node: mparser.DictNode) -> None:
+ self.visit_default_func(node)
+ self.level += 1
+ node.args.accept(self)
+ self.level -= 1
+
+ def visit_MethodNode(self, node: mparser.MethodNode) -> None:
+ self.visit_default_func(node)
+ node.source_object.accept(self)
+ self.level += 1
+ node.args.accept(self)
+ self.level -= 1
+
+ def visit_FunctionNode(self, node: mparser.FunctionNode) -> None:
+ self.visit_default_func(node)
+ self.level += 1
+ node.args.accept(self)
+ self.level -= 1
+
+ def visit_ForeachClauseNode(self, node: mparser.ForeachClauseNode) -> None:
+ self.visit_default_func(node)
+ self.level += 1
+ node.items.accept(self)
+ node.block.accept(self)
+ self.level -= 1
+
+ def visit_IfClauseNode(self, node: mparser.IfClauseNode) -> None:
+ self.visit_default_func(node)
+ for i in node.ifs:
+ i.accept(self)
+ if node.elseblock:
+ self.level += 1
+ node.elseblock.accept(self)
+ self.level -= 1
+
+ def visit_IfNode(self, node: mparser.IfNode) -> None:
+ self.visit_default_func(node)
+ self.level += 1
+ node.condition.accept(self)
+ node.block.accept(self)
+ self.level -= 1
+
+class AstIDGenerator(AstVisitor):
+ def __init__(self) -> None:
+ self.counter = {} # type: T.Dict[str, int]
+
+ def visit_default_func(self, node: mparser.BaseNode) -> None:
+ name = type(node).__name__
+ if name not in self.counter:
+ self.counter[name] = 0
+ node.ast_id = name + '#' + str(self.counter[name])
+ self.counter[name] += 1
+
+class AstConditionLevel(AstVisitor):
+ def __init__(self) -> None:
+ self.condition_level = 0
+
+ def visit_default_func(self, node: mparser.BaseNode) -> None:
+ node.condition_level = self.condition_level
+
+ def visit_ForeachClauseNode(self, node: mparser.ForeachClauseNode) -> None:
+ self.visit_default_func(node)
+ self.condition_level += 1
+ node.items.accept(self)
+ node.block.accept(self)
+ self.condition_level -= 1
+
+ def visit_IfClauseNode(self, node: mparser.IfClauseNode) -> None:
+ self.visit_default_func(node)
+ for i in node.ifs:
+ i.accept(self)
+ if node.elseblock:
+ self.condition_level += 1
+ node.elseblock.accept(self)
+ self.condition_level -= 1
+
+ def visit_IfNode(self, node: mparser.IfNode) -> None:
+ self.visit_default_func(node)
+ self.condition_level += 1
+ node.condition.accept(self)
+ node.block.accept(self)
+ self.condition_level -= 1
diff --git a/mesonbuild/ast/printer.py b/mesonbuild/ast/printer.py
new file mode 100644
index 0000000..3c53910
--- /dev/null
+++ b/mesonbuild/ast/printer.py
@@ -0,0 +1,399 @@
+# Copyright 2019 The Meson development team
+
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+
+# http://www.apache.org/licenses/LICENSE-2.0
+
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# This class contains the basic functionality needed to run any interpreter
+# or an interpreter-based tool
+from __future__ import annotations
+
+from .. import mparser
+from . import AstVisitor
+import re
+import typing as T
+
+arithmic_map = {
+ 'add': '+',
+ 'sub': '-',
+ 'mod': '%',
+ 'mul': '*',
+ 'div': '/'
+}
+
+class AstPrinter(AstVisitor):
+ def __init__(self, indent: int = 2, arg_newline_cutoff: int = 5, update_ast_line_nos: bool = False):
+ self.result = ''
+ self.indent = indent
+ self.arg_newline_cutoff = arg_newline_cutoff
+ self.ci = ''
+ self.is_newline = True
+ self.last_level = 0
+ self.curr_line = 1 if update_ast_line_nos else None
+
+ def post_process(self) -> None:
+ self.result = re.sub(r'\s+\n', '\n', self.result)
+
+ def append(self, data: str, node: mparser.BaseNode) -> None:
+ self.last_level = node.level
+ if self.is_newline:
+ self.result += ' ' * (node.level * self.indent)
+ self.result += data
+ self.is_newline = False
+
+ def append_padded(self, data: str, node: mparser.BaseNode) -> None:
+ if self.result and self.result[-1] not in [' ', '\n']:
+ data = ' ' + data
+ self.append(data + ' ', node)
+
+ def newline(self) -> None:
+ self.result += '\n'
+ self.is_newline = True
+ if self.curr_line is not None:
+ self.curr_line += 1
+
+ def visit_BooleanNode(self, node: mparser.BooleanNode) -> None:
+ self.append('true' if node.value else 'false', node)
+ node.lineno = self.curr_line or node.lineno
+
+ def visit_IdNode(self, node: mparser.IdNode) -> None:
+ assert isinstance(node.value, str)
+ self.append(node.value, node)
+ node.lineno = self.curr_line or node.lineno
+
+ def visit_NumberNode(self, node: mparser.NumberNode) -> None:
+ self.append(str(node.value), node)
+ node.lineno = self.curr_line or node.lineno
+
+ def escape(self, val: str) -> str:
+ return val.translate(str.maketrans({'\'': '\\\'',
+ '\\': '\\\\'}))
+
+ def visit_StringNode(self, node: mparser.StringNode) -> None:
+ assert isinstance(node.value, str)
+ self.append("'" + self.escape(node.value) + "'", node)
+ node.lineno = self.curr_line or node.lineno
+
+ def visit_FormatStringNode(self, node: mparser.FormatStringNode) -> None:
+ assert isinstance(node.value, str)
+ self.append("f'" + node.value + "'", node)
+ node.lineno = self.curr_line or node.lineno
+
+ def visit_ContinueNode(self, node: mparser.ContinueNode) -> None:
+ self.append('continue', node)
+ node.lineno = self.curr_line or node.lineno
+
+ def visit_BreakNode(self, node: mparser.BreakNode) -> None:
+ self.append('break', node)
+ node.lineno = self.curr_line or node.lineno
+
+ def visit_ArrayNode(self, node: mparser.ArrayNode) -> None:
+ node.lineno = self.curr_line or node.lineno
+ self.append('[', node)
+ node.args.accept(self)
+ self.append(']', node)
+
+ def visit_DictNode(self, node: mparser.DictNode) -> None:
+ node.lineno = self.curr_line or node.lineno
+ self.append('{', node)
+ node.args.accept(self)
+ self.append('}', node)
+
+ def visit_OrNode(self, node: mparser.OrNode) -> None:
+ node.left.accept(self)
+ self.append_padded('or', node)
+ node.lineno = self.curr_line or node.lineno
+ node.right.accept(self)
+
+ def visit_AndNode(self, node: mparser.AndNode) -> None:
+ node.left.accept(self)
+ self.append_padded('and', node)
+ node.lineno = self.curr_line or node.lineno
+ node.right.accept(self)
+
+ def visit_ComparisonNode(self, node: mparser.ComparisonNode) -> None:
+ node.left.accept(self)
+ self.append_padded(node.ctype, node)
+ node.lineno = self.curr_line or node.lineno
+ node.right.accept(self)
+
+ def visit_ArithmeticNode(self, node: mparser.ArithmeticNode) -> None:
+ node.left.accept(self)
+ self.append_padded(arithmic_map[node.operation], node)
+ node.lineno = self.curr_line or node.lineno
+ node.right.accept(self)
+
+ def visit_NotNode(self, node: mparser.NotNode) -> None:
+ node.lineno = self.curr_line or node.lineno
+ self.append_padded('not', node)
+ node.value.accept(self)
+
+ def visit_CodeBlockNode(self, node: mparser.CodeBlockNode) -> None:
+ node.lineno = self.curr_line or node.lineno
+ for i in node.lines:
+ i.accept(self)
+ self.newline()
+
+ def visit_IndexNode(self, node: mparser.IndexNode) -> None:
+ node.iobject.accept(self)
+ node.lineno = self.curr_line or node.lineno
+ self.append('[', node)
+ node.index.accept(self)
+ self.append(']', node)
+
+ def visit_MethodNode(self, node: mparser.MethodNode) -> None:
+ node.lineno = self.curr_line or node.lineno
+ node.source_object.accept(self)
+ self.append('.' + node.name + '(', node)
+ node.args.accept(self)
+ self.append(')', node)
+
+ def visit_FunctionNode(self, node: mparser.FunctionNode) -> None:
+ node.lineno = self.curr_line or node.lineno
+ self.append(node.func_name + '(', node)
+ node.args.accept(self)
+ self.append(')', node)
+
+ def visit_AssignmentNode(self, node: mparser.AssignmentNode) -> None:
+ node.lineno = self.curr_line or node.lineno
+ self.append(node.var_name + ' = ', node)
+ node.value.accept(self)
+
+ def visit_PlusAssignmentNode(self, node: mparser.PlusAssignmentNode) -> None:
+ node.lineno = self.curr_line or node.lineno
+ self.append(node.var_name + ' += ', node)
+ node.value.accept(self)
+
+ def visit_ForeachClauseNode(self, node: mparser.ForeachClauseNode) -> None:
+ node.lineno = self.curr_line or node.lineno
+ self.append_padded('foreach', node)
+ self.append_padded(', '.join(node.varnames), node)
+ self.append_padded(':', node)
+ node.items.accept(self)
+ self.newline()
+ node.block.accept(self)
+ self.append('endforeach', node)
+
+ def visit_IfClauseNode(self, node: mparser.IfClauseNode) -> None:
+ node.lineno = self.curr_line or node.lineno
+ prefix = ''
+ for i in node.ifs:
+ self.append_padded(prefix + 'if', node)
+ prefix = 'el'
+ i.accept(self)
+ if not isinstance(node.elseblock, mparser.EmptyNode):
+ self.append('else', node)
+ node.elseblock.accept(self)
+ self.append('endif', node)
+
+ def visit_UMinusNode(self, node: mparser.UMinusNode) -> None:
+ node.lineno = self.curr_line or node.lineno
+ self.append_padded('-', node)
+ node.value.accept(self)
+
+ def visit_IfNode(self, node: mparser.IfNode) -> None:
+ node.lineno = self.curr_line or node.lineno
+ node.condition.accept(self)
+ self.newline()
+ node.block.accept(self)
+
+ def visit_TernaryNode(self, node: mparser.TernaryNode) -> None:
+ node.lineno = self.curr_line or node.lineno
+ node.condition.accept(self)
+ self.append_padded('?', node)
+ node.trueblock.accept(self)
+ self.append_padded(':', node)
+ node.falseblock.accept(self)
+
+ def visit_ArgumentNode(self, node: mparser.ArgumentNode) -> None:
+ node.lineno = self.curr_line or node.lineno
+ break_args = (len(node.arguments) + len(node.kwargs)) > self.arg_newline_cutoff
+ for i in node.arguments + list(node.kwargs.values()):
+ if not isinstance(i, (mparser.ElementaryNode, mparser.IndexNode)):
+ break_args = True
+ if break_args:
+ self.newline()
+ for i in node.arguments:
+ i.accept(self)
+ self.append(', ', node)
+ if break_args:
+ self.newline()
+ for key, val in node.kwargs.items():
+ key.accept(self)
+ self.append_padded(':', node)
+ val.accept(self)
+ self.append(', ', node)
+ if break_args:
+ self.newline()
+ if break_args:
+ self.result = re.sub(r', \n$', '\n', self.result)
+ else:
+ self.result = re.sub(r', $', '', self.result)
+
+class AstJSONPrinter(AstVisitor):
+ def __init__(self) -> None:
+ self.result = {} # type: T.Dict[str, T.Any]
+ self.current = self.result
+
+ def _accept(self, key: str, node: mparser.BaseNode) -> None:
+ old = self.current
+ data = {} # type: T.Dict[str, T.Any]
+ self.current = data
+ node.accept(self)
+ self.current = old
+ self.current[key] = data
+
+ def _accept_list(self, key: str, nodes: T.Sequence[mparser.BaseNode]) -> None:
+ old = self.current
+ datalist = [] # type: T.List[T.Dict[str, T.Any]]
+ for i in nodes:
+ self.current = {}
+ i.accept(self)
+ datalist += [self.current]
+ self.current = old
+ self.current[key] = datalist
+
+ def _raw_accept(self, node: mparser.BaseNode, data: T.Dict[str, T.Any]) -> None:
+ old = self.current
+ self.current = data
+ node.accept(self)
+ self.current = old
+
+ def setbase(self, node: mparser.BaseNode) -> None:
+ self.current['node'] = type(node).__name__
+ self.current['lineno'] = node.lineno
+ self.current['colno'] = node.colno
+ self.current['end_lineno'] = node.end_lineno
+ self.current['end_colno'] = node.end_colno
+
+ def visit_default_func(self, node: mparser.BaseNode) -> None:
+ self.setbase(node)
+
+ def gen_ElementaryNode(self, node: mparser.ElementaryNode) -> None:
+ self.current['value'] = node.value
+ self.setbase(node)
+
+ def visit_BooleanNode(self, node: mparser.BooleanNode) -> None:
+ self.gen_ElementaryNode(node)
+
+ def visit_IdNode(self, node: mparser.IdNode) -> None:
+ self.gen_ElementaryNode(node)
+
+ def visit_NumberNode(self, node: mparser.NumberNode) -> None:
+ self.gen_ElementaryNode(node)
+
+ def visit_StringNode(self, node: mparser.StringNode) -> None:
+ self.gen_ElementaryNode(node)
+
+ def visit_FormatStringNode(self, node: mparser.FormatStringNode) -> None:
+ self.gen_ElementaryNode(node)
+
+ def visit_ArrayNode(self, node: mparser.ArrayNode) -> None:
+ self._accept('args', node.args)
+ self.setbase(node)
+
+ def visit_DictNode(self, node: mparser.DictNode) -> None:
+ self._accept('args', node.args)
+ self.setbase(node)
+
+ def visit_OrNode(self, node: mparser.OrNode) -> None:
+ self._accept('left', node.left)
+ self._accept('right', node.right)
+ self.setbase(node)
+
+ def visit_AndNode(self, node: mparser.AndNode) -> None:
+ self._accept('left', node.left)
+ self._accept('right', node.right)
+ self.setbase(node)
+
+ def visit_ComparisonNode(self, node: mparser.ComparisonNode) -> None:
+ self._accept('left', node.left)
+ self._accept('right', node.right)
+ self.current['ctype'] = node.ctype
+ self.setbase(node)
+
+ def visit_ArithmeticNode(self, node: mparser.ArithmeticNode) -> None:
+ self._accept('left', node.left)
+ self._accept('right', node.right)
+ self.current['op'] = arithmic_map[node.operation]
+ self.setbase(node)
+
+ def visit_NotNode(self, node: mparser.NotNode) -> None:
+ self._accept('right', node.value)
+ self.setbase(node)
+
+ def visit_CodeBlockNode(self, node: mparser.CodeBlockNode) -> None:
+ self._accept_list('lines', node.lines)
+ self.setbase(node)
+
+ def visit_IndexNode(self, node: mparser.IndexNode) -> None:
+ self._accept('object', node.iobject)
+ self._accept('index', node.index)
+ self.setbase(node)
+
+ def visit_MethodNode(self, node: mparser.MethodNode) -> None:
+ self._accept('object', node.source_object)
+ self._accept('args', node.args)
+ self.current['name'] = node.name
+ self.setbase(node)
+
+ def visit_FunctionNode(self, node: mparser.FunctionNode) -> None:
+ self._accept('args', node.args)
+ self.current['name'] = node.func_name
+ self.setbase(node)
+
+ def visit_AssignmentNode(self, node: mparser.AssignmentNode) -> None:
+ self._accept('value', node.value)
+ self.current['var_name'] = node.var_name
+ self.setbase(node)
+
+ def visit_PlusAssignmentNode(self, node: mparser.PlusAssignmentNode) -> None:
+ self._accept('value', node.value)
+ self.current['var_name'] = node.var_name
+ self.setbase(node)
+
+ def visit_ForeachClauseNode(self, node: mparser.ForeachClauseNode) -> None:
+ self._accept('items', node.items)
+ self._accept('block', node.block)
+ self.current['varnames'] = node.varnames
+ self.setbase(node)
+
+ def visit_IfClauseNode(self, node: mparser.IfClauseNode) -> None:
+ self._accept_list('ifs', node.ifs)
+ self._accept('else', node.elseblock)
+ self.setbase(node)
+
+ def visit_UMinusNode(self, node: mparser.UMinusNode) -> None:
+ self._accept('right', node.value)
+ self.setbase(node)
+
+ def visit_IfNode(self, node: mparser.IfNode) -> None:
+ self._accept('condition', node.condition)
+ self._accept('block', node.block)
+ self.setbase(node)
+
+ def visit_TernaryNode(self, node: mparser.TernaryNode) -> None:
+ self._accept('condition', node.condition)
+ self._accept('true', node.trueblock)
+ self._accept('false', node.falseblock)
+ self.setbase(node)
+
+ def visit_ArgumentNode(self, node: mparser.ArgumentNode) -> None:
+ self._accept_list('positional', node.arguments)
+ kwargs_list = [] # type: T.List[T.Dict[str, T.Dict[str, T.Any]]]
+ for key, val in node.kwargs.items():
+ key_res = {} # type: T.Dict[str, T.Any]
+ val_res = {} # type: T.Dict[str, T.Any]
+ self._raw_accept(key, key_res)
+ self._raw_accept(val, val_res)
+ kwargs_list += [{'key': key_res, 'val': val_res}]
+ self.current['kwargs'] = kwargs_list
+ self.setbase(node)
diff --git a/mesonbuild/ast/visitor.py b/mesonbuild/ast/visitor.py
new file mode 100644
index 0000000..8a0e77b
--- /dev/null
+++ b/mesonbuild/ast/visitor.py
@@ -0,0 +1,146 @@
+# Copyright 2019 The Meson development team
+
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+
+# http://www.apache.org/licenses/LICENSE-2.0
+
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# This class contains the basic functionality needed to run any interpreter
+# or an interpreter-based tool
+from __future__ import annotations
+
+import typing as T
+
+if T.TYPE_CHECKING:
+ from .. import mparser
+
+class AstVisitor:
+ def __init__(self) -> None:
+ pass
+
+ def visit_default_func(self, node: mparser.BaseNode) -> None:
+ pass
+
+ def visit_BooleanNode(self, node: mparser.BooleanNode) -> None:
+ self.visit_default_func(node)
+
+ def visit_IdNode(self, node: mparser.IdNode) -> None:
+ self.visit_default_func(node)
+
+ def visit_NumberNode(self, node: mparser.NumberNode) -> None:
+ self.visit_default_func(node)
+
+ def visit_StringNode(self, node: mparser.StringNode) -> None:
+ self.visit_default_func(node)
+
+ def visit_FormatStringNode(self, node: mparser.FormatStringNode) -> None:
+ self.visit_default_func(node)
+
+ def visit_ContinueNode(self, node: mparser.ContinueNode) -> None:
+ self.visit_default_func(node)
+
+ def visit_BreakNode(self, node: mparser.BreakNode) -> None:
+ self.visit_default_func(node)
+
+ def visit_ArrayNode(self, node: mparser.ArrayNode) -> None:
+ self.visit_default_func(node)
+ node.args.accept(self)
+
+ def visit_DictNode(self, node: mparser.DictNode) -> None:
+ self.visit_default_func(node)
+ node.args.accept(self)
+
+ def visit_EmptyNode(self, node: mparser.EmptyNode) -> None:
+ self.visit_default_func(node)
+
+ def visit_OrNode(self, node: mparser.OrNode) -> None:
+ self.visit_default_func(node)
+ node.left.accept(self)
+ node.right.accept(self)
+
+ def visit_AndNode(self, node: mparser.AndNode) -> None:
+ self.visit_default_func(node)
+ node.left.accept(self)
+ node.right.accept(self)
+
+ def visit_ComparisonNode(self, node: mparser.ComparisonNode) -> None:
+ self.visit_default_func(node)
+ node.left.accept(self)
+ node.right.accept(self)
+
+ def visit_ArithmeticNode(self, node: mparser.ArithmeticNode) -> None:
+ self.visit_default_func(node)
+ node.left.accept(self)
+ node.right.accept(self)
+
+ def visit_NotNode(self, node: mparser.NotNode) -> None:
+ self.visit_default_func(node)
+ node.value.accept(self)
+
+ def visit_CodeBlockNode(self, node: mparser.CodeBlockNode) -> None:
+ self.visit_default_func(node)
+ for i in node.lines:
+ i.accept(self)
+
+ def visit_IndexNode(self, node: mparser.IndexNode) -> None:
+ self.visit_default_func(node)
+ node.iobject.accept(self)
+ node.index.accept(self)
+
+ def visit_MethodNode(self, node: mparser.MethodNode) -> None:
+ self.visit_default_func(node)
+ node.source_object.accept(self)
+ node.args.accept(self)
+
+ def visit_FunctionNode(self, node: mparser.FunctionNode) -> None:
+ self.visit_default_func(node)
+ node.args.accept(self)
+
+ def visit_AssignmentNode(self, node: mparser.AssignmentNode) -> None:
+ self.visit_default_func(node)
+ node.value.accept(self)
+
+ def visit_PlusAssignmentNode(self, node: mparser.PlusAssignmentNode) -> None:
+ self.visit_default_func(node)
+ node.value.accept(self)
+
+ def visit_ForeachClauseNode(self, node: mparser.ForeachClauseNode) -> None:
+ self.visit_default_func(node)
+ node.items.accept(self)
+ node.block.accept(self)
+
+ def visit_IfClauseNode(self, node: mparser.IfClauseNode) -> None:
+ self.visit_default_func(node)
+ for i in node.ifs:
+ i.accept(self)
+ node.elseblock.accept(self)
+
+ def visit_UMinusNode(self, node: mparser.UMinusNode) -> None:
+ self.visit_default_func(node)
+ node.value.accept(self)
+
+ def visit_IfNode(self, node: mparser.IfNode) -> None:
+ self.visit_default_func(node)
+ node.condition.accept(self)
+ node.block.accept(self)
+
+ def visit_TernaryNode(self, node: mparser.TernaryNode) -> None:
+ self.visit_default_func(node)
+ node.condition.accept(self)
+ node.trueblock.accept(self)
+ node.falseblock.accept(self)
+
+ def visit_ArgumentNode(self, node: mparser.ArgumentNode) -> None:
+ self.visit_default_func(node)
+ for i in node.arguments:
+ i.accept(self)
+ for key, val in node.kwargs.items():
+ key.accept(self)
+ val.accept(self)