FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
codon/codon/parser/cache.cpp at develop · strider4560/codon · GitHub
strider4560
/
codon
Public
forked from
exaloop/codon
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
codon
/
codon
/
parser
/
cache.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
308 lines (274 loc) · 9.54 KB
Breadcrumbs
codon
/
codon
/
parser
/
cache.cpp
Copy path
File metadata and controls
308 lines (274 loc) · 9.54 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
//
Copyright (C) 2022-2026 Exaloop Inc. <https://exaloop.io>
#
include
"
cache.h
"
#
include
<
chrono
>
#
include
<
ranges
>
#
include
<
string
>
#
include
<
vector
>
#
include
"
codon/cir/pyextension.h
"
#
include
"
codon/cir/util/irtools.h
"
#
include
"
codon/parser/common.h
"
#
include
"
codon/parser/peg/peg.h
"
#
include
"
codon/parser/visitors/translate/translate.h
"
#
include
"
codon/parser/visitors/typecheck/ctx.h
"
#
include
"
codon/parser/visitors/typecheck/typecheck.h
"
namespace
codon
::ast {
const
std::string
VAR_ARGV
= getMangledVar(
"
"
,
"
__argv__
"
);
const
std::string
FN_OPTIONAL_UNWRAP
=
getMangledFunc
(
"
std.internal.types.optional
"
,
"
unwrap
"
);
Cache::Cache
(std::string argv0,
const
std::shared_ptr<IFilesystem> &fs) : fs(fs) {
if
(!
this
->
fs
) {
this
->
fs
= std::make_shared<Filesystem>(argv0);
}
this
->
_nodes
=
new
std::vector<std::unique_ptr<ast::ASTNode>>();
typeCtx = std::make_shared<TypeContext>(
this
,
"
.root
"
);
}
std::string
Cache::getTemporaryVar
(
const
std::string &prefix,
char
sigil) {
auto
n =
fmt::format
(
"
{}{}_{}
"
, sigil ?
fmt::format
(
"
{}_
"
, sigil) :
"
"
, prefix,
++varCount);
return
n;
}
std::string
Cache::rev
(
const
std::string &s) {
auto
i = reverseIdentifierLookup.
find
(s);
if
(i != reverseIdentifierLookup.
end
())
return
i->
second
;
seqassertn
(
false
,
"
'{}' has no non-canonical name
"
, s);
return
"
"
;
}
SrcInfo
Cache::generateSrcInfo
() {
return
{
FILE_GENERATED
, generatedSrcInfoCount, generatedSrcInfoCount++,
0
};
}
std::string
Cache::getContent
(
const
SrcInfo &info) {
auto
i = imports.
find
(info.
file
);
if
(i == imports.
end
())
return
"
"
;
int
line = info.
line
-
1
;
if
(line <
0
|| line >= i->
second
.
content
.
size
())
return
"
"
;
auto
s = i->
second
.
content
[line];
int
col = info.
col
-
1
;
if
(col <
0
|| col >= s.
size
())
return
"
"
;
int
len = info.
len
;
return
s.
substr
(col, len);
}
Cache::Class *
Cache::getClass
(
const
types::ClassType *type) {
auto
name = type->
name
;
return
in
(classes, name);
}
std::string
Cache::getMethod
(types::ClassType *typ,
const
std::string &member) {
if
(
auto
cls =
getClass
(typ)) {
if
(
auto
t =
in
(cls->
methods
, member))
return
*t;
}
seqassertn
(
false
,
"
cannot find '{}' in '{}'
"
, member, typ->
name
);
return
"
"
;
}
types::ClassType *
Cache::findClass
(
const
std::string &name)
const
{
auto
f = typeCtx->
find
(name);
if
(f && f->
isType
())
return
f->
getType
()->
getClass
()->
generics
[
0
].
getType
()->
getClass
();
return
nullptr
;
}
types::FuncType *
Cache::findFunction
(
const
std::string &name)
const
{
auto
f = typeCtx->
find
(name);
if
(f && f->
type
&& f->
isFunc
())
return
f->
type
->
getFunc
();
f = typeCtx->
find
(name +
"
:0
"
);
if
(f && f->
type
&& f->
isFunc
())
return
f->
type
->
getFunc
();
return
nullptr
;
}
types::FuncType *
Cache::findMethod
(types::ClassType *typ,
const
std::string &member,
const
std::vector<types::Type *> &args)
const
{
auto
f =
TypecheckVisitor
(typeCtx).
findBestMethod
(typ, member, args);
return
f;
}
ir::types::Type *
Cache::realizeType
(types::ClassType *type,
const
std::vector<types::TypePtr> &generics) {
auto
tv =
TypecheckVisitor
(typeCtx);
if
(
auto
rtv = tv.
realize
(tv.
instantiateType
(type,
castVectorPtr
(generics)))) {
return
classes[rtv->
getClass
()->
name
]
.
realizations
[rtv->
getClass
()->
realizedName
()]
->
ir
;
}
return
nullptr
;
}
ir::Func *
Cache::realizeFunction
(types::FuncType *type,
const
std::vector<types::TypePtr> &args,
const
std::vector<types::TypePtr> &generics,
types::ClassType *parentClass) {
auto
tv =
TypecheckVisitor
(typeCtx);
auto
t = tv.
instantiateType
(type, parentClass);
if
(args.
size
() != t->
size
() +
1
)
return
nullptr
;
types::Type::Unification undo;
if
(t->
getRetType
()->
unify
(args[
0
].
get
(), &undo) <
0
) {
undo.
undo
();
return
nullptr
;
}
for
(
int
gi =
1
; gi < args.
size
(); gi++) {
undo =
types::Type::Unification
();
if
((*t)[gi -
1
]->
unify
(args[gi].
get
(), &undo) <
0
) {
undo.
undo
();
return
nullptr
;
}
}
if
(!generics.
empty
()) {
if
(generics.
size
() != t->
funcGenerics
.
size
())
return
nullptr
;
for
(
int
gi =
0
; gi < generics.
size
(); gi++) {
undo =
types::Type::Unification
();
if
(t->
funcGenerics
[gi].
type
->
unify
(generics[gi].
get
(), &undo) <
0
) {
undo.
undo
();
return
nullptr
;
}
}
}
ir::Func *f =
nullptr
;
if
(
auto
rtv = tv.
realize
(t.
get
())) {
auto
pr = pendingRealizations;
//
copy it as it might be modified
for
(
const
auto
&key : pr | std::views::keys)
TranslateVisitor
(codegenCtx).
translateStmts
(
clone
(functions[key].
ast
));
f = functions[rtv->
getFunc
()->
ast
->
getName
()].
realizations
[rtv->
realizedName
()]->
ir
;
}
return
f;
}
ir::types::Type *
Cache::makeTuple
(
const
std::vector<types::TypePtr> &types) {
auto
tv =
TypecheckVisitor
(typeCtx);
auto
t = tv.
instantiateType
(tv.
generateTuple
(types.
size
()),
castVectorPtr
(types));
return
realizeType
(t->
getClass
(), types);
}
ir::types::Type *
Cache::makeFunction
(
const
std::vector<types::TypePtr> &types) {
auto
tv =
TypecheckVisitor
(typeCtx);
seqassertn
(!types.
empty
(),
"
types must have at least one argument
"
);
std::vector<types::Type *> tt;
for
(
size_t
i =
1
; i < types.
size
(); i++)
tt.
emplace_back
(types[i].
get
());
const
auto
&ret = types[
0
];
auto
argType = tv.
instantiateType
(tv.
generateTuple
(types.
size
() -
1
), tt);
auto
ft =
realizeType
(tv.
getStdLibType
(
"
Function
"
)->
getClass
(), {argType, ret});
return
ft;
}
ir::types::Type *
Cache::makeUnion
(
const
std::vector<types::TypePtr> &types) {
auto
tv =
TypecheckVisitor
(typeCtx);
auto
argType =
tv.
instantiateType
(tv.
generateTuple
(types.
size
()),
castVectorPtr
(types));
return
realizeType
(tv.
getStdLibType
(
"
Union
"
)->
getClass
(), {argType});
}
size_t
Cache::getRealizationId
(types::ClassType *type) {
auto
cv =
TypecheckVisitor
(typeCtx).
getClassRealization
(type);
return
cv->
id
;
}
std::vector<
size_t
>
Cache::getBaseRealizationIds
(types::ClassType *type) {
auto
r =
TypecheckVisitor
(typeCtx).
getClassRealization
(type);
std::vector<
size_t
> baseIds;
for
(
const
auto
&t : r->
bases
) {
baseIds.
push_back
(
getRealizationId
(t.
get
()));
}
return
baseIds;
}
std::vector<
size_t
>
Cache::getChildRealizationIds
(types::ClassType *type) {
auto
cv =
TypecheckVisitor
(typeCtx).
getClassRealization
(type);
auto
parentId = cv->
id
;
std::vector<
size_t
> childIds;
for
(
const
auto
&[_, c] : classes) {
for
(
const
auto
&[_, r] : c.
realizations
) {
for
(
const
auto
&t : r->
bases
) {
if
(
getRealizationId
(t.
get
()) == parentId) {
childIds.
push_back
(r->
id
);
break
;
}
}
}
}
return
childIds;
}
std::vector<ir::SeriesFlow *>
Cache::parseCode
(
const
std::string &code) {
auto
nodeOrErr =
ast::parseCode
(
this
,
"
<internal>
"
, code,
/*
startLine=
*/
0
);
if
(!nodeOrErr)
throw
exc::ParserException
(nodeOrErr.
takeError
());
auto
sctx = imports[
MAIN_IMPORT
].
ctx
;
auto
node =
ast::TypecheckVisitor::apply
(sctx, *nodeOrErr);
auto
old = codegenCtx->
series
;
codegenCtx->
series
.
clear
();
ast::TranslateVisitor
(codegenCtx).
initializeGlobals
();
ast::TranslateVisitor
(codegenCtx).
translateStmts
(node);
std::swap
(old, codegenCtx->
series
);
return
old;
}
std::vector<std::shared_ptr<types::ClassType>>
Cache::mergeC3
(std::vector<std::vector<types::TypePtr>> &seqs) {
//
Reference: https://www.python.org/download/releases/2.3/mro/
std::vector<std::shared_ptr<types::ClassType>> result;
for
(
size_t
i =
0
;; i++) {
bool
found =
false
;
std::shared_ptr<types::ClassType> cand =
nullptr
;
for
(
auto
&seq : seqs) {
if
(seq.
empty
())
continue
;
found =
true
;
bool
nothead =
false
;
for
(
auto
&s : seqs)
if
(!s.
empty
()) {
bool
in =
false
;
for
(
size_t
j =
1
; j < s.
size
(); j++) {
if
((in |= (seq[
0
]->
is
(s[j]->
getClass
()->
name
))))
break
;
}
if
(in) {
nothead =
true
;
break
;
}
}
if
(!nothead) {
cand = std::dynamic_pointer_cast<types::ClassType>(seq[
0
]);
break
;
}
}
if
(!found)
return
result;
if
(!cand)
return
{};
result.
push_back
(cand);
for
(
auto
&s : seqs)
if
(!s.
empty
() && cand->
is
(s[
0
]->
getClass
()->
name
)) {
s.
erase
(s.
begin
());
}
}
return
result;
}
/*
*
* Generate Python bindings for Cython-like access.
*/
void
Cache::populatePythonModule
() {
using
namespace
ast
;
const
std::string
CYTHON_ITER
=
"
_PyWrap.IterWrap
"
;
if
(!pythonExt)
return
;
if
(!pyModule)
pyModule = std::make_shared<ir::PyModule>();
LOG_USER
(
"
[py] ====== module generation =======
"
);
auto
tv =
TypecheckVisitor
(typeCtx);
auto
clss = classes;
//
needs copy as below fns can mutate this
for
(
const
auto
&cn : clss | std::views::keys) {
auto
py = tv.
cythonizeClass
(cn);
if
(!py.
name
.
empty
())
pyModule->
types
.
push_back
(py);
}
//
Handle __iternext__ wrappers
for
(
const
auto
&cn : classes[
CYTHON_ITER
].
realizations
| std::views::keys) {
auto
py = tv.
cythonizeIterator
(cn);
pyModule->
types
.
push_back
(py);
}
auto
fns = functions;
//
needs copy as below fns can mutate this
for
(
const
auto
&fn : fns | std::views::keys) {
auto
py = tv.
cythonizeFunction
(fn);
if
(!py.
name
.
empty
())
pyModule->
functions
.
push_back
(py);
}
//
Handle pending realizations!
auto
pr = pendingRealizations;
//
copy it as it might be modified
for
(
const
auto
&key : pr | std::views::keys)
TranslateVisitor
(codegenCtx).
translateStmts
(
clone
(functions[key].
ast
));
}
}
//
namespace codon::ast
Back
|
FazBrowse Home
|
New Git URL