forked from totalspectrum/spin2cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.c
3498 lines (3313 loc) · 109 KB
/
functions.c
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
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Spin to C/C++ converter
* Copyright 2011-2024 Total Spectrum Software Inc.
* See the file COPYING for terms of use
*
* code for handling functions
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <inttypes.h>
#include "spinc.h"
#include "preprocess.h"
// marked in MarkUsed(); checks for things like try()/catch() that
// may affect global code generation
int gl_features_used;
/* forward declaration */
static int InferTypesStmt(AST *);
static int InferTypesExpr(AST *expr, AST *expectedType);
/*
* how many items will this expression put on the stack?
*/
int
NumExprItemsOnStack(AST *expr)
{
AST *type;
int siz;
if (!expr) {
return 0;
}
type = ExprType(expr);
if (!type) {
return 1;
}
if (type == ast_type_void) {
return 0;
}
if (IsCLang(curfunc->language) && IsArrayType(type)) {
/* convert a to &a[0] */
AST *ref = DupAST(expr);
*expr = *NewAST(AST_ADDROF,
NewAST(AST_ARRAYREF,
ref, AstInteger(0)), NULL);
return 1;
}
if (TypeGoesOnStack(type)) {
// we'll pass this as a pointer
return 1;
}
siz = TypeSize(type);
return ((siz+3) & ~3) / LONG_SIZE;
}
Function *curfunc;
static int visitPass = 1;
static void ReinitFunction(Function *f, int language)
{
f->module = current;
memset(&f->localsyms, 0, sizeof(f->localsyms));
f->localsyms.next = ¤t->objsyms;
if (LangCaseInSensitive(language)) {
f->localsyms.flags = SYMTAB_FLAG_NOCASE;
}
f->optimize_flags = gl_optimize_flags;
f->warn_flags = gl_warn_flags;
}
static const char *
FindAnnotation(AST *annotations, const char *key)
{
const char *ptr;
int len = strlen(key);
while (annotations) {
if (annotations->kind == AST_ANNOTATION) {
ptr = annotations->d.string;
while (ptr && *ptr == '(') ptr++;
while (ptr && *ptr) {
if (!strncmp(ptr, key, len)) {
if (ptr[len] == 0 || !isalpha(ptr[len])) {
return ptr+len;
}
}
while (*ptr && *ptr != ',') ptr++;
if (*ptr == ',') ptr++;
}
}
annotations = annotations->right;
}
return 0;
}
Function *
NewFunction(int language)
{
Function *f;
Function *pf;
f = (Function *)calloc(1, sizeof(*f));
if (!f) {
fprintf(stderr, "FATAL ERROR: Out of memory!\n");
exit(1);
}
/* now link it into the current object */
if (current->functions == NULL) {
current->functions = f;
} else {
pf = current->functions;
while (pf->next != NULL)
pf = pf->next;
pf->next = f;
}
/* and initialize */
ReinitFunction(f, language);
return f;
}
static Symbol *
EnterVariable(int kind, SymbolTable *stab, AST *astname, AST *type, unsigned sym_flag)
{
Symbol *sym;
const char *name;
const char *username;
if (astname->kind == AST_LOCAL_IDENTIFIER) {
name = astname->left->d.string;
username = astname->right->d.string;
} else if (astname->kind == AST_IDENTIFIER) {
name = username = astname->d.string;
} else if (astname->kind == AST_VARARGS) {
name = username = VARARGS_PARAM_NAME;
} else {
ERROR(astname, "Internal error, bad identifier");
return NULL;
}
sym = AddSymbolPlaced(stab, name, kind, (void *)type, username, astname);
if (!sym) {
Symbol *oldsym;
// ignore duplicate definitions for SYM_TEMPVAR
if (kind == SYM_TEMPVAR && (!type || type == ast_type_generic)) {
return sym;
}
oldsym = LookupSymbolInTable(stab, name);
ERROR(astname, "duplicate definition for %s", username);
if (oldsym && oldsym->def) {
NOTE((AST *)oldsym->def, "... previous definition is here");
}
} else {
sym->flags |= sym_flag;
sym->module = (void *)current;
if (current && current != systemModule && IsSpinLang(current->curLanguage)) {
int warn_flags = curfunc ? curfunc->warn_flags : gl_warn_flags;
if ( (warn_flags & WARN_HIDE_MEMBERS)
|| ( (warn_flags & WARN_LANG_EXTENSIONS) && current->curLanguage == LANG_SPIN_SPIN2 ) )
{
Symbol *sym2;
switch (kind) {
case SYM_PARAMETER:
case SYM_RESULT:
case SYM_LOCALVAR:
if ( (sym2 = FindSymbol(¤t->objsyms, username)) != 0) {
switch (sym2->kind) {
case SYM_WEAK_ALIAS:
case SYM_FUNCTION:
/* no warning for this */
break;
default:
WARNING(astname, "definition of %s hides a member variable", username);
if (sym2->def) {
NOTE((AST *)sym2->def, "previous definition of %s is here", username);
}
break;
}
}
break;
default:
break;
}
}
}
}
return sym;
}
static AST *
ArrayDeclType(AST *indices, AST *basetype, AST *baseptr, AST *ident)
{
AST *arraytype;
if (!indices) {
return basetype;
}
if (indices->kind == AST_EXPRLIST) {
basetype = ArrayDeclType(indices->right, basetype, baseptr, ident);
arraytype = NewAST(AST_ARRAYTYPE, basetype, indices->left);
} else {
if (!IsConstExpr(indices)) {
if (ident) {
ERROR(ident, "Unable to determine size of array %s", GetUserIdentifierName(ident));
indices = AstInteger(1);
}
}
arraytype = NewAST(AST_ARRAYTYPE, basetype, indices);
}
arraytype->d.ptr = baseptr;
return arraytype;
}
int
EnterVars(int kind, SymbolTable *stab, AST *defaulttype, AST *varlist, int offset, int isUnion, unsigned sym_flags)
{
AST *lower;
AST *ast;
Symbol *sym;
AST *actualtype;
int size;
int typesize;
ASTReportInfo saveinfo;
for (lower = varlist; lower; lower = lower->right) {
if (lower->kind == AST_LISTHOLDER) {
ast = lower->left;
AstReportAs(ast, &saveinfo);
if (ast->kind == AST_DECLARE_VAR) {
actualtype = ast->left;
ast = ast->right;
} else {
actualtype = defaulttype;
}
if (actualtype) {
typesize = CheckedTypeSize(actualtype);
} else {
typesize = 4;
}
if (kind == SYM_LOCALVAR || kind == SYM_TEMPVAR || kind == SYM_PARAMETER) {
// keep things in registers, generally
if (typesize < 4) typesize = 4;
}
if (!ast) {
ast = AstTempIdentifier("_param_");
}
switch (ast->kind) {
case AST_INTTYPE:
case AST_UNSIGNEDTYPE:
case AST_ARRAYTYPE:
case AST_PTRTYPE:
case AST_FLOATTYPE:
case AST_VOIDTYPE:
case AST_GENERICTYPE:
case AST_MODIFIER_CONST:
case AST_MODIFIER_VOLATILE:
case AST_FUNCTYPE:
case AST_OBJECT:
// a type with no associated variable
actualtype = ast;
ast = AstTempIdentifier("_param_");
// fall through
case AST_VARARGS:
case AST_IDENTIFIER:
case AST_LOCAL_IDENTIFIER:
sym = EnterVariable(kind, stab, ast, actualtype, sym_flags);
if (sym) sym->offset = offset;
if (ast->kind != AST_VARARGS && !isUnion) {
offset += typesize;
}
break;
case AST_ARRAYDECL:
{
AST *arraytype;
AST *indices = ast->right;
arraytype = ArrayDeclType(indices, actualtype, (AST *)ast->d.ptr, ast->left);
sym = EnterVariable(kind, stab, ast->left, arraytype, sym_flags);
if (sym) sym->offset = offset;
if (!isUnion) {
size = TypeSize(arraytype);
offset += size;
}
break;
}
case AST_ANNOTATION:
/* just ignore it */
break;
case AST_ASSIGN:
{
AST *id = ast->left;
while (id && id->kind != AST_IDENTIFIER) {
id = id->left;
}
if (id) {
ERROR(ast, "initialization is not supported for member variable %s", GetIdentifierName(id));
} else {
ERROR(ast, "initialization is not supported for variables of this kind");
}
return offset;
}
default:
/* this may be a type with no variable */
ERROR(ast, "Internal error, bad AST value %d", ast->kind);
break;
}
} else {
ERROR(lower, "Expected list of variables, found %d instead", lower->kind);
AstReportDone(&saveinfo);
return offset;
}
AstReportDone(&saveinfo);
}
return offset;
}
/*
* declare a function and create a Function structure for it
*/
AST *
DeclareFunction(Module *P, AST *rettype, int is_public, AST *funcdef, AST *body, AST *annotation, AST *comment)
{
AST *funcblock;
AST *holder;
AST *funcdecl;
AST *retinfoholder;
holder = NewAST(AST_FUNCHOLDER, funcdef, body);
holder->d.ival = P->curLanguage;
funcblock = NewAST(is_public ? AST_PUBFUNC : AST_PRIFUNC, holder, annotation);
funcblock->d.ptr = (void *)comment;
funcblock = NewAST(AST_LISTHOLDER, funcblock, NULL);
funcdecl = funcdef->left;
// a bit of a hack here, undone in doDeclareFunction
retinfoholder = NewAST(AST_RETURN, rettype, funcdecl->right);
funcdecl->right = retinfoholder;
P->funcblock = AddToList(P->funcblock, funcblock);
return funcblock->left;
}
/* like DeclareFunction, but takes a function type */
AST *
DeclareTypedFunction(Module *P, AST *ftype, AST *name, int is_public, AST *fbody, AST *annotations, AST *comment)
{
AST *funcvars, *funcdef;
AST *funcdecl = NewAST(AST_FUNCDECL, name, NULL);
AST *type;
if (ftype->kind == AST_STATIC) {
is_public = 0;
ftype = ftype->left;
}
type = ftype->left;
funcvars = NewAST(AST_FUNCVARS, ftype->right, NULL);
funcdef = NewAST(AST_FUNCDEF, funcdecl, funcvars);
return DeclareFunction(P, type, is_public, funcdef, fbody, annotations, comment);
}
static AST *
TranslateToExprList(AST *listholder)
{
AST *head, *tail;
if (!listholder) {
return NULL;
}
tail = TranslateToExprList(listholder->right);
head = NewAST(AST_EXPRLIST, listholder->left, tail);
return head;
}
void
MarkSystemFuncUsed(const char *name)
{
Symbol *sym;
Function *calledf;
sym = FindSymbol(&systemModule->objsyms, name);
if (!sym) {
//it's OK, not all back ends have all system functions
//ERROR(NULL, "Internal error could not find %s", name);
return;
}
if (sym->kind == SYM_FUNCTION) {
calledf = (Function *)sym->v.ptr;
AddIndirectFunctionCall(calledf);
}
}
//
// try to infer the return type from a lambda expression
//
static AST *
GuessLambdaReturnType(AST *params, AST *body)
{
AST *expr;
AST *r;
if (body->kind == AST_STMTLIST) {
// look for a return statement
while (body && body->right != NULL && body->right->kind == AST_STMTLIST) {
body = body->right;
}
if (body->right != NULL)
return NULL;
body = body->left;
}
if (body->kind == AST_COMMENTEDNODE) {
body = body->left;
}
if (body->kind != AST_RETURN) {
return NULL;
}
expr = body->left;
if (!expr) {
return NULL;
}
// HACK: temporarily declare the parameters as symbols
{
SymbolTable *table = (SymbolTable *)calloc(1, sizeof(SymbolTable));
table->next = &curfunc->localsyms;
EnterVars(SYM_PARAMETER, table, NULL, params, 0, 0, 0);
r = ExprTypeRelative(table, expr, NULL);
free(table);
}
return r;
}
//
// undo a local variable delcaration
//
static void
UndoLocalIdentifier(AST *body, AST *ident)
{
if (!body || body == ident) {
return;
}
if (body->kind == AST_LOCAL_IDENTIFIER
&& !strcmp(body->left->d.string, ident->left->d.string))
{
body->kind = AST_IDENTIFIER;
body->d.string = ident->right->d.string;
body->left = body->right = NULL;
}
else
{
UndoLocalIdentifier(body->left, ident);
UndoLocalIdentifier(body->right, ident);
}
}
static bool AllZeros(AST *expr) {
if (!expr) return true;
if (expr->kind == AST_INTEGER && expr->d.ival == 0) return true;
if (expr->kind == AST_STRINGPTR) {
return AstStringLen(expr) == 0;
}
if (expr->kind == AST_EXPRLIST) {
if (!AllZeros(expr->left)) return false;
return AllZeros(expr->right);
}
return false;
}
//
// add initializers of the form "ident = expr" to a given sequence "seq"
// the type of ident is "basetype"
//
static AST *
AddInitializers(AST *seq, AST *ident, AST *expr, AST *basetype)
{
AST *assign;
AST *params;
AST *sub;
AST *subtype;
int i;
int limit;
ASTReportInfo saveinfo;
if (!expr) return seq;
AstReportAs(expr, &saveinfo);
if (TypeGoesOnStack(basetype)) {
if (AllZeros(expr)) {
params = NewAST(AST_EXPRLIST,
NewAST(AST_ABSADDROF, ident, NULL),
NewAST(AST_EXPRLIST, AstInteger(0),
NewAST(AST_EXPRLIST, AstInteger(TypeSize(basetype)), NULL)));
assign = NewAST(AST_FUNCCALL,
AstIdentifier("__builtin_memset"),
params);
seq = AddToList(seq, NewAST(AST_SEQUENCE, assign, NULL));
AstReportDone(&saveinfo);
return seq;
} else if (IsConstInitializer(expr)) {
AST *tmpname = AstTempIdentifier("_init_");
AST *tmpvar = NewAST(AST_ASSIGN, tmpname, expr);
DeclareOneGlobalVar(current, tmpvar, basetype, 1);
params = NewAST(AST_EXPRLIST,
NewAST(AST_ABSADDROF, ident, NULL),
NewAST(AST_EXPRLIST, NewAST(AST_ABSADDROF, tmpname, NULL),
NewAST(AST_EXPRLIST, AstInteger(TypeSize(basetype)), NULL)));
assign = NewAST(AST_FUNCCALL,
AstIdentifier("__builtin_memcpy"),
params);
seq = AddToList(seq, NewAST(AST_SEQUENCE, assign, NULL));
AstReportDone(&saveinfo);
return seq;
}
}
if (expr->kind == AST_STRINGPTR && IsArrayType(basetype)) {
int srclen, dstlen;
srclen = AstStringLen(expr);
dstlen = TypeSize(basetype);
if (srclen > dstlen) {
ERROR(expr, "Copying %d bytes to array which has only %d bytes space\n", srclen, dstlen);
}
params = NewAST(AST_EXPRLIST,
ident,
NewAST(AST_EXPRLIST,
expr, NULL));
assign = NewAST(AST_FUNCCALL,
AstIdentifier("__builtin_strcpy"),
params);
seq = AddToList(seq, NewAST(AST_SEQUENCE, assign, NULL));
AstReportDone(&saveinfo);
return seq;
} else if (expr->kind == AST_EXPRLIST) {
expr = FixupInitList(basetype, expr);
if (IsArrayType(basetype)) {
if (!IsConstExpr(basetype->right)) {
ERROR(ident, "Variable length arrays not supported yet");
AstReportDone(&saveinfo);
return seq;
}
limit = EvalConstExpr(basetype->right);
subtype = basetype->left;
for (i = 0; expr && i < limit; i++) {
sub = NewAST(AST_ARRAYREF, ident, AstInteger(i));
seq = AddInitializers(seq, sub, expr->left, subtype);
expr = expr->right;
}
while (i < limit) {
sub = NewAST(AST_ARRAYREF, ident, AstInteger(i));
seq = AddInitializers(seq, sub, AstInteger(0), basetype->left);
i++;
}
AstReportDone(&saveinfo);
return seq;
}
if (IsClassType(basetype)) {
Module *P = GetClassPtr(basetype);
AST *varlist = P ? P->finalvarblock : NULL;
AST *decl;
AST *subident;
AST *curexpr;
while (varlist && varlist->kind == AST_LISTHOLDER) {
decl = varlist->left;
varlist = varlist->right;
if (decl && decl->kind == AST_DECLARE_VAR) {
subtype = decl->left;
decl = decl->right;
while (decl) {
if (decl->kind == AST_LISTHOLDER) {
subident = decl->left;
decl = decl->right;
} else {
subident = decl;
decl = NULL;
}
if (expr) {
if (expr->kind == AST_EXPRLIST) {
curexpr = expr->left;
expr = expr->right;
} else {
curexpr = expr;
expr = NULL;
}
} else {
curexpr = AstInteger(0);
}
sub = NewAST(AST_METHODREF, ident, subident);
seq = AddInitializers(seq, sub, curexpr, subtype);
}
}
}
AstReportDone(&saveinfo);
return seq;
}
}
if (IsConstType(basetype)) {
// cast to avoid warnings
ident = NewAST(AST_CAST, RemoveTypeModifiers(basetype), ident);
}
assign = AstAssign(ident, expr);
assign->kind = AST_ASSIGN_INIT;
seq = AddToList(seq, NewAST(AST_SEQUENCE, assign, NULL));
AstReportDone(&saveinfo);
return seq;
}
//
// declare some static variables
//
//
// FIXME: someday we should implement scopes other
// than function scope
//
static void
findLocalsAndDeclare(Function *func, AST *ast)
{
AST *identlist;
AST *ident;
AST *name;
AST *datatype;
AST *basetype;
AST *expr;
AST *seq = NULL; // sequence for variable initialization
AST *arrayinfo = NULL;
int kind;
bool skipDef;
if (!ast) return;
kind = ast->kind;
switch(kind) {
case AST_DECLARE_ALIAS:
/* this case may be obsolete now */
name = ast->left;
ident = ast->right;
DeclareAlias(&func->localsyms, name, ident);
AstNullify(ast); // delete the declaration so we don't see it again
return;
case AST_GLOBALVARS:
ERROR(ast, "global variable declarations not allowed in functions");
return;
case AST_CAST:
return;
case AST_DECLARE_VAR:
case AST_DECLARE_VAR_WEAK:
identlist = ast->right;
basetype = ast->left;
if (basetype && basetype->kind == AST_STATIC) {
ERROR(ast, "Internal error, saw STATIC");
return;
}
while (identlist) {
datatype = expr = NULL;
if (identlist->kind == AST_LISTHOLDER) {
ident = identlist->left;
identlist = identlist->right;
} else {
ident = identlist;
identlist = NULL;
}
if (ident->kind == AST_ASSIGN) {
// check for x[] = {a, b, c} type initializers here
if (IsArrayType(basetype) && basetype->right == NULL) {
AST *tmp = FixupInitList(basetype, ident->right);
ident->right = tmp;
//fixupInitializer(current, ident->right, basetype);
if (ident->right->kind == AST_EXPRLIST) {
basetype->right = AstInteger(AstListLen(ident->right));
} else if (ident->right->kind == AST_STRINGPTR) {
basetype->right = AstInteger(AstStringLen(ident->right));
}
}
// write out an initialization for the variable
expr = ident->right;
ident = ident->left;
seq = AddInitializers(seq, ident, expr, basetype);
// scan for lambdas?
findLocalsAndDeclare(func, expr);
}
if (ident->kind == AST_ARRAYDECL) {
name = ident->left;
arrayinfo = ident;
} else {
name = ident;
arrayinfo = NULL;
}
if (kind == AST_DECLARE_VAR_WEAK) {
Symbol *sym = LookupSymbol(name->d.string);
skipDef = 0 != sym;
if (sym && sym->kind == SYM_CONSTANT) {
ERROR(ast, "Attempt to redefine constant %s as a variable", sym->user_name);
skipDef = false;
}
} else {
skipDef = false;
}
if (!skipDef && basetype && basetype->kind == AST_FUNCTYPE
&& IsCLang(func->language))
{
// ignore definitions of global functions and such
// complication: since it's a local variable we've defined
// an alias for it; we need to undo that here
skipDef = true;
if (ident->kind == AST_LOCAL_IDENTIFIER) {
UndoLocalIdentifier(func->body, ident);
}
}
if (!skipDef) {
if (basetype) {
datatype = basetype;
} else {
if (expr) {
datatype = ExprType(expr);
}
}
if (!datatype) {
datatype = InferTypeFromName(name);
}
if (arrayinfo) {
datatype = ArrayDeclType(arrayinfo->right, datatype, (AST *)arrayinfo->d.ptr, ident);
}
if (datatype && datatype->kind == AST_TYPEDEF) {
datatype = datatype->left;
EnterVariable(SYM_TYPEDEF, &func->localsyms, name, datatype, 0);
} else {
AddLocalVariable(func, name, datatype, SYM_LOCALVAR);
}
}
}
// now we can overwrite the original variable declaration
if (seq) {
*ast = *seq;
} else {
AstNullify(ast);
}
return;
case AST_LAMBDA:
// we will need a closure for this function
if (!func->closure) {
const char *closure_name;
Module *C;
Module *Parent = func->module;
AST *closure_type;
closure_name = NewTemporaryVariable("__closure__", NULL);
C = func->closure = NewModule(closure_name, current->curLanguage);
C->Lptr = current->Lptr;
closure_type = NewAbstractObject(AstIdentifier(closure_name), NULL, 0);
closure_type->d.ptr = func->closure;
AddSymbol(&func->localsyms, closure_name, SYM_CLOSURE, closure_type, NULL);
C->subclasses = Parent->subclasses;
C->objsyms.next = &Parent->objsyms;
Parent->subclasses = C;
C->superclass = Parent;
// we have to mark the global bytemove and _gc_alloc_managed functions
// as in used
MarkSystemFuncUsed("_gc_alloc_managed");
if (!TraditionalBytecodeOutput()) {
MarkSystemFuncUsed("bytemove");
}
}
{
AST *ftype = ast->left;
AST *fbody = ast->right;
AST *name = AstIdentifier(NewTemporaryVariable("func_", NULL));
AST *ptrref;
// check for the return type; we may have to infer this
if (!ftype->left) {
ftype->left = GuessLambdaReturnType(ftype->right, fbody);
}
// declare the lambda function
DeclareTypedFunction(func->closure, ftype, name, 1, fbody, NULL, NULL);
// now turn the lambda into a pointer deref
ptrref = NewAST(AST_METHODREF, AstIdentifier(func->closure->classname), name);
// AST_ADDROF can allow a type on the right, which will help
// ExprType() to avoid having to evaluate the ADDROF on an as
// yet partially defined function
ptrref = NewAST(AST_ADDROF, ptrref, ftype);
*ast = *ptrref;
}
// do *not* recurse into sub-parts of the lambda!
return;
default:
break;
}
findLocalsAndDeclare(func, ast->left);
findLocalsAndDeclare(func, ast->right);
}
static void
AddClosureSymbol(Function *f, Module *P, AST *ident)
{
AST *typ = NULL;
if (ident->kind == AST_DECLARE_VAR) {
typ = ident->left;
ident = ident->right;
}
if (ident->kind != AST_IDENTIFIER) {
ERROR(ident, "internal error in closure def");
return;
}
if (!typ) {
typ = ExprTypeRelative(&f->localsyms, ident, P);
}
MaybeDeclareMemberVar(P, ident, typ, 0, NORMAL_VAR);
}
static void
AdjustParameterTypes(AST *paramlist, int lang)
{
AST *param;
AST *type;
while (paramlist) {
param = paramlist->left;
if (param->kind == AST_DECLARE_VAR) {
type = param->left;
if ( (IsArrayType(type) && (IsCLang(lang) || IsBasicLang(lang)))
|| (IsClassType(type) && (IsBasicLang(lang) || IsPythonLang(lang)) )
)
{
type = BaseType(type);
type = NewAST(AST_PTRTYPE, type, NULL);
param->left = type;
}
else if (IsClassType(type) && IsCLang(lang) && TypeGoesOnStack(type) ) {
type = BaseType(type);
type = NewAST(AST_COPYREFTYPE, type, NULL);
param->left = type;
}
}
paramlist = paramlist->right;
}
}
static Function *
doDeclareFunction(AST *funcblock)
{
AST *holder;
AST *funcdef;
AST *body;
AST *annotation;
AST *srcname;
Function *fdef;
Function *oldcur = curfunc;
AST *vars;
AST *src;
AST *comment;
AST *defparams;
AST *retinfo;
int is_public;
int language;
const char *funcname_internal;
const char *funcname_user;
AST *oldtype = NULL;
Symbol *sym;
int is_cog;
is_public = (funcblock->kind == AST_PUBFUNC);
holder = funcblock->left;
annotation = funcblock->right;
funcdef = holder->left;
body = holder->right;
comment = (AST *)funcblock->d.ptr;
language = holder->d.ival;
if (funcdef->kind != AST_FUNCDEF || funcdef->left->kind != AST_FUNCDECL) {
ERROR(funcdef, "Internal error, bad function definition");
return NULL;
}
src = funcdef->left;
is_cog = CODE_PLACE_DEFAULT;
if (gl_p2 && FindAnnotation(annotation, "lut") != 0) {
is_cog = CODE_PLACE_LUT;
gl_have_lut++;
} else if (FindAnnotation(annotation, "cog") != 0) {
is_cog = CODE_PLACE_COG;
} else if (FindAnnotation(annotation, "hub") != 0) {
is_cog = CODE_PLACE_HUB;
}
srcname = src->left;
if (srcname->kind == AST_LOCAL_IDENTIFIER) {
funcname_internal = srcname->left->d.string;
funcname_user = srcname->right->d.string;
} else if (srcname->kind == AST_IDENTIFIER) {
funcname_internal = funcname_user = srcname->d.string;
} else {
ERROR(funcdef, "Internal error, no function name");
return NULL;
}
/* look for an existing definition */
sym = FindSymbol(¤t->objsyms, funcname_internal);
if (sym && sym->kind == SYM_VARIABLE) {
AST *typ = (AST *)sym->v.ptr;
if (typ && typ->kind == AST_FUNCTYPE) {
// We are redefining a bare function declaration in a class
fdef = NewFunction(language);
sym->v.ptr = (void *)fdef;
sym->kind = SYM_FUNCTION;
}
}
if (sym && sym->kind != SYM_WEAK_ALIAS) {
if (sym->kind != SYM_FUNCTION) {
ERROR(funcdef, "Redefining %s as a function or subroutine", funcname_user);
if (sym->def) {
NOTE((AST *)sym->def, "... previous definition is here");
}
return NULL;
}
fdef = (Function *)sym->v.ptr;
oldtype = fdef->overalltype;
} else {
fdef = NewFunction(language);
/* define the function symbol itself */
/* note: static functions may get alias names, so we have to look
* for an alias and declare under that name; that's what AliasName()
* does
*/
sym = AddSymbolPlaced(¤t->objsyms, funcname_internal, SYM_FUNCTION, fdef, funcname_user, funcdef);
if (!is_public) {
sym->flags |= SYMF_PRIVATE;
}
sym->module = current;
}
if (FindAnnotation(annotation, "constructor")) fdef->attributes |= FUNC_ATTR_CONSTRUCTOR;
if (FindAnnotation(annotation, "destructor")) fdef->attributes |= FUNC_ATTR_DESTRUCTOR;
if (FindAnnotation(annotation, "needsinit")) fdef->attributes |= FUNC_ATTR_NEEDSINIT;
if (FindAnnotation(annotation, "complexio")) fdef->attributes |= FUNC_ATTR_COMPLEXIO;
if (FindAnnotation(annotation, "inline")) fdef->prefer_inline = 1;
if (fdef->body) {
// we already saw a definition for the function; if this was just
// an alias then it may be OK
if (fdef->body->kind == AST_STRING) {
if (body && body->kind == AST_STRING) {
if (0 != strcmp(fdef->body->d.string, body->d.string)) {
ERROR(funcdef, "different __fromfile strings for function %s", funcname_user);
}
return fdef; // nothing else we need to do here
}
} else {
if (body && body->kind == AST_STRING) {
/* providing a __fromfile() declaration after we saw
a real declaration; just ignore it */
return fdef;
} else if (!AstMatch(fdef->decl, funcdef) || !AstBodyMatch(fdef->body, body)) {
ERROR(funcdef, "redefining function or subroutine %s", funcname_user);
NOTE(fdef->decl, "the previous definition is here");
} else {
if (DifferentLineNumbers(funcdef, fdef->decl)) {
WARNING(funcdef, "duplicate definition for %s", funcname_user);
NOTE(fdef->decl, "the previous definition is here");
}
}
}
// if we get here then we are redefining a previously seen __fromfile
// ignore the old stub function and start with a fresh one
// BEWARE: there's some stuff (like fdef->next) we do not want to lose
ReinitFunction(fdef, fdef->language);
}
{
const char *opts = FindAnnotation(annotation, "opt");
if (opts) {
//printf("Optimize string: [%s]\n", opt);
if (opts[0] != '(') {
ERROR(annotation, "optimization options must be enclosed in parentheses");
} else {
ParseOptimizeString(annotation, opts+1, &fdef->optimize_flags);
}
}
}
{
const char *opts = FindAnnotation(annotation, "warn");
if (opts) {
//printf("Optimize string: [%s]\n", opt);
if (opts[0] != '(') {
ERROR(annotation, "warning options must be enclosed in parentheses");
} else {
ParseWarnString(annotation, opts+1, &fdef->warn_flags);
}
}
}
if (FindAnnotation(annotation, "noinline") != 0) {
fdef->no_inline = 1;
}
{
const char *spfunc = FindAnnotation(annotation, "specialfunc");
if (spfunc) {
//printf("Optimize string: [%s]\n", opt);