-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfunction.cc
632 lines (580 loc) · 15.4 KB
/
function.cc
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
#include "primula.h"
#include "../include/statement_t.h"
#include "namespace.h"
void function_overload_t::Parse(int line_num, namespace_t * parent, Code::statement_list_t * source)
{
if (this->space != nullptr)
parent->CreateError(line_num, -7777721, "Function '%s' already defined", (function->name + this->mangle).c_str());
this->space = new namespace_t(parent, namespace_t::function_space, function->name + this->mangle);
this->space->owner_function = this;
// Set space for arguments
for (auto arg : this->arguments)
arg->space = this->space;
if (!parent->no_parse_methods_body)
this->space->Parse(*source);
else
this->source_code = source;
}
static void SplitFunctionArguments(Code::lexem_list_t args, Code::statement_list_t * splitted)
{
enum
{
find_delimiter,
skip_subexpression
} state = find_delimiter;
Code::lexem_list_t argument;
int barakets_counter = 0;
#if MODERN_COMPILER
for (auto lex : args)
{
if (lex.lexem == lt_openbraket)
{
barakets_counter++;
state = skip_subexpression;
}
switch (state)
{
case find_delimiter:
if (lex.lexem == lt_comma)
{
splitted->push_back(argument);
argument.clear();
continue;
}
case skip_subexpression:
if (lex.lexem == lt_closebraket)
{
if (barakets_counter)
barakets_counter--;
else
state = find_delimiter;
}
}
argument.push_back(lex);
continue;
}
#else
Code::lexem_list_t::iterator lex;
for (lex = args.begin(); lex != args.end(); ++lex)
{
if (lex->lexem == lt_openbraket)
{
barakets_counter++;
state = skip_subexpression;
}
switch (state)
{
case find_delimiter:
if (lex->lexem == lt_comma)
{
splitted->push_back(argument);
argument.clear();
continue;
}
case skip_subexpression:
if (lex->lexem == lt_closebraket)
{
if (barakets_counter)
barakets_counter--;
else
state = find_delimiter;
}
}
argument.push_back(*lex);
continue;
}
#endif
if(argument.size() > 0)
splitted->push_back(argument);
}
variable_base_t * function_overload_t::FindArgument(std::string name)
{
variable_base_t * var = nullptr;
#if MODERN_COMPILER
for (auto arg : this->arguments)
{
if (arg.name == name)
{
var = new farg_t(arg);
var->name = name;
break;
}
}
#else
arg_list_t::iterator arg;
for (arg = arguments.begin(); arg != arguments.end(); ++arg)
{
if ((*arg)->name == name)
{
var = (*arg);
// printf("Find argument: %s %p\n", name.c_str(), var);
break;
}
}
#endif
return var;
}
statement_t * function_overload_parser::CallParser(Code::lexem_list_t args_sequence)
{
Code::statement_list_t args;
SplitFunctionArguments(args_sequence, &args);
std::string mangled_name = function->name + mangle;
call_t * result = new call_t(this->space /* Not sure about THIS space */, mangled_name, this);
Code::statement_list_t::iterator arg = args.begin();
arg_list_t::iterator arg_proto = arguments.begin();
while (true)
{
if (arg == args.end())
{
if (arg_proto == arguments.end() || (*arg_proto)->type == nullptr || (*arg_proto)->default_value != nullptr )
break; // No more arguments to function
}
else
{
if (arg_proto == arguments.end() )
{
// Check arguments overflow
space->CreateError(arg->begin()->line_number, -7777721, "too many arguments for function '%s'", mangled_name.c_str());
return nullptr;
}
}
SourcePtr arg_ptr(&*arg);
expression_t * expr = space->ParseExpression(arg_ptr);
if (expr == nullptr)
{
space->CreateError(arg_ptr.line_number , -7777722, "call function '%s' - argument format error", mangled_name.c_str());
return nullptr;
}
if ((*arg_proto)->type != nullptr)
{
if ((*arg_proto)->type != expr->type)
{
space->CreateError(arg_ptr.line_number, -7777722, "function '%s' argument type mismatch", mangled_name.c_str());
return nullptr;
}
// add argument to caller
}
arg++;
if ((*arg_proto)->type != 0) // Ýìóëÿöèÿ ìíîãîòî÷èÿ
(*arg_proto)++;
}
while (arg_proto != arguments.end())
if ((*arg_proto)->type == nullptr)
break;
else
{
// add default argument
(*arg_proto)++;
}
return result;
}
void function_overload_parser::ParseArgunentDefinition(namespace_t * parent_space, SourcePtr source)
{
int status = 0;
type_t * type = nullptr;
farg_t * argument = nullptr;
std::string name;
bool const_arg = false;
enum {
wait_type,
wait_only_pointer,
wait_name,
wait_delimiter,
got_three_dots
} state = wait_type;
for (SourcePtr node = source; status == 0 && node == true; node == true ? node++ : node)
{
switch (state)
{
case wait_type:
{
switch (node.lexem)
{
case lt_const:
{
// type = (type != nullptr) ? new const_t(type) : new const_t;
const_arg = true;
continue;
}
case lt_three_dots:
{
argument = new farg_t(this->space, nullptr, "...");
arguments.push_back(argument);
state = got_three_dots;
status = 1;
continue;
}
default:
type = parent_space->GetBuiltinType(node.lexem);
if (type == nullptr)
{
type = parent_space->TryLexenForType(node);
}
if (type == nullptr)
{
switch (node.lexem)
{
case lt_struct:
case lt_class:
case lt_enum:
state = wait_only_pointer;
continue;
default:
status = -7777786;
parent_space->CreateError(node.line_number, status, "argument's type expected");
node.Finish();
continue;
}
}
if (const_arg)
{
type = new const_t(type);
}
state = wait_name;
continue;;
}
}
case wait_only_pointer:
{
if (node.lexem == lt_mul)
{
type = parent_space->GetBuiltinType(lt_void);
if (const_arg)
type = new const_t(type);
type = new pointer_t(type);
state = wait_name;
continue;;
}
status = -7777786;
parent_space->CreateError(node.line_number, status, "type not defined"); // Name of type?
node.Finish();
continue;
}
case wait_name:
if (node.lexem == lt_mul)
{
type = new pointer_t(type);
continue;
}
if (node.lexem == lt_and)
{
type = new address_t(type);
continue;
}
if (node.lexem == lt_const)
{
type = new const_t(type);
continue;
}
if (node.lexem == lt_comma)
{
argument = new farg_t(this->space, type, ""); // space->CreateVariable(type, name); // Create argument here
arguments.push_back(argument);
const_arg = false;
state = wait_type;
continue;
}
if (node.lexem != lt_word)
{
status = -7777701;
space->CreateError(node.line_number, status, "parser expected name at function_parser::ParseArgunentDefinition");
node.Finish();
continue;
}
name = node.value;
state = wait_delimiter;
break;
case wait_delimiter:
if (node.lexem == lt_openindex)
{
if (node.sequence->size() > 0)
{
status = -7777702;
space->CreateError(node.line_number, status, "array cannot be a functions' agument");
node.Finish();
continue;
}
type = new pointer_t(type);
state = wait_delimiter;
break;
}
if (node.lexem == lt_set)
{
throw "TODO: add default arguments in function_parser::ParseArgunentDefinition";
}
if (node.lexem != lt_comma)
{
status = -7777701;
space->CreateError(node.line_number, status, "parser expected delimiter at function_parser::ParseArgunentDefinition");
node.Finish();
continue;
}
argument = new farg_t(this->space, type, name); // space->CreateVariable(type, name); // Create argument here
arguments.push_back(argument);
const_arg = false;
state = wait_type;
break;
case got_three_dots:
status = -7777701;
space->CreateError(node.line_number, status, "close bracket expected" );
node.Finish();
continue;
default:
throw "function_parser::ParseArgunentDefinition()";
}
}
if (status >= 0)
{
if (type->prop != type_t::void_type)
{
switch (state)
{
case wait_delimiter:
argument = new farg_t(this->space, type, name);
#if ! COMPILER
argument->value.offset = arguments.size() * 4;
#endif
arguments.push_back(argument);
break;
case wait_name:
argument = new farg_t(this->space, type, "");
#if ! COMPILER
argument->value.offset = arguments.size() * 4;
#endif
arguments.push_back(argument);
break;
}
}
else
{
if (arguments.size() != 0)
{
status = -7770701;
space->CreateError(source.line_number, status, "plain void argument mixed with others");
}
}
}
}
void MangleType(const type_t * type, std::string & parent)
{
switch (type->prop)
{
case type_t::pointer_type:
{
parent = "* " + parent;
pointer_t * ptr = (pointer_t*) type;
MangleType(ptr->parent_type, parent);
break;
}
case type_t::constant_type:
{
parent = "const " + parent;
const_t * ptr = (const_t*)type;
MangleType(ptr->parent_type, parent);
break;
}
default:
parent += type->name;
}
}
void function_overload_parser::MangleArguments()
{
if (arguments.size() > 0)
{
#if MODERN_COMPILER
for (auto & arg : arguments)
{
if (arg.type != nullptr)
{
MangleType(arg.type, mangle);
if(&arg != &arguments.back())
mangle += ",";
}
else
mangle += "...";
}
#else
arg_list_t::iterator arg;
for (arg = arguments.begin(); arg != arguments.end(); ++arg)
{
if ( (*arg)->type != nullptr)
{
MangleType( (*arg)->type, mangle);
if (&(*arg) != &arguments.back())
mangle += ",";
}
else
mangle += "...";
}
#endif
}
this->mangle = "@" + mangle;
}
void function_parser::RegisterFunctionOverload(function_overload_t * overload)
{
this->overload_list.push_back(overload);
}
void function_parser::FindBestFunctionOverload(call_t * call)
{
#if MODERN_COMPILER
// function_overload_t * overload = nullptr;
for (auto function : this->overload_list)
{
call->code = function;
arg_list_t::iterator arg_proto = function->arguments.begin();
for (auto & arg : call->arguments)
{
const type_t * proto = arg_proto->type;
const type_t * type = arg->type;
if (proto == nullptr)
{
// We found "..."
break;
}
bool zero_rval = arg->IsConstZero();
if (CompareTypes(proto, type, true, zero_rval) != no_cast)
{
arg_proto++;
continue;
}
else
{
// printf("types_not_match, but we still looking for function\n");
call->code = nullptr;
break;
}
}
if (call->code != nullptr)
break;
}
#else
function_overload_list_t::iterator function;
for (function = overload_list.begin(); function != overload_list.end(); ++function)
{
call->code = *function;
if ((*function)->arguments.size() == 0)
{
if (call->arguments.size() != 0)
continue;
break;
}
arg_list_t::iterator arg_proto = (*function)->arguments.begin();
std::list<expression_t *>::iterator arg;
for (arg = call->arguments.begin(); arg != call->arguments.end(); ++arg)
{
const type_t * proto = (*arg_proto)->type;
const type_t * type = (*arg)->type;
if (proto == nullptr)
{
// We found "..."
break;
}
bool zero_rval = (*arg)->IsConstZero();
if (CompareTypes(proto, type, true, zero_rval) != no_cast)
{
arg_proto++;
continue;
}
else
{
// printf("types_not_match, but we still looking for function\n");
call->code = nullptr;
break;
}
}
if (call->code != nullptr)
break;
}
#endif
if (call->code != nullptr)
{
// printf("found calling function %s%s\n", this->name.c_str(), call->code->mangle.c_str());
++call->code->access_count;
}
else
{
call->caller->CreateError(-1, -7778899, "Unable found calling function %s with requested set of arguments\n", this->name.c_str());
}
}
function_overload_t * function_parser::FindOverload(call_t * call)
{
FindBestFunctionOverload(call);
return call->code;
}
call_t * function_parser::TryCallFunction(namespace_t * space, SourcePtr & arg_list)
{
call_t * call = new call_t(space, this->name, nullptr);
while (arg_list != false)
{
expression_t * arg = space->ParseExpression(arg_list);
if (arg == nullptr)
break;
call->argument_frame_size += arg->type->bitsize >> 3;
call->arguments.push_back(arg);
switch (arg_list.lexem)
{
case lt_comma:
arg_list++;
break;
default:
if (arg_list == true)
space->CreateError(arg_list.line_number, -77712983, "Unparsed lexem '%d' function call in arguments", arg_list.lexem);
break;
}
}
FindBestFunctionOverload(call);
return call;
}
function_overload_t * namespace_t::CreateFunction(type_t *type, std::string name, Code::lexem_list_t * sequence, linkage_t * linkage)
{
function_parser * function = nullptr;
function_overload_t * exist = nullptr;
#if MODERN_COMPILER
auto pair = function_map.find(name);
#else
std::map<std::string, class function_parser*>::iterator pair;
pair = function_map.find(name);
#endif
if (pair != function_map.end())
function = pair->second;
if (function == nullptr)
{
// Function is not overloaded yet, so create base
function = new function_parser(type, name);
RegisterFunction(name, function, true);
}
function_overload_parser * overload = new function_overload_parser(function, linkage);
if (sequence->size() > 0)
overload->ParseArgunentDefinition(this, sequence);
overload->MangleArguments();
#if MODERN_COMPILER
for (auto func : function->overload_list)
{
if(func->mangle == overload->mangle)
{
exist = func;
break;
}
}
#else
function_overload_list_t::iterator func;
for(func = function->overload_list.begin(); func != function->overload_list.end(); ++func)
{
if ((*func)->mangle == overload->mangle)
{
exist = *func;
break;
}
}
#endif
if (exist == nullptr)
{
function->RegisterFunctionOverload(overload);
exist = overload;
}
else
{
delete overload;
}
return exist;
}