-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathgensyms.py
166 lines (121 loc) · 3.92 KB
/
gensyms.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import configparser
from os import path
import sys
HEADER_FILE = """\
// WARNING!
// This file is generated. Do not edit by hand.
// Instead, edit ../symbols.cfg and run ../gensyms.py
#include "emacs-module.h"
#include "git2.h"
#ifndef SYMBOLS_H
#define SYMBOLS_H
typedef union {{
{union}
}} esym_enumval;
typedef struct {{
emacs_value *symbol;
esym_enumval value;
}} esym_map;
{decls}
void esyms_init(emacs_env *env);
#endif /* SYMBOLS_H */
"""
IMPL_FILE = """\
// WARNING!
// This file is generated. Do not edit by hand.
// Instead, edit ../symbols.cfg and run ../gensyms.py
#include "symbols.h"
#include "git2.h"
{decls}
void esyms_init(emacs_env *env)
{{
{init}
}}
"""
RESERVED_SECS = {'DEFAULT', 'unmapped'}
def join_indent(lines, levels=0, sep='\n'):
indent = ' ' * 4 * levels
return sep.join(indent + line for line in lines)
def sym_to_c(sym):
return 'esym_{}'.format(sym.replace('-', '_'))
def unique_syms(spec):
if isinstance(spec, configparser.ConfigParser):
syms = {
sym for section in spec.values() for sym in section
if not sym.startswith('__')
}
else:
syms = {
sym for sym in spec
if not sym.startswith('__')
}
return sorted(syms)
def all_syms(section):
return list((sym, val) for sym, val in section.items() if not sym.startswith('__'))
def mapname(name, raw=False):
if name.startswith('git_'):
name = name[4:]
if name.endswith('_t'):
name = name[:-2]
if raw:
return name
return 'esym_{}_map'.format(name)
def gen_header(spec):
declarations = []
union = []
for secname, section in spec.items():
if secname in RESERVED_SECS:
continue
typename = section.get('__type', secname)
union.append('{} {};'.format(typename, mapname(secname, raw=True)))
declarations.append('extern esym_map {}[{}];'.format(
mapname(secname), len(unique_syms(section))+1))
for sym in unique_syms(spec):
declarations.append('extern emacs_value {};'.format(sym_to_c(sym)))
union = join_indent(union, levels=1)
declarations = join_indent(declarations)
return HEADER_FILE.format(decls=declarations, union=union)
def gen_impl(spec):
declarations = []
inits = []
for sym in unique_syms(spec):
declarations.append('emacs_value {};'.format(sym_to_c(sym)))
inits.append('{} = env->make_global_ref(env, env->intern(env, "{}"));'.format(sym_to_c(sym), sym))
for secname, section in spec.items():
if secname in RESERVED_SECS:
continue
mname = mapname(secname)
syms = all_syms(section)
prefix = section.get('__prefix', '')
map_inits = []
for sym, val in syms:
if val is None:
val = sym.upper().replace('-', '_')
cname = prefix + val
map_inits.append('{{&{}, {{.{} = {}}}}}'.format(sym_to_c(sym), mapname(secname, raw=True), cname))
map_inits.append('{NULL, {0}}')
map_inits = join_indent(map_inits, levels=1, sep=',\n')
declarations.append('esym_map {}[{}] = {{\n'.format(
mname, len(syms)+1) + map_inits + '\n};')
declarations = join_indent(declarations)
inits = join_indent(inits, levels=1)
return IMPL_FILE.format(
decls=declarations,
init=inits,
)
def gensyms(spec):
header = gen_header(spec)
impl = gen_impl(spec)
return header, impl
if __name__ == '__main__':
spec = configparser.ConfigParser(allow_no_value=True, strict=False)
rootdir = path.dirname(path.abspath(__file__))
infile = path.join(rootdir, 'symbols.cfg')
headerfile = path.join(rootdir, 'src', 'symbols.h')
implfile = path.join(rootdir, 'src', 'symbols.c')
spec.read(infile)
header, impl = gensyms(spec)
with open(headerfile, 'w') as f:
f.write(header)
with open(implfile, 'w') as f:
f.write(impl)