docs: cleanup: Rephrase and correct typos
[barectf.git] / barectf / codegen.py
1 # The MIT License (MIT)
2 #
3 # Copyright (c) 2014-2020 Philippe Proulx <pproulx@efficios.com>
4 #
5 # Permission is hereby granted, free of charge, to any person obtaining
6 # a copy of this software and associated documentation files (the
7 # "Software"), to deal in the Software without restriction, including
8 # without limitation the rights to use, copy, modify, merge, publish,
9 # distribute, sublicense, and/or sell copies of the Software, and to
10 # permit persons to whom the Software is furnished to do so, subject to
11 # the following conditions:
12 #
13 # The above copyright notice and this permission notice shall be
14 # included in all copies or substantial portions of the Software.
15 #
16 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19 # IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20 # CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21 # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22 # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
24 import barectf.tsdl182gen as barectf_tsdl182gen
25 import barectf.config as barectf_config
26 import barectf.cgen as barectf_cgen
27 from typing import List, Optional
28
29
30 # A file generated by a `CodeGenerator` object.
31 #
32 # A generated file has a name (influenced by the configuration's
33 # file name prefix option) and contents.
34 class _GeneratedFile:
35 def __init__(self, name: str, contents: str):
36 self._name = name
37 self._contents = contents
38
39 @property
40 def name(self) -> str:
41 return self._name
42
43 @property
44 def contents(self) -> str:
45 return self._contents
46
47
48 # A barectf code generator.
49 #
50 # Build a code generator with a barectf configuration.
51 #
52 # A code generator can generate the TSDL `metadata` file and C source
53 # and header files.
54 class CodeGenerator:
55 def __init__(self, configuration: barectf_config.Configuration):
56 self._config = configuration
57 self._file_name_prefix = configuration.options.code_generation_options.file_name_prefix
58 self._c_code_gen = barectf_cgen._CodeGen(configuration)
59 self._c_headers: Optional[List[_GeneratedFile]] = None
60 self._c_sources: Optional[List[_GeneratedFile]] = None
61 self._metadata_stream: Optional[_GeneratedFile] = None
62
63 @property
64 def _barectf_header_name(self) -> str:
65 return f'{self._file_name_prefix}.h'
66
67 @property
68 def _bitfield_header_name(self) -> str:
69 return f'{self._file_name_prefix}-bitfield.h'
70
71 def generate_c_headers(self) -> List[_GeneratedFile]:
72 if self._c_headers is None:
73 self._c_headers = [
74 _GeneratedFile(self._barectf_header_name, self._c_code_gen.gen_header()),
75 _GeneratedFile(self._bitfield_header_name, self._c_code_gen.gen_bitfield_header()),
76 ]
77
78 return self._c_headers
79
80 def generate_c_sources(self) -> List[_GeneratedFile]:
81 if self._c_sources is None:
82 self._c_sources = [
83 _GeneratedFile(f'{self._file_name_prefix}.c',
84 self._c_code_gen.gen_src(self._barectf_header_name,
85 self._bitfield_header_name))
86 ]
87
88 return self._c_sources
89
90 def generate_metadata_stream(self) -> _GeneratedFile:
91 if self._metadata_stream is None:
92 self._metadata_stream = _GeneratedFile('metadata',
93 barectf_tsdl182gen._from_config(self._config))
94
95 return self._metadata_stream
This page took 0.029939 seconds and 4 git commands to generate.