-
Notifications
You must be signed in to change notification settings - Fork 131
Expand file tree
/
Copy pathwrite.cc
More file actions
373 lines (345 loc) · 13.1 KB
/
Copy pathwrite.cc
File metadata and controls
373 lines (345 loc) · 13.1 KB
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
/*****************************************************************************
* write.cc *
* *
* Description: Contains the write() functions through which the complex *
* structures can write themselves to file. More specifically, *
* the structures generate code in an output file which can be *
* used to recreate the same structures. *
* *
* Created by: David Rosenfeld *
* Date: 01/09/97 *
*****************************************************************************/
#include <cstdint>
#include <cstdlib>
#include <iomanip>
#include <map>
#include <sstream>
#include <string>
#include <vector>
#include "complexSupport.h"
#include "generated_output.h"
// Local function prototypes:
static void writeheader( ostream &, int );
struct CompactComplexNode {
JoinType kind;
uint32_t name;
uint32_t firstChild;
uint32_t nextSibling;
};
static uint32_t compactString( const std::string & value,
std::string & strings,
std::map<std::string, uint32_t> & offsets ) {
std::map<std::string, uint32_t>::const_iterator found =
offsets.find( value );
if( found != offsets.end() ) {
return found->second;
}
const uint32_t offset = static_cast<uint32_t>( strings.size() );
offsets[value] = offset;
strings.append( value );
strings.push_back( '\0' );
return offset;
}
static uint32_t appendCompactNode(
EntList * node, std::vector<CompactComplexNode> & nodes,
std::string & strings, std::map<std::string, uint32_t> & offsets ) {
const uint32_t noNode = UINT32_MAX;
const uint32_t index = static_cast<uint32_t>( nodes.size() );
CompactComplexNode record = { node->join, 0, noNode, noNode };
if( node->join == SIMPLE ) {
record.name = compactString(
static_cast<SimpleList *>( node )->Name(), strings, offsets );
}
nodes.push_back( record );
if( node->multiple() ) {
MultList * parent = static_cast<MultList *>( node );
uint32_t previous = noNode;
for( int i = 0; i < parent->childCount(); ++i ) {
uint32_t child = appendCompactNode(
parent->getChild( i ), nodes, strings, offsets );
if( previous == noNode ) {
nodes[index].firstChild = child;
} else {
nodes[previous].nextSibling = child;
}
previous = child;
}
}
return index;
}
static void writeCompactString( ostream & output, const std::string & value ) {
output << '"';
for( size_t i = 0; i < value.size(); ++i ) {
const unsigned char c = static_cast<unsigned char>( value[i] );
if( c == '\\' || c == '"' ) {
output << '\\' << static_cast<char>( c );
} else if( c >= 32 && c < 127 ) {
output << static_cast<char>( c );
} else {
output << '\\' << std::oct << std::setw( 3 ) << std::setfill( '0' )
<< static_cast<unsigned>( c ) << std::dec;
}
}
output << '"';
}
static const char * compactNodeKind( JoinType kind ) {
switch( kind ) {
case AND:
return "ComplexNodeInit_And";
case OR:
return "ComplexNodeInit_Or";
case ANDOR:
return "ComplexNodeInit_AndOr";
case SIMPLE:
default:
return "ComplexNodeInit_Simple";
}
}
static void writeCompactComplex( ostream & output, ComplexCollect & collect ) {
const uint32_t noNode = UINT32_MAX;
std::vector<CompactComplexNode> nodes;
std::vector<uint32_t> roots;
std::string strings( 1, '\0' );
std::map<std::string, uint32_t> stringOffsets;
stringOffsets[""] = 0;
for( ComplexList * list = collect.clists; list; list = list->next ) {
roots.push_back( appendCompactNode(
list->head, nodes, strings, stringOffsets ) );
}
output << "// compact, table-driven API v2 complex metadata\n"
<< "#include \"clstepcore/schemaInit.h\"\n"
<< "#include <cstddef>\n"
<< "#include <cstdint>\n\n"
<< "namespace {\n"
<< "struct GeneratedComplexImage {\n"
<< " PackedComplexImage header;\n"
<< " PackedComplexNode nodes[" << ( nodes.empty() ? 1 : nodes.size() )
<< "];\n"
<< " PackedComplexList lists[" << ( roots.empty() ? 1 : roots.size() )
<< "];\n"
<< " char strings[" << strings.size() + 1 << "];\n"
<< "};\n\n"
<< "const GeneratedComplexImage generatedComplexImage = {\n"
<< " { PackedComplexImageVersion_1, "
"sizeof(GeneratedComplexImage),\n"
<< " offsetof(GeneratedComplexImage, strings), "
<< strings.size() << ",\n"
<< " offsetof(GeneratedComplexImage, nodes), "
<< nodes.size() << ",\n"
<< " offsetof(GeneratedComplexImage, lists), "
<< roots.size() << " },\n"
<< " {\n";
if( nodes.empty() ) {
output << " {},\n";
}
for( size_t i = 0; i < nodes.size(); ++i ) {
output << " { " << compactNodeKind( nodes[i].kind ) << ", "
<< nodes[i].name << ", ";
if( nodes[i].firstChild == noNode ) {
output << "UINT32_MAX";
} else {
output << nodes[i].firstChild;
}
output << ", ";
if( nodes[i].nextSibling == noNode ) {
output << "UINT32_MAX";
} else {
output << nodes[i].nextSibling;
}
output << " },\n";
}
output << " },\n {\n";
if( roots.empty() ) {
output << " {},\n";
}
for( size_t i = 0; i < roots.size(); ++i ) {
output << " { " << roots[i] << " },\n";
}
output << " },\n ";
writeCompactString( output, strings );
output << "\n};\n}\n\nComplexCollect * gencomplex() {\n"
<< " return InitializePackedComplexSupport(\n"
<< " generatedComplexImage.header );\n"
<< "}\n";
}
static bool writeGeneratedComplex( const char * filename,
const std::ostringstream & output ) {
const std::string contents = output.str();
if( GENERATEDwrite( filename, contents.data(), contents.size() ) ) {
return true;
}
cerr << "ERROR: Could not create output file " << filename << endl;
exit( EXIT_FAILURE );
return false;
}
void print_complex( ComplexCollect & collect, const char * filename,
bool compact )
/*
* Standalone function called from exp2cxx. Takes a ComplexCollect
* and writes its contents to a file (filename) which can be used to
* recreate this CCollect.
*/
{
#ifdef COMPLEX_INFO
ComplexList * cl;
if( collect.clists ) {
// If there's something in this collect, print it out:
cout << "\nHere's everything:\n";
for( cl = collect.clists; cl != NULL; cl = cl->next ) {
cout << *cl << endl;
}
}
#endif
collect.write( filename, compact );
}
void ComplexCollect::write( const char * fname, bool compactOutput )
/*
* Generates C++ code in os which may be compiled and run to create a
* ComplexCollect structure. Functions are called to write out the
* ComplexList structures contained in this.
*/
{
std::ostringstream complex;
ComplexList * clist;
int maxlevel, listmax;
if( compactOutput ) {
writeCompactComplex( complex, *this );
writeGeneratedComplex( fname, complex );
return;
}
writeheader( complex, clists == NULL );
// If there's nothing in this, make function a stub (very little was
// printed in writeheader() also):
if( clists == NULL ) {
complex << " return 0;" << endl;
complex << "}" << endl;
writeGeneratedComplex( fname, complex );
return;
}
// First write to os the variables it will need:
complex << " ComplexCollect *cc;\n";
complex << " ComplexList *cl;\n";
complex << " EntList *node, *child;\n";
// Determine maximum EntList level among all lists so we know how large
// of an array to create.
maxlevel = 0;
clist = clists;
while( clist ) {
listmax = clist->getEntListMaxLevel();
if( listmax > maxlevel ) {
maxlevel = listmax;
}
clist = clist->next;
}
complex << " EntList *next[" << maxlevel + 1 << "];\n\n";
// Next create the CCollect and CLists:
complex << " cc = new ComplexCollect;\n";
clist = clists;
while( clist ) {
complex << endl;
complex << " // ComplexList with supertype \"" << clist->supertype()
<< "\":\n";
clist->write( complex );
complex << " cc->insert( cl );\n";
clist = clist->next;
}
// Close up:
complex << "\n return cc;\n";
complex << "}" << endl;
writeGeneratedComplex( fname, complex );
}
static void writeheader( ostream & os, int noLists )
/*
* Writes the header for the complex file.
*/
{
// If there are no ComplexLists in the ComplexCollect, make this function
// a stub:
if( noLists ) {
os << "/*" << endl
<< " * This file normally contains instantiation statements to\n"
<< " * create complex support structures. For the current EXPRESS\n"
<< " * file, however, there are no complex entities, so this\n"
<< " * function is a stub.\n"
<< " */" << endl << endl;
os << "#include \"clstepcore/complexSupport.h\"\n\n";
os << "ComplexCollect *gencomplex()" << endl;
os << "{" << endl;
return;
}
// Otherwise, write file and function comments:
os << "/*" << endl
<< " * This file contains instantiation statements to create complex\n"
<< " * support structures. The structures will be used in the SCL to\n"
<< " * validate user requests to instantiate complex entities.\n"
<< " */" << endl << endl;
os << "#include \"clstepcore/complexSupport.h\"\n\n";
os << "ComplexCollect *gencomplex()" << endl;
os << " /*" << endl
<< " * This function contains instantiation statements for all the\n"
<< " * ComplexLists and EntLists in a ComplexCollect. The instan-\n"
<< " * stiation statements were generated in order of lower to\n"
<< " * higher, and last to first to simplify creating some of the\n"
<< " * links between structures. Because of this, the code is not\n"
<< " * very readable, but does the trick.\n"
<< " */" << endl;
os << "{" << endl;
}
void ComplexList::write( ostream & os )
/*
* Generates C++ code in os which will create an instantiation of a CList
* which will recreate this.
*/
{
head->write( os );
os << " cl = new ComplexList((AndList *)node);\n";
os << " cl->buildList();\n";
os << " cl->head->setLevel( 0 );\n";
}
void MultList::write( ostream & os )
/*
* Writes to os code to instantiate a replica of this. Does so by first
* recursing to replicate this's children, and then instantiating this.
* When write() is finished, the "node" variable in os will point to this.
*/
{
EntList * child = getLast();
// First write our children, from last to first. (We go in backwards order
// so that "node" (a variable name in the os) will = our first child when
// this loop is done. See below.)
child->write( os );
while( child->prev ) {
// Whenever an EntList::write() function is called, it writes to os
// an instantiation statement basically of the form "node = new XXX-
// List;". So we know that in the output file (os) the newly-created
// EntList is pointed to by variable node.
os << " next[" << level + 1 << "] = node;\n";
child = child->prev;
child->write( os );
os << " next[" << level + 1 << "]->prev = node;\n";
os << " node->next = next[" << level + 1 << "];\n";
}
// Now write out this:
os << " child = node;\n";
// "node" was set to the last child list we just added (which is the first
// of our children). Now we set the variable "child" to it so we can reset
// node. We do this so that node will = this when we're done and return
// to the calling function (so the calling fn can make the same assumption
// we just did).
if( join == AND ) {
os << " node = new AndList;\n";
} else if( join == ANDOR ) {
os << " node = new AndOrList;\n";
} else {
os << " node = new OrList;\n";
}
os << " ((MultList *)node)->appendList( child );\n";
// The above line will set node's childList and numchidren count.
}
void SimpleList::write( ostream & os )
/*
* Writes to os a statement to instantiate this.
*/
{
os << " node = new SimpleList( \"" << name << "\" );\n";
}