1 | //
|
---|
2 | // Cforall Version 1.0.0 Copyright (C) 2016 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 | // ExceptDecl.cc --
|
---|
8 | //
|
---|
9 | // Author : Henry Xue
|
---|
10 | // Created On : Tue Jul 20 04:10:50 2021
|
---|
11 | // Last Modified By : Henry Xue
|
---|
12 | // Last Modified On : Tue Jul 20 04:10:50 2021
|
---|
13 | // Update Count : 1
|
---|
14 | //
|
---|
15 |
|
---|
16 | #include "ExceptDecl.h"
|
---|
17 |
|
---|
18 | #include "Common/PassVisitor.h" // for PassVisitor
|
---|
19 | #include "SynTree/Mutator.h" // for mutateAll
|
---|
20 | #include "Virtual/Tables.h" // for helpers
|
---|
21 |
|
---|
22 | namespace ControlStruct {
|
---|
23 |
|
---|
24 | StructDecl * ehmTypeIdStruct( const std::string & exceptionName, std::list< TypeDecl *> * parameters ) {
|
---|
25 | StructDecl * structDecl = new StructDecl( Virtual::typeIdType( exceptionName ) );
|
---|
26 | structDecl->members.push_back( new ObjectDecl(
|
---|
27 | "parent",
|
---|
28 | noStorageClasses,
|
---|
29 | LinkageSpec::Cforall,
|
---|
30 | nullptr,
|
---|
31 | new PointerType( noQualifiers,
|
---|
32 | new TypeInstType( Type::Const, "__cfavir_type_info", false ) ),
|
---|
33 | nullptr
|
---|
34 | ) );
|
---|
35 | structDecl->set_body( true );
|
---|
36 | if ( parameters ) {
|
---|
37 | structDecl->parameters = *parameters;
|
---|
38 | }
|
---|
39 | return structDecl;
|
---|
40 | }
|
---|
41 |
|
---|
42 | ObjectDecl * ehmTypeIdValue( const std::string & exceptionName, std::list< Expression *> * parameters ) {
|
---|
43 | StructInstType * structInstType = new StructInstType( Type::Const, Virtual::typeIdType( exceptionName ) );
|
---|
44 | if ( parameters ) {
|
---|
45 | structInstType->parameters = *parameters;
|
---|
46 | }
|
---|
47 | return new ObjectDecl(
|
---|
48 | Virtual::typeIdName( exceptionName ),
|
---|
49 | noStorageClasses,
|
---|
50 | LinkageSpec::Cforall,
|
---|
51 | nullptr,
|
---|
52 | structInstType,
|
---|
53 | new ListInit( { new SingleInit(
|
---|
54 | new AddressExpr( new NameExpr( "__cfatid_exception_t" ) )
|
---|
55 | ) }, {}, true ),
|
---|
56 | { new Attribute( "cfa_linkonce" ) }
|
---|
57 | );
|
---|
58 | }
|
---|
59 |
|
---|
60 | StructDecl * ehmExceptionStructDecl( const std::string & exceptionName, std::list< TypeDecl *> * parameters ) {
|
---|
61 | StructDecl * structDecl = new StructDecl( exceptionName );
|
---|
62 | if ( parameters ) {
|
---|
63 | structDecl->parameters = *parameters;
|
---|
64 | }
|
---|
65 | return structDecl;
|
---|
66 | }
|
---|
67 |
|
---|
68 | StructDecl * ehmVirtualTableStruct( const std::string & exceptionName, std::list< TypeDecl *> * parameters ) {
|
---|
69 | // _EHM_TYPE_ID_TYPE(exception_name) parameters const * __cfavir_typeid;
|
---|
70 | ObjectDecl * typeId = new ObjectDecl(
|
---|
71 | "__cfavir_typeid",
|
---|
72 | noStorageClasses,
|
---|
73 | LinkageSpec::Cforall,
|
---|
74 | nullptr,
|
---|
75 | new PointerType( noQualifiers,
|
---|
76 | new StructInstType( Type::Const, Virtual::typeIdType( exceptionName ) ) ),
|
---|
77 | nullptr
|
---|
78 | );
|
---|
79 |
|
---|
80 | // size_t size;
|
---|
81 | ObjectDecl * size = new ObjectDecl(
|
---|
82 | "size",
|
---|
83 | noStorageClasses,
|
---|
84 | LinkageSpec::Cforall,
|
---|
85 | nullptr,
|
---|
86 | new TypeInstType( noQualifiers, "size_t", false ),
|
---|
87 | nullptr
|
---|
88 | );
|
---|
89 |
|
---|
90 | // void (*copy)(exception_name parameters * this, exception_name parameters * other);
|
---|
91 | FunctionType * copyFnType = new FunctionType( noQualifiers, false );
|
---|
92 | copyFnType->get_parameters().push_back( new ObjectDecl(
|
---|
93 | "this",
|
---|
94 | noStorageClasses,
|
---|
95 | LinkageSpec::Cforall,
|
---|
96 | nullptr,
|
---|
97 | new PointerType( noQualifiers,
|
---|
98 | new TypeInstType( noQualifiers, exceptionName, false ) ),
|
---|
99 | nullptr
|
---|
100 | ) );
|
---|
101 | copyFnType->get_parameters().push_back( new ObjectDecl(
|
---|
102 | "other",
|
---|
103 | noStorageClasses,
|
---|
104 | LinkageSpec::Cforall,
|
---|
105 | nullptr,
|
---|
106 | new PointerType( noQualifiers,
|
---|
107 | new TypeInstType( noQualifiers, exceptionName, false ) ),
|
---|
108 | nullptr
|
---|
109 | ) );
|
---|
110 | copyFnType->get_returnVals().push_back( new ObjectDecl(
|
---|
111 | "",
|
---|
112 | noStorageClasses,
|
---|
113 | LinkageSpec::Cforall,
|
---|
114 | nullptr,
|
---|
115 | new VoidType( noQualifiers ),
|
---|
116 | nullptr
|
---|
117 | ) );
|
---|
118 | ObjectDecl * copy = new ObjectDecl(
|
---|
119 | "copy",
|
---|
120 | noStorageClasses,
|
---|
121 | LinkageSpec::Cforall,
|
---|
122 | nullptr,
|
---|
123 | new PointerType( noQualifiers, copyFnType ),
|
---|
124 | nullptr
|
---|
125 | );
|
---|
126 |
|
---|
127 | // void (*^?{})(exception_name parameters & this);
|
---|
128 | FunctionType * dtorFnType = new FunctionType( noQualifiers, false );
|
---|
129 | dtorFnType->get_parameters().push_back( new ObjectDecl(
|
---|
130 | "this",
|
---|
131 | noStorageClasses,
|
---|
132 | LinkageSpec::Cforall,
|
---|
133 | nullptr,
|
---|
134 | new ReferenceType( noQualifiers,
|
---|
135 | new TypeInstType( noQualifiers, exceptionName, false ) ),
|
---|
136 | nullptr
|
---|
137 | ) );
|
---|
138 | dtorFnType->get_returnVals().push_back( new ObjectDecl(
|
---|
139 | "",
|
---|
140 | noStorageClasses,
|
---|
141 | LinkageSpec::Cforall,
|
---|
142 | nullptr,
|
---|
143 | new VoidType( noQualifiers ),
|
---|
144 | nullptr
|
---|
145 | ) );
|
---|
146 | ObjectDecl * dtor = new ObjectDecl(
|
---|
147 | "^?{}",
|
---|
148 | noStorageClasses,
|
---|
149 | LinkageSpec::Cforall,
|
---|
150 | nullptr,
|
---|
151 | new PointerType( noQualifiers, dtorFnType ),
|
---|
152 | nullptr
|
---|
153 | );
|
---|
154 |
|
---|
155 | // const char * (*msg)(exception_name parameters * this);
|
---|
156 | FunctionType * msgFnType = new FunctionType( noQualifiers, false );
|
---|
157 | msgFnType->get_parameters().push_back( new ObjectDecl(
|
---|
158 | "this",
|
---|
159 | noStorageClasses,
|
---|
160 | LinkageSpec::Cforall,
|
---|
161 | nullptr,
|
---|
162 | new PointerType( noQualifiers,
|
---|
163 | new TypeInstType( noQualifiers, exceptionName, false ) ),
|
---|
164 | nullptr
|
---|
165 | ) );
|
---|
166 | msgFnType->get_returnVals().push_back( new ObjectDecl(
|
---|
167 | "",
|
---|
168 | noStorageClasses,
|
---|
169 | LinkageSpec::Cforall,
|
---|
170 | nullptr,
|
---|
171 | new PointerType( noQualifiers, new BasicType( Type::Const, BasicType::Char ) ),
|
---|
172 | nullptr
|
---|
173 | ) );
|
---|
174 | ObjectDecl * msg = new ObjectDecl(
|
---|
175 | "msg",
|
---|
176 | noStorageClasses,
|
---|
177 | LinkageSpec::Cforall,
|
---|
178 | nullptr,
|
---|
179 | new PointerType( noQualifiers, msgFnType ),
|
---|
180 | nullptr
|
---|
181 | );
|
---|
182 |
|
---|
183 | StructDecl * structDecl = new StructDecl( Virtual::vtableTypeName( exceptionName ) );
|
---|
184 | structDecl->members.push_back( typeId );
|
---|
185 | structDecl->members.push_back( size );
|
---|
186 | structDecl->members.push_back( copy );
|
---|
187 | structDecl->members.push_back( dtor );
|
---|
188 | structDecl->members.push_back( msg );
|
---|
189 | structDecl->set_body( true );
|
---|
190 | if ( parameters ) {
|
---|
191 | structDecl->parameters = *parameters;
|
---|
192 | }
|
---|
193 | return structDecl;
|
---|
194 | }
|
---|
195 |
|
---|
196 | StructDecl * ehmExceptionStruct( const std::string & exceptionName, const std::list<Declaration*> & members,
|
---|
197 | std::list<TypeDecl *> * parameters ) {
|
---|
198 | StructDecl * structDecl = new StructDecl( exceptionName );
|
---|
199 | structDecl->members = members;
|
---|
200 | structDecl->members.push_front( new ObjectDecl(
|
---|
201 | "virtual_table",
|
---|
202 | noStorageClasses,
|
---|
203 | LinkageSpec::Cforall,
|
---|
204 | nullptr,
|
---|
205 | new PointerType( noQualifiers,
|
---|
206 | new StructInstType( Type::Const, Virtual::vtableTypeName( exceptionName ) ) ),
|
---|
207 | nullptr
|
---|
208 | ) );
|
---|
209 | structDecl->set_body( true );
|
---|
210 | if ( parameters ) {
|
---|
211 | structDecl->parameters = *parameters;
|
---|
212 | }
|
---|
213 | return structDecl;
|
---|
214 | }
|
---|
215 |
|
---|
216 | class ExceptDeclCore : public WithDeclsToAdd {
|
---|
217 | public:
|
---|
218 | Declaration * postmutate( StructDecl * structDecl );
|
---|
219 | };
|
---|
220 |
|
---|
221 | Declaration * ExceptDeclCore::postmutate( StructDecl * structDecl ) {
|
---|
222 | if ( structDecl->is_exception() ) {
|
---|
223 | const std::string & exceptionName = structDecl->name;
|
---|
224 | declsToAddBefore.push_back( ehmTypeIdStruct( exceptionName, nullptr ) );
|
---|
225 | declsToAddBefore.push_back( ehmTypeIdValue( exceptionName, nullptr ) );
|
---|
226 | declsToAddBefore.push_back( ehmExceptionStructDecl( exceptionName, nullptr ) );
|
---|
227 | declsToAddBefore.push_back( ehmVirtualTableStruct( exceptionName, nullptr ) );
|
---|
228 | return ehmExceptionStruct( exceptionName, structDecl->get_members(), nullptr );
|
---|
229 | }
|
---|
230 | return structDecl;
|
---|
231 | }
|
---|
232 |
|
---|
233 | void translateExcept( std::list< Declaration *> & translationUnit ) {
|
---|
234 | PassVisitor<ExceptDeclCore> translator;
|
---|
235 | mutateAll( translationUnit, translator );
|
---|
236 | }
|
---|
237 |
|
---|
238 | }
|
---|