1 | // |
---|
2 | // Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo |
---|
3 | // |
---|
4 | // The contents of this file are covered under the licence agreement in the |
---|
5 | // file "LICENCE" distributed with Cforall. |
---|
6 | // |
---|
7 | // MakeLibCfa.cc -- |
---|
8 | // |
---|
9 | // Author : Richard C. Bilson |
---|
10 | // Created On : Sat May 16 10:33:33 2015 |
---|
11 | // Last Modified By : Rob Schluntz |
---|
12 | // Last Modified On : Tue Jan 19 13:20:26 2016 |
---|
13 | // Update Count : 40 |
---|
14 | // |
---|
15 | |
---|
16 | #include "MakeLibCfa.h" |
---|
17 | #include "SynTree/Visitor.h" |
---|
18 | #include "SynTree/Declaration.h" |
---|
19 | #include "SynTree/Type.h" |
---|
20 | #include "SynTree/Expression.h" |
---|
21 | #include "SynTree/Statement.h" |
---|
22 | #include "SynTree/Initializer.h" |
---|
23 | #include "CodeGen/OperatorTable.h" |
---|
24 | #include "UniqueName.h" |
---|
25 | |
---|
26 | namespace LibCfa { |
---|
27 | class MakeLibCfa : public Visitor { |
---|
28 | public: |
---|
29 | void visit( FunctionDecl* funcDecl ); |
---|
30 | void visit( ObjectDecl* objDecl ); |
---|
31 | |
---|
32 | std::list< Declaration* > &get_newDecls() { return newDecls; } |
---|
33 | private: |
---|
34 | std::list< Declaration* > newDecls; |
---|
35 | }; |
---|
36 | |
---|
37 | void makeLibCfa( std::list< Declaration* > &prelude ) { |
---|
38 | MakeLibCfa maker; |
---|
39 | acceptAll( prelude, maker ); |
---|
40 | prelude.splice( prelude.end(), maker.get_newDecls() ); |
---|
41 | } |
---|
42 | |
---|
43 | void MakeLibCfa::visit( FunctionDecl* origFuncDecl ) { |
---|
44 | if ( origFuncDecl->get_linkage() != LinkageSpec::Intrinsic ) return; |
---|
45 | |
---|
46 | FunctionDecl *funcDecl = origFuncDecl->clone(); |
---|
47 | CodeGen::OperatorInfo opInfo; |
---|
48 | bool lookResult = CodeGen::operatorLookup( funcDecl->get_name(), opInfo ); |
---|
49 | assert( lookResult ); |
---|
50 | assert( ! funcDecl->get_statements() ); |
---|
51 | UntypedExpr *newExpr = new UntypedExpr( new NameExpr( funcDecl->get_name() ) ); |
---|
52 | UniqueName paramNamer( "_p" ); |
---|
53 | std::list< DeclarationWithType* >::iterator param = funcDecl->get_functionType()->get_parameters().begin(); |
---|
54 | assert( param != funcDecl->get_functionType()->get_parameters().end() ); |
---|
55 | |
---|
56 | for ( ; param != funcDecl->get_functionType()->get_parameters().end(); ++param ) { |
---|
57 | if ( (*param)->get_name() == "" ) { |
---|
58 | (*param)->set_name( paramNamer.newName() ); |
---|
59 | (*param)->set_linkage( LinkageSpec::C ); |
---|
60 | } |
---|
61 | newExpr->get_args().push_back( new VariableExpr( *param ) ); |
---|
62 | } // for |
---|
63 | |
---|
64 | funcDecl->set_statements( new CompoundStmt( std::list< Label >() ) ); |
---|
65 | newDecls.push_back( funcDecl ); |
---|
66 | |
---|
67 | switch ( opInfo.type ) { |
---|
68 | case CodeGen::OT_INDEX: |
---|
69 | case CodeGen::OT_CALL: |
---|
70 | case CodeGen::OT_PREFIX: |
---|
71 | case CodeGen::OT_POSTFIX: |
---|
72 | case CodeGen::OT_INFIX: |
---|
73 | case CodeGen::OT_PREFIXASSIGN: |
---|
74 | case CodeGen::OT_POSTFIXASSIGN: |
---|
75 | case CodeGen::OT_INFIXASSIGN: |
---|
76 | funcDecl->get_statements()->get_kids().push_back( new ReturnStmt( std::list< Label >(), newExpr ) ); |
---|
77 | break; |
---|
78 | case CodeGen::OT_CTOR: |
---|
79 | // ctors don't return a value |
---|
80 | if ( funcDecl->get_functionType()->get_parameters().size() == 1 ) { |
---|
81 | // intrinsic default constructors should do nothing |
---|
82 | // delete newExpr; |
---|
83 | break; |
---|
84 | } else { |
---|
85 | assert( funcDecl->get_functionType()->get_parameters().size() == 2 ); |
---|
86 | // anything else is a single parameter constructor that is effectively a C-style assignment |
---|
87 | // delete newExpr->get_function(); |
---|
88 | assert(newExpr->get_args().size()==2); |
---|
89 | newExpr->set_function( new NameExpr( "?=?" ) ); |
---|
90 | funcDecl->get_statements()->get_kids().push_back( new ExprStmt( std::list< Label >(), newExpr ) ); |
---|
91 | } |
---|
92 | break; |
---|
93 | case CodeGen::OT_DTOR: |
---|
94 | // intrinsic destructors should do nothing |
---|
95 | // delete newExpr; |
---|
96 | break; |
---|
97 | case CodeGen::OT_CONSTANT: |
---|
98 | case CodeGen::OT_LABELADDRESS: |
---|
99 | // there are no intrinsic definitions of 0/1 or label addresses as functions |
---|
100 | assert( false ); |
---|
101 | } // switch |
---|
102 | } |
---|
103 | |
---|
104 | void MakeLibCfa::visit( ObjectDecl* origObjDecl ) { |
---|
105 | if ( origObjDecl->get_linkage() != LinkageSpec::Intrinsic ) return; |
---|
106 | |
---|
107 | ObjectDecl *objDecl = origObjDecl->clone(); |
---|
108 | assert( ! objDecl->get_init() ); |
---|
109 | std::list< Expression* > noDesignators; |
---|
110 | objDecl->set_init( new SingleInit( new NameExpr( objDecl->get_name() ), noDesignators, false ) ); // cannot be constructed |
---|
111 | newDecls.push_back( objDecl ); |
---|
112 | } |
---|
113 | } // namespace LibCfa |
---|