| 1 |
''' |
| 2 |
MIT License |
| 3 |
|
| 4 |
Copyright 2020 Sophie Winter |
| 5 |
|
| 6 |
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: |
| 7 |
|
| 8 |
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. |
| 9 |
|
| 10 |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 11 |
''' |
| 12 |
|
| 13 |
import logging |
| 14 |
import re |
| 15 |
|
| 16 |
from version import Version |
| 17 |
import code |
| 18 |
|
| 19 |
logger = logging.getLogger(__name__) |
| 20 |
|
| 21 |
def remove_comments(code): |
| 22 |
return re.sub(r'(/\*.*?\*/|//.*?\n)', '', code, flags=re.DOTALL) |
| 23 |
|
| 24 |
# These can be combined in any order, we do not check that it is valid |
| 25 |
std_types = set([ |
| 26 |
'void', |
| 27 |
'int', |
| 28 |
'char', |
| 29 |
'signed', |
| 30 |
'unsigned', |
| 31 |
'short', |
| 32 |
'long', |
| 33 |
'float', |
| 34 |
'double', |
| 35 |
'uint32_t', |
| 36 |
'gint', |
| 37 |
'guint', |
| 38 |
'gchar', |
| 39 |
'gboolean', |
| 40 |
'gpointer', |
| 41 |
]) |
| 42 |
|
| 43 |
def is_std_type(name): |
| 44 |
for i in name.split(): |
| 45 |
if not i in std_types: |
| 46 |
return False |
| 47 |
return True |
| 48 |
|
| 49 |
class CType: |
| 50 |
def __str__(self): |
| 51 |
return self.str_left(False) + self.str_right(False) |
| 52 |
|
| 53 |
def str_right(self, resolved): |
| 54 |
return '' |
| 55 |
|
| 56 |
def __eq__(self, other): |
| 57 |
return str(self) == str(other) |
| 58 |
|
| 59 |
class StdType(CType): |
| 60 |
def __init__(self, name): |
| 61 |
assert isinstance(name, str) |
| 62 |
assert is_std_type(name), name |
| 63 |
self.name = name |
| 64 |
|
| 65 |
def resolve(self, ctx): |
| 66 |
pass |
| 67 |
|
| 68 |
def str_left(self, resolved): |
| 69 |
return self.name |
| 70 |
|
| 71 |
class CustomType(CType): |
| 72 |
def __init__(self, name): |
| 73 |
assert isinstance(name, str) |
| 74 |
self.explicit_struct = False |
| 75 |
if name.startswith('struct'): |
| 76 |
name = name[6:].strip() |
| 77 |
self.explicit_struct = True |
| 78 |
self.explicit_enum = False |
| 79 |
if name.startswith('enum'): |
| 80 |
name = name[4:].strip() |
| 81 |
self.explicit_enum = True |
| 82 |
self.name = name |
| 83 |
self.version = None |
| 84 |
self.resolved_struct = None |
| 85 |
|
| 86 |
def resolve(self, ctx): |
| 87 |
if self.explicit_enum: |
| 88 |
struct = None |
| 89 |
elif self.explicit_struct: |
| 90 |
struct = ctx.project.lookup_struct_name(self.name) |
| 91 |
else: |
| 92 |
struct = ctx.project.lookup_typedef(self.name) |
| 93 |
|
| 94 |
if struct: |
| 95 |
self.resolved_struct = struct |
| 96 |
self.version = ctx.version |
| 97 |
|
| 98 |
def str_left(self, resolved): |
| 99 |
if self.resolved_struct and resolved: |
| 100 |
v = self.resolved_struct.lookup_version(self.version) |
| 101 |
return 'struct ' + v.versioned_struct_name() |
| 102 |
else: |
| 103 |
result = '' |
| 104 |
if self.explicit_struct: |
| 105 |
result += 'struct ' |
| 106 |
if self.explicit_enum: |
| 107 |
result += 'enum ' |
| 108 |
result += self.name |
| 109 |
return result |
| 110 |
|
| 111 |
class PtrType(CType): |
| 112 |
def __init__(self, inner): |
| 113 |
assert isinstance(inner, CType) |
| 114 |
self.inner = inner |
| 115 |
|
| 116 |
def resolve(self, ctx): |
| 117 |
self.inner.resolve(ctx) |
| 118 |
|
| 119 |
def str_left(self, resolved): |
| 120 |
result = self.inner.str_left(False) |
| 121 |
if not isinstance(self.inner, PtrType): |
| 122 |
result += ' ' |
| 123 |
result += '*' |
| 124 |
return result |
| 125 |
|
| 126 |
def str_right(self, resolved): |
| 127 |
return self.inner.str_right(False) |
| 128 |
|
| 129 |
class ConstType(CType): |
| 130 |
def __init__(self, inner): |
| 131 |
assert isinstance(inner, CType) |
| 132 |
self.inner = inner |
| 133 |
|
| 134 |
def resolve(self, ctx): |
| 135 |
self.inner.resolve(ctx) |
| 136 |
|
| 137 |
def str_left(self, resolved): |
| 138 |
return 'const ' + self.inner.str_left(resolved) |
| 139 |
|
| 140 |
def str_right(self, resolved): |
| 141 |
return self.inner.str_right(resolved) |
| 142 |
|
| 143 |
# Note the first (and usually only) layer of "pointer" is consumed by this type |
| 144 |
# Do not wrap in a PtrType in most cases |
| 145 |
class FpType(CType): |
| 146 |
def __init__(self, return_type, arg_list): |
| 147 |
assert isinstance(return_type, CType) |
| 148 |
assert isinstance(arg_list, list) |
| 149 |
for i in arg_list: |
| 150 |
assert isinstance(i, PropertyNode), str(i) + ' is not a property node' |
| 151 |
self.return_type = return_type |
| 152 |
self.arg_list = arg_list |
| 153 |
|
| 154 |
def resolve(self, ctx): |
| 155 |
self.return_type.resolve(ctx) |
| 156 |
for arg in self.arg_list: |
| 157 |
arg.resolve(ctx) |
| 158 |
|
| 159 |
def str_left(self, resolved): |
| 160 |
return str(self.return_type) + ' (*' |
| 161 |
|
| 162 |
def str_right(self, resolved): |
| 163 |
return ') (' + str(', '.join([str(t) for t in self.arg_list])) + ')' |
| 164 |
|
| 165 |
class ArrayType(CType): |
| 166 |
def __init__(self, inner, size): |
| 167 |
assert isinstance(inner, CType) |
| 168 |
assert isinstance(size, str) |
| 169 |
self.inner = inner |
| 170 |
self.size = size |
| 171 |
|
| 172 |
def resolve(self, ctx): |
| 173 |
self.inner.resolve(ctx) |
| 174 |
|
| 175 |
def str_left(self, resolved): |
| 176 |
if resolved: |
| 177 |
return self.inner.str_left(True) |
| 178 |
else: |
| 179 |
return self.inner.str_left(False) + '*' |
| 180 |
|
| 181 |
def str_right(self, resolved): |
| 182 |
if resolved: |
| 183 |
return '[' + self.size + ']' + self.inner.str_right(True) |
| 184 |
else: |
| 185 |
return self.inner.str_right(False) |
| 186 |
|
| 187 |
class AstNode: |
| 188 |
def __eq__(self, other): |
| 189 |
return str(self) == str(other) |
| 190 |
|
| 191 |
class PropertyNode(AstNode): |
| 192 |
def __init__(self, c_type, name, bit_field): |
| 193 |
assert isinstance(c_type, CType) |
| 194 |
assert name is None or isinstance(name, str) |
| 195 |
assert bit_field is None or isinstance(bit_field, int) |
| 196 |
self.c_type = c_type |
| 197 |
self.name = name |
| 198 |
self.statement = False |
| 199 |
self.bit_field = bit_field |
| 200 |
|
| 201 |
def resolve(self, ctx): |
| 202 |
self.c_type.resolve(ctx) |
| 203 |
|
| 204 |
def get_property_list(self, prefix): |
| 205 |
return [(self.c_type, prefix + self.name)] |
| 206 |
|
| 207 |
def __str__(self): |
| 208 |
result = self.c_type.str_left(True) |
| 209 |
if self.name: |
| 210 |
if not isinstance(self.c_type, PtrType): |
| 211 |
result += ' ' |
| 212 |
result += self.name |
| 213 |
result += self.c_type.str_right(True) |
| 214 |
if self.bit_field: |
| 215 |
result += ' : ' + str(self.bit_field) |
| 216 |
if self.statement: |
| 217 |
result += ';\n' |
| 218 |
return result |
| 219 |
|
| 220 |
class ListNode(AstNode): |
| 221 |
def __init__(self, nodes): |
| 222 |
assert isinstance(nodes, list) |
| 223 |
for node in nodes: |
| 224 |
assert isinstance(node, AstNode) |
| 225 |
self.nodes = nodes |
| 226 |
|
| 227 |
def resolve(self, ctx): |
| 228 |
for node in self.nodes: |
| 229 |
node.resolve(ctx) |
| 230 |
|
| 231 |
def get_property_list(self, prefix): |
| 232 |
result = [] |
| 233 |
for node in self.nodes: |
| 234 |
result += node.get_property_list(prefix) |
| 235 |
return result |
| 236 |
|
| 237 |
def __str__(self): |
| 238 |
return ''.join(str(node) for node in self.nodes) |
| 239 |
|
| 240 |
class SubStructNode(AstNode): |
| 241 |
def __init__(self, name, content): |
| 242 |
assert isinstance(name, str) |
| 243 |
assert isinstance(content, AstNode) |
| 244 |
self.name = name |
| 245 |
self.content = content |
| 246 |
|
| 247 |
def resolve(self, ctx): |
| 248 |
self.content.resolve(ctx) |
| 249 |
|
| 250 |
def get_property_list(self, prefix): |
| 251 |
return self.content.get_property_list(prefix + self.name + '.') |
| 252 |
|
| 253 |
def __str__(self): |
| 254 |
return ( |
| 255 |
'struct {' + |
| 256 |
('\n' + code.INDENT).join([''] + str(self.content).splitlines()) + |
| 257 |
'\n} ' + self.name + ';\n') |