FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
codon/codon/parser/common.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
/
common.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
462 lines (420 loc) · 13.6 KB
Breadcrumbs
codon
/
codon
/
parser
/
common.cpp
Copy path
File metadata and controls
462 lines (420 loc) · 13.6 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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
//
Copyright (C) 2022-2026 Exaloop Inc. <https://exaloop.io>
#
include
"
common.h
"
#
include
<
cinttypes
>
#
include
<
climits
>
#
include
<
filesystem
>
#
include
<
string
>
#
include
<
vector
>
#
include
"
codon/compiler/compiler.h
"
#
include
"
codon/parser/cache.h
"
#
include
"
llvm/Support/FileSystem.h
"
#
include
"
llvm/Support/Path.h
"
#
include
<
fmt/format.h
>
#
include
<
cmrc/cmrc.hpp
>
CMRC_DECLARE
(codon);
namespace
codon
::ast {
IFilesystem::
path_t
IFilesystem::canonical
(
const
path_t
&path)
const
{
return
std::filesystem::weakly_canonical
(path);
}
void
IFilesystem::add_search_path
(
const
std::string &p) {
auto
path =
path_t
(p);
if
(
exists
(path)) {
search_paths.
emplace_back
(
canonical
(path));
}
}
std::vector<IFilesystem::
path_t
>
IFilesystem::get_stdlib_paths
()
const
{
return
search_paths;
}
ImportFile
IFilesystem::get_root
(
const
path_t
&sp)
const
{
bool
isStdLib =
false
;
std::string s = sp;
std::string root;
for
(
auto
&p :
get_stdlib_paths
())
if
(
startswith
(s, p)) {
root = p;
isStdLib =
true
;
break
;
}
auto
module0 =
get_module0
().
parent_path
();
if
(!isStdLib && !module0.
empty
() &&
startswith
(s, module0))
root = module0;
std::string ext =
"
.codon
"
;
if
(!((root.
empty
() ||
startswith
(s, root)) &&
endswith
(s, ext)))
ext =
"
.py
"
;
seqassertn
((root.
empty
() ||
startswith
(s, root)) &&
endswith
(s, ext),
"
bad path substitution: {}, {}
"
, s, root);
auto
module
= s.
substr
(root.
size
() +
1
, s.
size
() - root.
size
() - ext.
size
() -
1
);
std::ranges::replace
(
module
,
'
/
'
,
'
.
'
);
return
ImportFile{(!isStdLib && root == module0) ? ImportFile::
PACKAGE
: ImportFile::
STDLIB
,
s,
module
};
}
Filesystem::Filesystem
(
const
std::string &argv0,
const
std::string &module0)
: argv0(argv0), module0(module0) {
if
(
auto
p =
getenv
(
"
CODON_PATH
"
)) {
add_search_path
(p);
}
if
(!argv0.
empty
()) {
auto
root =
executable_path
(argv0.
c_str
()).
parent_path
();
for
(
auto
loci : {
"
../lib/codon/stdlib
"
,
"
../stdlib
"
,
"
stdlib
"
}) {
add_search_path
(root / loci);
}
for
(
auto
loci : {
"
../lib/codon/plugins
"
,
"
../plugins
"
}) {
add_search_path
(root / loci);
}
}
}
std::vector<std::string>
Filesystem::read_lines
(
const
path_t
&path)
const
{
std::vector<std::string> lines;
if
(path ==
"
-
"
) {
for
(std::string line;
getline
(std::cin, line);) {
lines.
push_back
(line);
}
}
else
{
std::ifstream
fin
(path);
if
(!fin)
E
(error::Error::
COMPILER_NO_FILE
,
SrcInfo
(), path);
for
(std::string line;
getline
(fin, line);) {
lines.
push_back
(line);
}
fin.
close
();
}
return
lines;
}
void
Filesystem::set_module0
(
const
std::string &s) { module0 =
canonical
(
path_t
(s)); }
IFilesystem::
path_t
Filesystem::get_module0
()
const
{
return
module0.
empty
() ?
IFilesystem::path_t
() :
canonical
(module0);
}
IFilesystem::
path_t
Filesystem::executable_path
(
const
char
*argv0) {
auto
exc =
llvm::sys::fs::getMainExecutable
(
argv0,
reinterpret_cast
<
void
*>(executable_path));
return
path_t
(exc);
}
bool
Filesystem::exists
(
const
IFilesystem::
path_t
&path)
const
{
return
std::filesystem::exists
(path);
}
ResourceFilesystem::ResourceFilesystem
(
const
std::string &argv0,
const
std::string &module0,
bool
allowExternal)
: Filesystem(argv0, module0), allowExternal(allowExternal) {
search_paths = {
"
/stdlib
"
};
}
std::vector<std::string>
ResourceFilesystem::read_lines
(
const
path_t
&path)
const
{
auto
fs =
cmrc::codon::get_filesystem
();
if
(!fs.
exists
(path) && allowExternal)
return
Filesystem::read_lines
(path);
std::vector<std::string> lines;
if
(path ==
"
-
"
) {
E
(error::Error::
COMPILER_NO_FILE
,
SrcInfo
(),
"
<stdin>
"
);
}
else
{
try
{
auto
fd = fs.
open
(path);
auto
contents =
std::string
(fd.
begin
(), fd.
end
());
lines =
split
(contents,
'
\n
'
);
}
catch
(std::system_error &) {
E
(error::Error::
COMPILER_NO_FILE
,
SrcInfo
(), path);
}
}
return
lines;
}
bool
ResourceFilesystem::exists
(
const
path_t
&path)
const
{
auto
fs =
cmrc::codon::get_filesystem
();
if
(fs.
exists
(path))
return
true
;
if
(allowExternal)
return
Filesystem::exists
(path);
return
false
;
}
//
/ String and collection utilities
std::vector<std::string>
split
(
const
std::string &s,
char
delim) {
std::vector<std::string> items;
std::string item;
std::istringstream
iss
(s);
while
(
std::getline
(iss, item, delim))
items.
push_back
(item);
return
items;
}
//
clang-format off
std::string
escape
(
const
std::string &str) {
std::string r;
r.
reserve
(str.
size
());
for
(
unsigned
char
c : str) {
switch
(c) {
case
'
\a
'
: r +=
"
\\
a
"
;
break
;
case
'
\b
'
: r +=
"
\\
b
"
;
break
;
case
'
\f
'
: r +=
"
\\
f
"
;
break
;
case
'
\n
'
: r +=
"
\\
n
"
;
break
;
case
'
\r
'
: r +=
"
\\
r
"
;
break
;
case
'
\t
'
: r +=
"
\\
t
"
;
break
;
case
'
\v
'
: r +=
"
\\
v
"
;
break
;
case
'
\'
'
: r +=
"
\\
'
"
;
break
;
case
'
\\
'
: r +=
"
\\\\
"
;
break
;
default
:
if
(c <
32
|| c >=
127
)
r +=
fmt::format
(
"
\\
x{:x}
"
, c);
else
r += c;
}
}
return
r;
}
std::string
unescape
(
const
std::string &str) {
std::string r;
r.
reserve
(str.
size
());
for
(
int
i =
0
; i < str.
size
(); i++) {
if
(str[i] ==
'
\\
'
&& i +
1
< str.
size
())
switch
(str[i +
1
]) {
case
'
a
'
: r +=
'
\a
'
; i++;
break
;
case
'
b
'
: r +=
'
\b
'
; i++;
break
;
case
'
f
'
: r +=
'
\f
'
; i++;
break
;
case
'
n
'
: r +=
'
\n
'
; i++;
break
;
case
'
r
'
: r +=
'
\r
'
; i++;
break
;
case
'
t
'
: r +=
'
\t
'
; i++;
break
;
case
'
v
'
: r +=
'
\v
'
; i++;
break
;
case
'
"
'
: r +=
'
\"
'
; i++;
break
;
case
'
\'
'
: r +=
'
\'
'
; i++;
break
;
case
'
\\
'
: r +=
'
\\
'
; i++;
break
;
case
'
x
'
: {
if
(i +
3
> str.
size
())
throw
std::invalid_argument
(
"
invalid
\\
x code
"
);
size_t
pos =
0
;
auto
code =
std::stoi
(str.
substr
(i +
2
,
2
), &pos,
16
);
r +=
static_cast
<
char
>(code);
i += pos +
1
;
break
;
}
default
:
if
(str[i +
1
] >=
'
0
'
&& str[i +
1
] <=
'
7
'
) {
size_t
pos =
0
;
auto
code =
std::stoi
(str.
substr
(i +
1
,
3
), &pos,
8
);
r +=
static_cast
<
char
>(code);
i += pos;
}
else
{
r += str[i];
}
}
else
r += str[i];
}
return
r;
}
//
clang-format on
std::string
escapeFStringBraces
(
const
std::string &str,
int
start,
int
len) {
std::string t;
t.
reserve
(len);
for
(
int
i = start; i < start + len; i++)
if
(str[i] ==
'
{
'
)
t +=
"
{{
"
;
else
if
(str[i] ==
'
}
'
)
t +=
"
}}
"
;
else
t += str[i];
return
t;
}
int
findStar
(
const
std::string &s) {
int
i =
0
;
for
(; i < s.
size
(); i++) {
if
(s[i] ==
'
(
'
)
return
i +
1
;
if
(!
isspace
(s[i]))
return
i;
}
return
i;
}
bool
in
(
const
std::string &m,
char
item) {
auto
f = m.
find
(item);
return
f != std::string::npos;
}
bool
in
(
const
std::string &m,
const
std::string &item) {
auto
f = m.
find
(item);
return
f != std::string::npos;
}
size_t
startswith
(
const
std::string &str,
const
std::string &prefix) {
if
(prefix.
empty
())
return
true
;
return
(str.
size
() >= prefix.
size
() && str.
substr
(
0
, prefix.
size
()) == prefix)
? prefix.
size
()
:
0
;
}
size_t
endswith
(
const
std::string &str,
const
std::string &suffix) {
if
(suffix.
empty
())
return
true
;
return
(str.
size
() >= suffix.
size
() &&
str.
substr
(str.
size
() - suffix.
size
()) == suffix)
? suffix.
size
()
:
0
;
}
void
ltrim
(std::string &str) {
str.
erase
(str.
begin
(),
std::ranges::find_if
(
str, [](
unsigned
char
ch) {
return
!
std::isspace
(ch); }));
}
void
rtrim
(std::string &str) {
//
/ https://stackoverflow.com/questions/216823/whats-the-best-way-to-trim-stdstring
str.
erase
(
std::find_if
(str.
rbegin
(), str.
rend
(),
[](
unsigned
char
ch) {
return
!
std::isspace
(ch); })
.
base
(),
str.
end
());
}
bool
isdigit
(
const
std::string &str) {
return
std::ranges::all_of
(str, ::isdigit); }
//
Adapted from https://github.com/gpakosz/whereami/blob/master/src/whereami.c (MIT)
#
ifdef
__APPLE__
#
include
<
dlfcn.h
>
#
include
<
mach-o/dyld.h
>
#
endif
std::string
library_path
() {
std::string result;
#
ifdef
__APPLE__
char
buffer[
PATH_MAX
];
for
(;;) {
Dl_info info;
if
(
dladdr
(
__builtin_extract_return_addr
(
__builtin_return_address
(
0
)), &info)) {
char
*resolved =
realpath
(info.
dli_fname
, buffer);
if
(!resolved)
break
;
result =
std::string
(resolved);
}
break
;
}
#
else
for
(
int
r =
0
; r <
5
; r++) {
FILE
*maps =
fopen
(
"
/proc/self/maps
"
,
"
r
"
);
if
(!maps)
break
;
for
(;;) {
char
buffer[
PATH_MAX
<
1024
?
1024
:
PATH_MAX
];
uint64_t
low, high;
char
perms[
5
];
uint64_t
offset;
uint32_t
major, minor;
char
path[
PATH_MAX
];
uint32_t
inode;
if
(!
fgets
(buffer,
sizeof
(buffer), maps))
break
;
if
(
sscanf
(buffer,
"
%
"
PRIx64
"
-%
"
PRIx64
"
%s %
"
PRIx64
"
%x:%x %u %s
\n
"
, &low,
&high, perms, &offset, &major, &minor, &inode, path) ==
8
) {
uint64_t
addr =
(
uintptr_t
)(
__builtin_extract_return_addr
(
__builtin_return_address
(
0
)));
if
(low <= addr && addr <= high) {
char
*resolved =
realpath
(path, buffer);
if
(resolved)
result =
std::string
(resolved);
break
;
}
}
}
fclose
(maps);
if
(!result.
empty
())
break
;
}
#
endif
return
result;
}
std::string
Filesystem::get_absolute_path
(
const
std::string &path) {
char
*c =
realpath
(path.
c_str
(),
nullptr
);
if
(!c)
return
path;
std::string
result
(c);
free
(c);
return
result;
}
std::shared_ptr<ImportFile>
getImportFile
(Cache *cache,
const
std::string &what,
const
std::string &relativeTo,
bool
forceStdlib) {
auto
fs = cache->
fs
.
get
();
std::vector<std::string> paths;
auto
parentRelativeTo =
IFilesystem::path_t
(relativeTo).
parent_path
();
if
(what !=
"
<jit>
"
) {
if
(!forceStdlib) {
auto
path = parentRelativeTo / what;
path.
replace_extension
(
"
codon
"
);
if
(fs->
exists
(path))
paths.
emplace_back
(fs->
canonical
(path));
path = parentRelativeTo / what /
"
__init__.codon
"
;
if
(fs->
exists
(path))
paths.
emplace_back
(fs->
canonical
(path));
path = parentRelativeTo / what;
path.
replace_extension
(
"
py
"
);
if
(fs->
exists
(path))
paths.
emplace_back
(fs->
canonical
(path));
path = parentRelativeTo / what /
"
__init__.py
"
;
if
(fs->
exists
(path))
paths.
emplace_back
(fs->
canonical
(path));
}
}
auto
checkPlugin = [&paths, &fs, &cache](
const
std::filesystem::path &path,
const
std::string &what) {
if
(fs->
exists
(path / what /
"
plugin.toml
"
) &&
fs->
exists
(path / what /
"
stdlib
"
/ what /
"
__init__.codon
"
)) {
bool
failed =
false
;
if
(cache->
compiler
&& !cache->
compiler
->
isPluginLoaded
(path / what)) {
LOG_REALIZE
(
"
Loading plugin {}
"
, path / what);
llvm::handleAllErrors
(cache->
compiler
->
load
(path / what),
[&failed](
const
codon::error::PluginErrorInfo &e) {
codon::compilationError
(e.
getMessage
(),
/*
file=
*/
"
"
,
/*
line=
*/
0
,
/*
col=
*/
0
,
/*
len=
*/
0
,
/*
errorCode=
*/
-
1
,
/*
terminate=
*/
false
);
failed =
true
;
});
}
if
(!failed)
paths.
emplace_back
(
fs->
canonical
(path / what /
"
stdlib
"
/ what /
"
__init__.codon
"
));
}
};
if
(paths.
empty
()) {
//
Load a plugin maybe
checkPlugin
(parentRelativeTo, what);
}
for
(
auto
&p : fs->
get_stdlib_paths
()) {
auto
path = p / what;
path.
replace_extension
(
"
codon
"
);
if
(fs->
exists
(path))
paths.
emplace_back
(fs->
canonical
(path));
path = p / what /
"
__init__.codon
"
;
if
(fs->
exists
(path))
paths.
emplace_back
(fs->
canonical
(path));
//
Load a plugin maybe
checkPlugin
(p, what);
}
if
(paths.
empty
())
return
nullptr
;
return
std::make_shared<ImportFile>(fs->
get_root
(paths[
0
]));
}
std::string
getMangledClass
(
const
std::string &
module
,
const
std::string &cls,
size_t
id) {
if
(
module
==
"
std.internal.core
"
)
return
cls;
std::string num;
if
(!
in
(cls,
'
.
'
))
num =
"
.
"
+
std::to_string
(id);
return
(
module
.
empty
() ?
"
"
: (
module
+
"
.
"
)) + cls + num;
}
std::string
getMangledFunc
(
const
std::string &
module
,
const
std::string &fn,
size_t
overload,
size_t
id) {
if
(
module
==
"
std.internal.core
"
)
return
fn +
"
:
"
+
std::to_string
(overload);
std::string num;
if
(!
in
(fn,
'
.
'
))
num =
"
.
"
+
std::to_string
(id);
return
(
module
.
empty
() ?
"
"
: (
module
+
"
.
"
)) + fn + num +
"
:
"
+
std::to_string
(overload);
}
std::string
getMangledMethod
(
const
std::string &
module
,
const
std::string &cls,
const
std::string &method,
size_t
overload,
size_t
id) {
if
(
module
==
"
std.internal.core
"
)
return
cls +
"
.
"
+ method +
"
:
"
+
std::to_string
(overload);
std::string num;
if
(!
in
(cls,
'
.
'
))
num =
"
.
"
+
std::to_string
(id);
return
(
module
.
empty
() ?
"
"
: (
module
+
"
.
"
)) + cls + num +
"
.
"
+ method +
"
:
"
+
std::to_string
(overload);
}
std::string
getMangledVar
(
const
std::string &
module
,
const
std::string &var,
size_t
id) {
std::string num;
if
(!
in
(var,
'
.
'
))
num =
"
.
"
+
std::to_string
(id);
return
(
module
.
empty
() ?
"
"
: (
module
+
"
.
"
)) + var + num;
}
}
//
namespace codon::ast
Back
|
FazBrowse Home
|
New Git URL