FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
daScript/src/ast/ast_module.cpp at master · Xsankor/daScript · GitHub
Xsankor
/
daScript
Public
forked from
GaijinEntertainment/daScript
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
daScript
/
src
/
ast
/
ast_module.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
858 lines (784 loc) · 31.3 KB
Breadcrumbs
daScript
/
src
/
ast
/
ast_module.cpp
Copy path
File metadata and controls
858 lines (784 loc) · 31.3 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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
#
include
"
daScript/misc/platform.h
"
#
include
"
daScript/ast/ast.h
"
#
include
"
daScript/ast/ast_visitor.h
"
#
include
<
atomic
>
namespace
das
{
DAS_THREAD_LOCAL
unsigned
ModuleKarma =
0
;
bool
splitTypeName
(
const
string & name, string & moduleName, string & funcName ) {
auto
at = name.
find
(
"
::
"
);
if
( at!=string::npos ) {
moduleName = name.
substr
(
0
,at);
funcName = name.
substr
(at+
2
);
if
( moduleName==
"
builtin`
"
) moduleName=
"
$
"
;
return
true
;
}
else
{
moduleName =
"
*
"
;
funcName = name;
return
false
;
}
}
//
ANNOTATION
const
AnnotationArgument *
AnnotationArgumentList::find
(
const
string & name, Type type )
const
{
auto
it =
find_if
(
begin
(),
end
(), [&](
const
AnnotationArgument & arg){
return
(arg.
name
==name) && (type==Type::tVoid || type==arg.
type
);
});
return
it==
end
() ?
nullptr
: &*it;
}
bool
AnnotationArgumentList::getBoolOption
(
const
string & name,
bool
def)
const
{
auto
arg =
find
(name, Type::tBool);
return
arg ? arg->
bValue
: def;
}
int32_t
AnnotationArgumentList::getIntOption
(
const
string & name,
int32_t
def)
const
{
auto
arg =
find
(name, Type::tInt);
return
arg ? arg->
iValue
: def;
}
//
MODULE
void
Module::addDependency
( Module * mod,
bool
pub ) {
requireModule[mod] |= pub;
for
(
auto
it : mod->
requireModule
) {
if
( it.
second
) {
addDependency
(it.
first
,
false
);
}
}
}
TypeAnnotation *
Module::resolveAnnotation
(
const
TypeInfo * info ) {
intptr_t
ann = (
intptr_t
) (info->
annotation_or_name
);
if
( ann &
1
) {
//
we add ~ at the begining of the name for padding
//
if name is allocated by the compiler, it does not guarantee that it is aligned
//
we check if there is a ~ at the begining of the name, and if it is - we skip it
//
that way we can accept both aligned and unaligned names
auto
cvtbuf = (
char
*) ann;
if
( cvtbuf[
0
]==
'
~
'
) cvtbuf++;
string moduleName, annName;
splitTypeName
(cvtbuf, moduleName, annName);
TypeAnnotation * resolve =
nullptr
;
for
(
auto
pm = daScriptEnvironment::bound->
modules
; pm!=
nullptr
; pm=pm->
next
) {
if
( pm->
name
== moduleName ) {
if
(
auto
annT = pm->
findAnnotation
(annName) ) {
resolve = (TypeAnnotation *) annT.
get
();
}
break
;
}
}
if
( daScriptEnvironment::bound->
g_resolve_annotations
) {
info->
annotation_or_name
= resolve;
}
return
resolve;
}
else
{
return
info->
annotation_or_name
;
}
}
void
resetFusionEngine
();
atomic<
int
>
g_envTotal
(
0
);
void
Module::Initialize
() {
daScriptEnvironment::ensure
();
g_envTotal ++;
bool
all =
false
;
while
( !all ) {
all =
true
;
for
(
auto
m = daScriptEnvironment::bound->
modules
; m ; m = m->
next
) {
all &= m->
initDependencies
();
}
}
}
void
Module::Shutdown
() {
DAS_ASSERT
(daScriptEnvironment::owned!=
nullptr
);
DAS_ASSERT
(daScriptEnvironment::bound!=
nullptr
);
g_envTotal --;
if
( g_envTotal==
0
) {
shutdownDebugAgent
();
}
auto
m = daScriptEnvironment::bound->
modules
;
while
( m ) {
auto
pM = m;
m = m->
next
;
delete
pM;
}
clearGlobalAotLibrary
();
resetFusionEngine
();
daScriptEnvironment::bound =
nullptr
;
if
( daScriptEnvironment::owned ) {
delete
daScriptEnvironment::owned;
daScriptEnvironment::owned =
nullptr
;
}
}
void
Module::Reset
(
bool
debAg) {
if
( debAg )
shutdownDebugAgent
();
auto
m = daScriptEnvironment::bound->
modules
;
while
( m ) {
auto
pM = m;
m = m->
next
;
if
( pM->
promoted
)
delete
pM;
}
}
void
Module::foreach
(
const
callable<
bool
(Module *
module
)> & func ) {
for
(
auto
m = daScriptEnvironment::bound->
modules
; m !=
nullptr
; m = m->
next
) {
if
(!
func
(m))
break
;
}
}
Module *
Module::require
(
const
string & name ) {
if
( !daScriptEnvironment::bound )
return
nullptr
;
for
(
auto
m = daScriptEnvironment::bound->
modules
; m !=
nullptr
; m = m->
next
) {
if
( m->
name
== name ) {
return
m;
}
}
return
nullptr
;
}
Module *
Module::requireEx
(
const
string & name,
bool
allowPromoted ) {
if
( !daScriptEnvironment::bound )
return
nullptr
;
for
(
auto
m = daScriptEnvironment::bound->
modules
; m !=
nullptr
; m = m->
next
) {
if
( allowPromoted || !m->
promoted
) {
if
( m->
name
== name ) {
return
m;
}
}
}
return
nullptr
;
}
Type
Module::findOption
(
const
string & name ) {
Type optT = Type::none;
for
(
auto
m = daScriptEnvironment::bound->
modules
; m !=
nullptr
; m = m->
next
) {
auto
tt = m->
getOptionType
(name);
if
( tt != Type::none ) {
DAS_ASSERTF
(optT==Type::none,
"
duplicate module option %s
"
, name.
c_str
());
optT = tt;
}
}
return
optT;
}
Module::Module
(
const
string & n ) : name(n) {
if
( !name.
empty
() ) {
auto
first = daScriptEnvironment::bound->
modules
;
while
(first !=
nullptr
)
{
if
(first->
name
== n) {
DAS_FATAL_ERROR
(
"
Module `%s` already created
"
, first->
name
.
c_str
());
}
first = first->
next
;
}
next = daScriptEnvironment::bound->
modules
;
daScriptEnvironment::bound->
modules
=
this
;
builtIn =
true
;
}
if
( n !=
"
$
"
) {
requireModule[
require
(
"
$
"
)] =
false
;
}
else
if
(!name.
empty
()) {
requireModule[
this
] =
false
;
}
isModule = !n.
empty
();
isPublic =
true
;
}
void
Module::promoteToBuiltin
(
const
FileAccessPtr & access) {
DAS_ASSERTF
(!builtIn,
"
failed to promote. already builtin
"
);
next = daScriptEnvironment::bound->
modules
;
daScriptEnvironment::bound->
modules
=
this
;
builtIn =
true
;
promoted =
true
;
promotedAccess = access;
}
Module::~Module
() {
if
( builtIn ) {
Module ** p = &daScriptEnvironment::bound->
modules
;
for
(
auto
m = daScriptEnvironment::bound->
modules
; m !=
nullptr
; p = &m->
next
, m = m->
next
) {
if
( m ==
this
) {
*p = m->
next
;
return
;
}
}
DAS_ASSERTF
(
0
,
"
failed to unlink. was builtIn field assigned after the fact?
"
);
}
}
void
Module::ClearSharedModules
() {
vector<Module *> kmp;
Module::foreach
([&](Module * mod){
if
( mod->
promoted
) {
kmp.
push_back
(mod);
}
return
true
;
});
for
(
auto
km : kmp ) {
delete
km;
}
clearGlobalAotLibrary
();
}
bool
Module::addAlias
(
const
TypeDeclPtr & at,
bool
canFail ) {
if
( at->
alias
.
empty
() )
return
false
;
if
( aliasTypes.
insert
(at->
alias
, at) ) {
at->
module
=
this
;
return
true
;
}
else
{
if
( !canFail ) {
DAS_FATAL_ERROR
(
"
can't add duplicate alias %s to module %s
\n
"
,at->
alias
.
c_str
(), name.
c_str
() );
}
return
false
;
}
}
bool
Module::addAnnotation
(
const
AnnotationPtr & ptr,
bool
canFail ) {
if
( handleTypes.
insert
(ptr->
name
, ptr) ) {
ptr->
seal
(
this
);
return
true
;
}
else
{
if
( !canFail ) {
DAS_FATAL_ERROR
(
"
can't add duplicate annotation %s to module %s
\n
"
, ptr->
name
.
c_str
(), name.
c_str
() );
}
return
false
;
}
}
bool
Module::addTypeInfoMacro
(
const
TypeInfoMacroPtr & ptr,
bool
canFail ) {
if
( typeInfoMacros.
insert
(
make_pair
(ptr->
name
,
move
(ptr))).
second
) {
ptr->
seal
(
this
);
return
true
;
}
else
{
if
( !canFail ) {
DAS_FATAL_ERROR
(
"
can't add duplicate typeinfo macro %s to module %s
\n
"
, ptr->
name
.
c_str
(), name.
c_str
() );
}
return
false
;
}
}
bool
Module::addReaderMacro
(
const
ReaderMacroPtr & ptr,
bool
canFail ) {
if
( readMacros.
insert
(
make_pair
(ptr->
name
,
move
(ptr))).
second
) {
ptr->
seal
(
this
);
return
true
;
}
else
{
if
( !canFail ) {
DAS_FATAL_ERROR
(
"
can't add duplicate reader macro %s to module %s
\n
"
, ptr->
name
.
c_str
(), name.
c_str
() );
}
return
false
;
}
}
bool
Module::addCommentReader
(
const
CommentReaderPtr & ptr,
bool
canFail ) {
if
( commentReader ) {
if
( !canFail ) {
DAS_FATAL_ERROR
(
"
can't add 2nd comment reader to module %s
\n
"
, name.
c_str
() );
}
return
false
;
}
commentReader = ptr;
return
true
;
}
bool
Module::addVariable
(
const
VariablePtr & var,
bool
canFail ) {
if
( globals.
insert
(var->
name
, var) ) {
var->
module
=
this
;
return
true
;
}
else
{
if
( !canFail ) {
DAS_FATAL_ERROR
(
"
can't add duplicate variable %s to module %s
\n
"
, var->
name
.
c_str
(), name.
c_str
() );
}
return
false
;
}
}
bool
Module::addEnumeration
(
const
EnumerationPtr & en,
bool
canFail ) {
if
( enumerations.
insert
(en->
name
, en) ) {
en->
module
=
this
;
return
true
;
}
else
{
if
( !canFail ) {
DAS_FATAL_ERROR
(
"
can't add duplicate enumeration %s to module %s
\n
"
, en->
name
.
c_str
(), name.
c_str
() );
}
return
false
;
}
}
bool
Module::removeStructure
(
const
StructurePtr & st ) {
return
structures.
remove
(st->
name
);
}
bool
Module::addStructure
(
const
StructurePtr & st,
bool
canFail ) {
if
( structures.
insert
(st->
name
, st) ) {
st->
module
=
this
;
return
true
;
}
else
{
if
( !canFail ) {
DAS_FATAL_ERROR
(
"
can't add duplicate structure %s to module %s
\n
"
, st->
name
.
c_str
(), name.
c_str
() );
}
return
false
;
}
}
bool
Module::addFunction
(
const
FunctionPtr & fn,
bool
canFail ) {
fn->
module
=
this
;
auto
mangledName = fn->
getMangledName
();
fn->
module
=
nullptr
;
if
( fn->
builtIn
&& fn->
sideEffectFlags
==
uint32_t
(SideEffects::none) && fn->
result
->
isVoid
() ) {
DAS_FATAL_ERROR
(
"
can't add function %s to module %s; it has no side effects and no return type
\n
"
, mangledName.
c_str
(), name.
c_str
() );
}
if
( fn->
builtIn
&& fn->
sideEffectFlags
==
uint32_t
(SideEffects::modifyArgument) ) {
bool
anyRW =
false
;
for
(
const
auto
& arg : fn->
arguments
) {
if
( arg->
type
->
isRef
() && !arg->
type
->
isConst
() ) {
anyRW =
true
;
}
else
if
( arg->
type
->
isPointer
() && arg->
type
->
firstType
&& !arg->
type
->
firstType
->
isConst
() ) {
anyRW =
true
;
}
}
if
( !anyRW ) {
DAS_FATAL_ERROR
(
"
can't add function %s to module %s; modify argument requires non-const ref argument
\n
"
, mangledName.
c_str
(), name.
c_str
() );
}
}
if
( functions.
insert
(mangledName, fn) ) {
functionsByName[
hash64z
(fn->
name
.
c_str
())].
push_back
(fn);
fn->
module
=
this
;
return
true
;
}
else
{
if
( !canFail ) {
DAS_FATAL_ERROR
(
"
can't add duplicate function %s to module %s
\n
"
, mangledName.
c_str
(), name.
c_str
() );
}
return
false
;
}
}
bool
Module::addGeneric
(
const
FunctionPtr & fn,
bool
canFail ) {
auto
mangledName = fn->
getMangledName
();
if
( generics.
insert
(mangledName, fn) ) {
genericsByName[
hash64z
(fn->
name
.
c_str
())].
push_back
(fn);
fn->
module
=
this
;
return
true
;
}
else
{
if
( !canFail ) {
DAS_FATAL_ERROR
(
"
can't add duplicate generic function %s to module %s
\n
"
, mangledName.
c_str
(), name.
c_str
() );
}
return
false
;
}
}
TypeDeclPtr
Module::findAlias
(
const
string & na )
const
{
return
aliasTypes.
find
(na);
}
VariablePtr
Module::findVariable
(
const
string & na )
const
{
return
globals.
find
(na);
}
FunctionPtr
Module::findFunction
(
const
string & mangledName )
const
{
return
functions.
find
(mangledName);
}
FunctionPtr
Module::findUniqueFunction
(
const
string & mangledName )
const
{
auto
it = functionsByName.
find
(
hash64z
(mangledName.
c_str
()));
if
( it==functionsByName.
end
() )
return
nullptr
;
if
( it->
second
.
size
()!=
1
)
return
nullptr
;
return
it->
second
[
0
];
}
StructurePtr
Module::findStructure
(
const
string & na )
const
{
return
structures.
find
(na);
}
AnnotationPtr
Module::findAnnotation
(
const
string & na )
const
{
return
handleTypes.
find
(na);
}
TypeInfoMacroPtr
Module::findTypeInfoMacro
(
const
string & na )
const
{
auto
it = typeInfoMacros.
find
(na);
return
it != typeInfoMacros.
end
() ? it->
second
:
nullptr
;
}
EnumerationPtr
Module::findEnum
(
const
string & na )
const
{
return
enumerations.
find
(na);
}
ExprCallFactory *
Module::findCall
(
const
string & na )
const
{
auto
it = callThis.
find
(na);
return
it != callThis.
end
() ? &it->
second
:
nullptr
;
}
bool
Module::compileBuiltinModule
(
const
string & modName,
unsigned
char
* str,
unsigned
int
str_len ) {
TextWriter issues;
auto
access = make_smart<FileAccess>();
auto
fileInfo = make_unique<TextFileInfo>((
char
*) str,
uint32_t
(str_len),
false
);
access->
setFileInfo
(modName,
move
(fileInfo));
ModuleGroup dummyLibGroup;
auto
program =
parseDaScript
(modName, access, issues, dummyLibGroup,
true
);
ownFileInfo = access->
letGoOfFileInfo
(modName);
DAS_ASSERTF
(ownFileInfo,
"
something went wrong and FileInfo for builtin module can not be obtained
"
);
if
( program ) {
if
(program->
failed
()) {
for
(
auto
& err : program->
errors
) {
issues <<
reportError
(err.
at
, err.
what
, err.
extra
, err.
fixme
, err.
cerr
);
}
DAS_FATAL_ERROR
(
"
%s
\n
builtin module did not compile %s
\n
"
, issues.
str
().
c_str
(), modName.
c_str
());
return
false
;
}
//
ok, now let's rip content
program->
thisModule
->
aliasTypes
.
foreach
([&](
auto
aliasTypePtr){
addAlias
(aliasTypePtr);
});
program->
thisModule
->
enumerations
.
foreach
([&](
auto
penum){
addEnumeration
(penum);
});
program->
thisModule
->
structures
.
foreach
([&](
auto
pst){
addStructure
(pst);
});
program->
thisModule
->
generics
.
foreach
([&](
auto
fn){
addGeneric
(fn);
});
program->
thisModule
->
globals
.
foreach
([&](
auto
gvar){
addVariable
(gvar);
});
program->
thisModule
->
functions
.
foreach
([&](
auto
fn){
addFunction
(fn);
});
for
(
auto
& rqm : program->
thisModule
->
requireModule
) {
if
( rqm.
first
!=
this
) {
requireModule[rqm.
first
] |= rqm.
second
;
}
}
//
macros
auto
ptm = program->
thisModule
.
get
();
if
( ptm->
macroContext
) {
swap
( macroContext, ptm->
macroContext
);
ptm->
handleTypes
.
foreach
([&](
auto
fna){
addAnnotation
(fna);
});
}
variantMacros.
insert
(variantMacros.
end
(), ptm->
variantMacros
.
begin
(), ptm->
variantMacros
.
end
());
macros.
insert
(macros.
end
(), ptm->
macros
.
begin
(), ptm->
macros
.
end
());
inferMacros.
insert
(inferMacros.
end
(), ptm->
inferMacros
.
begin
(), ptm->
inferMacros
.
end
());
optimizationMacros.
insert
(optimizationMacros.
end
(), ptm->
optimizationMacros
.
begin
(), ptm->
optimizationMacros
.
end
());
lintMacros.
insert
(lintMacros.
end
(), ptm->
lintMacros
.
begin
(), ptm->
lintMacros
.
end
());
globalLintMacros.
insert
(globalLintMacros.
end
(), ptm->
globalLintMacros
.
begin
(), ptm->
globalLintMacros
.
end
());
for
(
auto
& rm : ptm->
readMacros
) {
addReaderMacro
(rm.
second
);
}
commentReader = ptm->
commentReader
;
return
true
;
}
else
{
DAS_FATAL_ERROR
(
"
builtin module did not parse %s
\n
"
, modName.
c_str
());
return
false
;
}
}
bool
isValidBuiltinName
(
const
string & name,
bool
canPunkt ) {
bool
hasPunkt =
false
;
bool
hasAlNum =
false
;
for
(
auto
ch : name ) {
if
(
isalpha
(ch) ) {
hasAlNum =
true
;
if
(
isupper
(ch) ) {
return
false
;
}
}
else
if
(
isdigit
(ch) || ch==
'
_
'
|| ch==
'
`
'
) {
hasAlNum =
true
;
}
else
if
(
ispunct
(ch) ) {
if
( canPunkt ) {
hasPunkt =
true
;
}
else
{
return
false
;
}
}
else
{
return
false
;
}
}
return
hasPunkt ^ hasAlNum;
//
has punkt, or alnum, but never both
}
void
Module::verifyBuiltinNames
(
uint32_t
flags) {
bool
failed =
false
;
if
( flags & VerifyBuiltinFlags::verifyAliasTypes ) {
aliasTypes.
foreach
([&](
auto
aliasTypePtr){
if
( !
isValidBuiltinName
(aliasTypePtr->
alias
) ) {
DAS_FATAL_ERROR
(
"
%s - alias type has incorrect name. expecting snake_case
\n
"
, aliasTypePtr->
alias
.
c_str
());
failed =
true
;
}
});
}
if
( flags & VerifyBuiltinFlags::verifyHandleTypes ) {
handleTypes.
foreach
([&](
auto
annPtr){
if
( !
isValidBuiltinName
(annPtr->
name
) ) {
DAS_FATAL_LOG
(
"
%s - annotation has incorrect name. expecting snake_case
\n
"
, annPtr->
name
.
c_str
());
failed =
true
;
}
});
}
if
( flags & VerifyBuiltinFlags::verifyGlobals ) {
globals.
foreach
([&](
auto
var){
if
( !
isValidBuiltinName
(var->
name
) ) {
DAS_FATAL_LOG
(
"
%s - global variable has incorrect name. expecting snake_case
\n
"
, var->
name
.
c_str
());
failed =
true
;
}
});
}
if
( flags & VerifyBuiltinFlags::verifyFunctions ) {
functions.
foreach
([&](
auto
fun){
if
( !
isValidBuiltinName
(fun->
name
,
true
) ) {
DAS_FATAL_LOG
(
"
%s - function has incorrect name. expecting snake_case
\n
"
, fun->
name
.
c_str
());
failed =
true
;
}
});
}
if
( flags & VerifyBuiltinFlags::verifyGenerics ) {
generics.
foreach
([&](
auto
fun){
if
( !
isValidBuiltinName
(fun->
name
) ) {
DAS_FATAL_LOG
(
"
%s - generic function has incorrect name. expecting snake_case
\n
"
, fun->
name
.
c_str
());
failed =
true
;
}
});
}
if
( flags & VerifyBuiltinFlags::verifyStructures ) {
structures.
foreach
([&](
auto
st){
if
( !
isValidBuiltinName
(st->
name
) ) {
DAS_FATAL_LOG
(
"
%s - structure has incorrect name. expecting snake_case
\n
"
, st->
name
.
c_str
());
failed =
true
;
}
if
( flags & VerifyBuiltinFlags::verifyStructureFields ) {
for
(
auto
& fd : st->
fields
) {
if
( !
isValidBuiltinName
(fd.
name
) ) {
DAS_FATAL_LOG
(
"
%s.%s - structure field has incorrect name. expecting snake_case
\n
"
, st->
name
.
c_str
(), fd.
name
.
c_str
());
failed =
true
;
}
}
}
});
}
if
( flags & VerifyBuiltinFlags::verifyEnums ) {
enumerations.
foreach
([&](
auto
en){
if
( !
isValidBuiltinName
(en->
name
) ) {
DAS_FATAL_LOG
(
"
%s - enumeration has incorrect name. expecting snake_case
\n
"
, en->
name
.
c_str
());
failed =
true
;
}
if
( flags & VerifyBuiltinFlags::verifyEnumFields ) {
for
(
auto
& fd : en->
list
) {
if
( !
isValidBuiltinName
(fd.
name
) ) {
DAS_FATAL_LOG
(
"
%s.%s - enumeration field has incorrect name. expecting snake_case
\n
"
, en->
name
.
c_str
(), fd.
name
.
c_str
());
failed =
true
;
}
}
}
});
}
if
( failed ) {
DAS_FATAL_ERROR
(
"
verifyBuiltinNames failed
"
);
}
}
void
Module::verifyAotReady
() {
bool
failed =
false
;
functions.
foreach
([&](
auto
fun){
if
( fun->
builtIn
) {
auto
bif = (BuiltInFunction *) fun.
get
();
if
( !bif->
policyBased
&& bif->
cppName
.
empty
() ) {
DAS_FATAL_LOG
(
"
builtin function %s is missing cppName
\n
"
, fun->
describe
().
c_str
());
failed =
true
;
}
}
});
if
( failed ) {
DAS_FATAL_ERROR
(
"
verifyAotReady failed
"
);
}
}
//
MODULE LIBRARY
void
ModuleLibrary::addBuiltInModule
() {
Module *
module
=
Module::require
(
"
$
"
);
DAS_ASSERTF
(
module
,
"
builtin module not found? or you have forgotten to NEED_MODULE(Module_BuiltIn) be called first
"
);
if
(
module
)
addModule
(
module
);
}
bool
ModuleLibrary::addModule
( Module *
module
) {
DAS_ASSERTF
(
module
,
"
module not found? or you have forgotten to NEED_MODULE(Module_<name>) be called first before addModule(require(<name>))
"
);
if
(
module
) {
if
(
find
(modules.
begin
(),modules.
end
(),
module
)==modules.
end
() ) {
for
(
auto
dep :
module
->
requireModule
) {
if
( dep.
first
!=
module
) {
addModule
( dep.
first
);
}
}
modules.
push_back
(
module
);
module
->
addPrerequisits
(*
this
);
return
true
;
}
}
return
false
;
}
void
ModuleLibrary::foreach
(
const
callable<
bool
(Module *
module
)> & func,
const
string & moduleName )
const
{
bool
any = moduleName==
"
*
"
;
for
(
auto
pm : modules ) {
if
( !any && pm->
name
!=moduleName )
continue
;
if
( !
func
(pm) )
break
;
}
}
void
ModuleLibrary::foreach_in_order
(
const
callable<
bool
(Module *
module
)> & func, Module * thisM )
const
{
DAS_ASSERT
(modules.
size
());
//
{builtin} {THIS_MODULE} {require1} {require2} ...
for
(
auto
m = modules.
begin
(); m!=modules.
end
(); ++m ) {
if
( *m==thisM )
continue
;
if
( !
func
(*m) )
return
;
}
func
(thisM);
}
Module *
ModuleLibrary::findModule
(
const
string & mn )
const
{
auto
it =
find_if
(modules.
begin
(), modules.
end
(), [&](Module * mod){
return
mod->
name
== mn;
});
return
it!=modules.
end
() ? *it :
nullptr
;
}
vector<TypeDeclPtr>
ModuleLibrary::findAlias
(
const
string & name, Module * inWhichModule )
const
{
vector<TypeDeclPtr> ptr;
string moduleName, aliasName;
splitTypeName
(name, moduleName, aliasName);
foreach
([&](Module * pm) ->
bool
{
if
( !inWhichModule || inWhichModule->
isVisibleDirectly
(pm) )
if
(
auto
pp = pm->
findAlias
(aliasName) ) {
if
( pp->
isEnumT
() ) {
if
( !pp->
enumType
->
isPrivate
|| pp->
enumType
->
module
==inWhichModule ) {
ptr.
push_back
(pp);
}
}
else
if
( pp->
baseType
==Type::tStructure ) {
if
( !pp->
structType
->
privateStructure
|| pp->
structType
->
module
==inWhichModule ) {
ptr.
push_back
(pp);
}
}
else
{
ptr.
push_back
(pp);
}
}
return
true
;
}, moduleName);
return
ptr;
}
vector<AnnotationPtr>
ModuleLibrary::findAnnotation
(
const
string & name, Module * inWhichModule )
const
{
vector<AnnotationPtr> ptr;
string moduleName, annName;
splitTypeName
(name, moduleName, annName);
foreach
([&](Module * pm) ->
bool
{
if
( !inWhichModule || inWhichModule->
isVisibleDirectly
(pm) )
if
(
auto
pp = pm->
findAnnotation
(annName) )
ptr.
push_back
(pp);
return
true
;
}, moduleName);
return
ptr;
}
vector<TypeInfoMacroPtr>
ModuleLibrary::findTypeInfoMacro
(
const
string & name, Module * inWhichModule )
const
{
vector<TypeInfoMacroPtr> ptr;
string moduleName, annName;
splitTypeName
(name, moduleName, annName);
foreach
([&](Module * pm) ->
bool
{
if
( !inWhichModule || inWhichModule->
isVisibleDirectly
(pm) )
if
(
auto
pp = pm->
findTypeInfoMacro
(annName) )
ptr.
push_back
(pp);
return
true
;
}, moduleName);
return
ptr;
}
vector<StructurePtr>
ModuleLibrary::findStructure
(
const
string & name, Module * inWhichModule )
const
{
vector<StructurePtr> ptr;
string moduleName, funcName;
splitTypeName
(name, moduleName, funcName);
foreach
([&](Module * pm) ->
bool
{
if
( !inWhichModule || inWhichModule->
isVisibleDirectly
(pm) ) {
if
(
auto
pp = pm->
findStructure
(funcName) ) {
if
( !pp->
privateStructure
|| pp->
module
==inWhichModule ) {
ptr.
push_back
(pp);
}
}
}
return
true
;
}, moduleName);
return
ptr;
}
vector<EnumerationPtr>
ModuleLibrary::findEnum
(
const
string & name, Module * inWhichModule )
const
{
vector<EnumerationPtr> ptr;
string moduleName, enumName;
splitTypeName
(name, moduleName, enumName);
foreach
([&](Module * pm) ->
bool
{
if
( !inWhichModule || inWhichModule->
isVisibleDirectly
(pm) ) {
if
(
auto
pp = pm->
findEnum
(enumName) ) {
if
( !pp->
isPrivate
|| pp->
module
==inWhichModule ) {
ptr.
push_back
(pp);
}
}
}
return
true
;
}, moduleName);
return
ptr;
}
TypeDeclPtr
ModuleLibrary::makeStructureType
(
const
string & name )
const
{
auto
t = make_smart<TypeDecl>(Type::tStructure);
auto
structs =
findStructure
(name,
nullptr
);
if
( structs.
size
()==
1
) {
t->
structType
= structs.
back
().
get
();
}
else
{
DAS_FATAL_ERROR
(
"
makeStructureType(%s) failed
\n
"
, name.
c_str
());
return
nullptr
;
}
return
t;
}
vector<AnnotationPtr>
ModuleLibrary::findStaticAnnotation
(
const
string & name )
const
{
vector<AnnotationPtr> ptr;
string moduleName, annName;
splitTypeName
(name, moduleName, annName);
Module::foreach
([&](Module * pm) ->
bool
{
if
(
auto
pp = pm->
findAnnotation
(annName) )
ptr.
push_back
(pp);
return
true
;
});
return
ptr;
}
TypeDeclPtr
ModuleLibrary::makeHandleType
(
const
string & name )
const
{
auto
t = make_smart<TypeDecl>(Type::tHandle);
auto
handles =
findAnnotation
(name,
nullptr
);
#
if
DAS_ALLOW_ANNOTATION_LOOKUP
bool
need_require =
false
;
if
( handles.
size
()==
0
) {
handles =
findStaticAnnotation
(name);
need_require =
true
;
}
#
endif
if
( handles.
size
()==
1
) {
#
if
DAS_ALLOW_ANNOTATION_LOOKUP
if
( need_require ) {
ModuleLibrary *
THAT
= (ModuleLibrary *)
this
;
THAT
->
addModule
(handles.
back
()->
module
);
}
#
endif
if
( handles.
back
()->
rtti_isHandledTypeAnnotation
() ) {
t->
annotation
=
static_cast
<TypeAnnotation*>(handles.
back
().
get
());
}
else
{
DAS_FATAL_ERROR
(
"
makeHandleType(%s) failed, not a handle type
\n
"
, name.
c_str
());
return
nullptr
;
}
}
else
if
( handles.
size
()==
0
) {
#
if
DAS_ALLOW_ANNOTATION_LOOKUP
DAS_FATAL_LOG
(
"
NEED_MODULE(Module_<name>) with %s needs to be before this module binding.
\n
"
, name.
c_str
());
DAS_FATAL_LOG
(
"
Explicit lib.addModule(require(
\"
<name>
\"
)); would be preferable.
\n
"
);
#
else
DAS_FATAL_LOG
(
"
In the bound module missing is lib.addModule(require(
\"
module-name
\"
)) which contains module-name::%s
\n
"
, name.
c_str
());
Module::foreach
([&](Module * mod){
if
( mod->
findAnnotation
(name) ) {
DAS_FATAL_LOG
(
"
\t
Likely candidate is lib.addModule(require(
\"
%s
\"
));
\n
"
, mod->
name
.
c_str
());
}
return
true
;
});
#
endif
DAS_FATAL_ERROR
(
"
makeHandleType(%s) failed, missing annotation
\n
"
, name.
c_str
());
return
nullptr
;
}
else
{
for
(
auto
& h : handles ) {
DAS_FATAL_LOG
(
"
\t
duplicate annotation%s::%s
\n
"
, h->
name
.
c_str
(), name.
c_str
());
}
DAS_FATAL_ERROR
(
"
makeHandleType(%s) failed, duplicate annotation
\n
"
, name.
c_str
());
return
nullptr
;
}
return
t;
}
TypeDeclPtr
ModuleLibrary::makeEnumType
(
const
string & name )
const
{
auto
enums =
findEnum
(name,
nullptr
);
if
( enums.
size
()==
1
) {
return
enums.
back
()->
makeEnumType
();
}
else
{
DAS_FATAL_ERROR
(
"
makeEnumType(%s) failed
\n
"
, name.
c_str
());
return
nullptr
;
}
}
void
ModuleLibrary::reset
() {
for
(
auto
& mod : modules ) {
if
( !mod->
builtIn
) {
delete
mod;
}
}
modules.
clear
();
}
//
Module group
ModuleGroup::~ModuleGroup
() {
reset
();
}
ModuleGroupUserData *
ModuleGroup::getUserData
(
const
string & dataName )
const
{
auto
it = userData.
find
(dataName);
return
it != userData.
end
() ? it->
second
.
get
() :
nullptr
;
}
bool
ModuleGroup::setUserData
( ModuleGroupUserData * data ) {
auto
it = userData.
find
(data->
name
);
if
( it != userData.
end
() ) {
return
false
;
}
userData[data->
name
] =
ModuleGroupUserDataPtr
(data);
return
true
;
}
bool
Module::isVisibleDirectly
( Module * objModule )
const
{
if
( objModule==
this
)
return
true
;
return
requireModule.
find
(objModule) != requireModule.
end
();
}
}
Back
|
FazBrowse Home
|
New Git URL