31
31
32
32
namespace LCompilers {
33
33
34
+ enum SpecialFunc {
35
+ Printf,
36
+ Exit
37
+ };
38
+
39
+ std::map<std::string, SpecialFunc> special_function_map = {
40
+ {" printf" , SpecialFunc::Printf},
41
+ {" exit" , SpecialFunc::Exit}
42
+ };
43
+
34
44
class ClangASTtoASRVisitor : public clang ::RecursiveASTVisitor<ClangASTtoASRVisitor> {
35
45
36
46
public:
@@ -140,6 +150,16 @@ class ClangASTtoASRVisitor: public clang::RecursiveASTVisitor<ClangASTtoASRVisit
140
150
tu->m_symtab ->add_symbol (prog_name, ASR::down_cast<ASR::symbol_t >(prog));
141
151
}
142
152
153
+ void remove_special_functions () {
154
+ ASR::TranslationUnit_t* tu = (ASR::TranslationUnit_t*)this ->tu ;
155
+ if (tu->m_symtab ->resolve_symbol (" printf" )) {
156
+ tu->m_symtab ->erase_symbol (" printf" );
157
+ }
158
+ if (tu->m_symtab ->resolve_symbol (" exit" )) {
159
+ tu->m_symtab ->erase_symbol (" exit" );
160
+ }
161
+ }
162
+
143
163
bool TraverseTranslationUnitDecl (clang::TranslationUnitDecl *x) {
144
164
SymbolTable *parent_scope = al.make_new <SymbolTable>(nullptr );
145
165
current_scope = parent_scope;
@@ -151,8 +171,7 @@ class ClangASTtoASRVisitor: public clang::RecursiveASTVisitor<ClangASTtoASRVisit
151
171
}
152
172
153
173
construct_program ();
154
- // remove printf definition
155
- ((ASR::TranslationUnit_t*)tu)->m_symtab ->erase_symbol (" printf" );
174
+ remove_special_functions ();
156
175
157
176
return true ;
158
177
}
@@ -339,27 +358,6 @@ class ClangASTtoASRVisitor: public clang::RecursiveASTVisitor<ClangASTtoASRVisit
339
358
return true ;
340
359
}
341
360
342
- void handle_printf (clang::CallExpr *x) {
343
- Vec<ASR::expr_t *> args;
344
- args.reserve (al, 1 );
345
- bool skip_format_str = true ;
346
- for (auto *p : x->arguments ()) {
347
- TraverseStmt (p);
348
- if (skip_format_str) {
349
- skip_format_str = false ;
350
- continue ;
351
- }
352
- args.push_back (al, ASRUtils::EXPR (tmp));
353
- }
354
- tmp = ASR::make_Print_t (al, Lloc (x), args.p , args.size (), nullptr , nullptr );
355
- is_stmt_created = true ;
356
- }
357
-
358
- bool check_printf (ASR::expr_t * callee) {
359
- ASR::Var_t* callee_Var = ASR::down_cast<ASR::Var_t>(callee);
360
- return std::string (ASRUtils::symbol_name (callee_Var->m_v )) == " printf" ;
361
- }
362
-
363
361
bool TraverseCXXOperatorCallExpr (clang::CXXOperatorCallExpr* x) {
364
362
clang::Expr* callee = x->getCallee ();
365
363
TraverseStmt (callee);
@@ -385,11 +383,41 @@ class ClangASTtoASRVisitor: public clang::RecursiveASTVisitor<ClangASTtoASRVisit
385
383
return true ;
386
384
}
387
385
386
+ bool check_and_handle_special_function (
387
+ clang::CallExpr *x, ASR::expr_t * callee) {
388
+ ASR::Var_t* callee_Var = ASR::down_cast<ASR::Var_t>(callee);
389
+ std::string func_name = std::string (ASRUtils::symbol_name (callee_Var->m_v ));
390
+ if (special_function_map.find (func_name) == special_function_map.end ()) {
391
+ return false ;
392
+ }
393
+ SpecialFunc sf = special_function_map[func_name];
394
+ Vec<ASR::expr_t *> args;
395
+ args.reserve (al, 1 );
396
+ bool skip_format_str = true ;
397
+ for (auto *p : x->arguments ()) {
398
+ TraverseStmt (p);
399
+ if (sf == SpecialFunc::Printf && skip_format_str) {
400
+ skip_format_str = false ;
401
+ continue ;
402
+ }
403
+ args.push_back (al, ASRUtils::EXPR (tmp));
404
+ }
405
+ if (sf == SpecialFunc::Printf) {
406
+ tmp = ASR::make_Print_t (al, Lloc (x), args.p , args.size (), nullptr , nullptr );
407
+ } else if (sf == SpecialFunc::Exit) {
408
+ LCOMPILERS_ASSERT (args.size () == 1 );
409
+ tmp = ASR::make_Stop_t (al, Lloc (x), args[0 ]);
410
+ } else {
411
+ throw std::runtime_error (" Only printf and exit special functions supported" );
412
+ }
413
+ is_stmt_created = true ;
414
+ return true ;
415
+ }
416
+
388
417
bool TraverseCallExpr (clang::CallExpr *x) {
389
418
TraverseStmt (x->getCallee ());
390
419
ASR::expr_t * callee = ASRUtils::EXPR (tmp);
391
- if ( check_printf (callee) ) {
392
- handle_printf (x);
420
+ if ( check_and_handle_special_function (x, callee) ) {
393
421
return true ;
394
422
}
395
423
0 commit comments