-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCGOutput.cpp
295 lines (250 loc) · 8.82 KB
/
CGOutput.cpp
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
/****************************************************************************
* Modul: $RCSfile: CGOutput.cpp,v $
*
* $Author: md $
* $Date: 1998/01/14 10:59:58 $
* $Revision: 1.3 $
*
* Aufgabe: Dieses Modul enthaelt die Generierung des C++-Codes fuer
* entsprechende SDL-Aktionen
****************************************************************************/
#ifdef USE_PRAGMA
#pragma interface
#endif
/****************************************************************************
* Konstanten
****************************************************************************/
/****************************************************************************
* Include-Anweisungen
****************************************************************************/
#include <DS/DSString.h>
#include <DS/DSBlock.h>
#include <DS/DSProcess.h>
#include <DS/DSSignal.h>
#include <DS/DSExpression.h>
#include "CGFile.h"
#include "CGOutput.h"
#ifdef DEBUG
#include <dmalloc.h>
#endif
/****************************************************************************
* Externe Variablen
****************************************************************************/
/****************************************************************************
* Globale Variablen
****************************************************************************/
/****************************************************************************
* Konstruktoren
****************************************************************************/
CGOutput::CGOutput(DSObject* father, DSString* name,
DSSignalRef sig, DSActualParamList* param) :
DSOutput(father, name, sig, param)
{
}
CGOutput::CGOutput(DSObject* father, DSString& name,
DSSignalRef sig, DSActualParamList* param) :
DSOutput(father, name, sig, param)
{
}
CGOutput::CGOutput(DSObject* father, const char* name,
DSSignalRef sig, DSActualParamList* param) :
DSOutput(father, name, sig, param)
{
}
/****************************************************************************
* Destruktor
****************************************************************************/
CGOutput::~CGOutput(void)
{
}
/****************************************************************************
* Virtueller Konstruktor
****************************************************************************/
DSObject *CGOutput::New(DSObject *father) const
{
return (DSOutput *)new CGOutput(father);
}
/****************************************************************************
* Write(): Oberfunktion der Codegenerierung fuer Output
* -> writer
* -> what
* Ergebnis: DS_OK oder DS_ERROR
* Seiteneffekte: Anlegen und Fuellen der .h und .cc-Files
****************************************************************************/
DSResult CGOutput::Write(DSWriter *writer, DSCardinal what) const
{
DSResult result;
(void)what;
result = WriteImplementation((CGWriter *)writer);
if (result != DS_OK) return result;
return DS_OK;
}
/****************************************************************************
* WriteImplementation(): schreibt einen Output
* -> writer
* Ergebnis: DS_OK,falls Aktion erfolgreich war, sonst DS_ERROR
* Seiteneffekte: Zieldatei wird gefuellt
****************************************************************************/
DSResult CGOutput::WriteImplementation(CGWriter *writer) const
{
DSResult result;
CGFile *out;
CGPos pos;
DSSignalRef signal;
DSProcessRef process_ref;
DSProcessRefList *destination_process_list;
DSActualParam *actual_param;
DSCardinal i = 1;
DSBlockRef block;
DSSortRef sort;
DSString str;
out = writer->CGGetFilePointer();
assert(out);
pos = writer->CGGetIndentPos();
result = WriteLabel(writer);
if (result != DS_OK) return result;
assert(GetSignalRef());
signal = GetSignalRef();
assert(signal);
// Signalparameter vorhanden?
if (GetActualParamList())
{
InsertString(*out, PREFIX_SIGNAL_PARAM_VARIABLE, pos, CG_NO_EOL);
InsertString(*out, signal->GetName(), 0, CG_NO_EOL);
InsertString(*out, " = new ", 0, CG_NO_EOL);
InsertString(*out, PREFIX_SIGNAL_PARAM_TYPE, 0, CG_NO_EOL);
InsertString(*out, signal->GetName(), 0, CG_NO_EOL);
InsertString(*out, ";", 0, CG_WITH_EOL);
InsertString(*out, "assert(", pos, CG_NO_EOL);
InsertString(*out, PREFIX_SIGNAL_PARAM_VARIABLE, 0, CG_NO_EOL);
InsertString(*out, signal->GetName(), 0, CG_NO_EOL);
InsertString(*out, ");", 0, CG_WITH_EOL);
for (actual_param = GetFirstActualParam(),
sort = signal->GetFirstSortRef();
actual_param;
actual_param = GetNextActualParam(),
sort = signal->GetNextSortRef())
{
InsertString(*out, PREFIX_SIGNAL_PARAM_VARIABLE, pos, CG_NO_EOL);
InsertString(*out, signal->GetName(), 0,
CG_NO_EOL);
InsertString(*out, "->", 0, CG_NO_EOL);
str.Format("%s%d", PREFIX_SIGNAL_FIELD, i);
InsertString(*out, str, 0, CG_NO_EOL);
InsertString(*out, " = ", 0, CG_NO_EOL);
writer->CGSetIndentPos(0);
if ((result = actual_param->Write(writer) ) != DS_OK)
{
return result;
}
InsertString(*out, ";", 0, CG_WITH_EOL);
writer->CGSetIndentPos(pos);
i++;
}
}
// Empfaenger ueber TO spezifiziert?
if (GetProcessID())
{
InsertString(*out, CG_NAME_SCL_OUTPUT, pos, CG_NO_EOL);
InsertString(*out, "(", 0, CG_NO_EOL);
writer->CGSetIndentPos(0);
if ((result = GetProcessID()->Write(writer) ) != DS_OK)
{
return result;
}
writer->CGSetIndentPos(pos);
}
else // Empfaenger NICHT ueber TO spezifiziert!
{
// Signalziel(e) aus statischer Systemstruktur bestimmen:
destination_process_list = GetSignalDestination();
if (!destination_process_list || destination_process_list->IsEmpty())
{
writer->cgCout << "Can't determine destination for signal '"
<< (GetSignalRef())->GetName() << "'" << std::endl;
if (destination_process_list) delete destination_process_list;
return DS_OK;
}
if (destination_process_list->NumberOfElements() > 1)
{
writer->cgCout << "Warning: More than one destination for signal '"
<< (GetSignalRef())->GetName() << "', using first." << std::endl;
}
InsertString(*out, CG_NAME_SCL_OUTPUT, pos, CG_NO_EOL);
InsertString(*out, "(", 0, CG_NO_EOL);
assert(destination_process_list->MoveFirst() == DS_OK);
process_ref = destination_process_list->GetCurrentData();
assert(process_ref);
block = process_ref->GetThisBlock();
assert(block);
writer->CGSetIndentPos(0);
block->Write(writer, CG_WRITE_IDENTIFIER);
writer->CGSetIndentPos(pos);
str.Format("::%s%s",
PREFIX_PROCESS_TYPE_VARIABLE,
process_ref->GetName()->GetString());
InsertString(*out, str, 0, CG_NO_EOL);
delete destination_process_list;
}
str.Format(", %s%s", PREFIX_SIGNAL_TYPE_VARIABLE,
signal->GetName()->GetString());
InsertString(*out, str, 0, CG_NO_EOL);
if (GetActualParamList())
{
str.Format(", %s%s",
PREFIX_SIGNAL_PARAM_VARIABLE,
signal->GetName()->GetString());
InsertString(*out, str, 0, CG_NO_EOL);
}
else if (GetDelay())
{
InsertString(*out, ", NULL", 0, CG_NO_EOL);
}
if (GetDelay())
{
InsertString(*out, ", ", 0, CG_NO_EOL);
writer->CGSetIndentPos(0);
result = GetDelay()->Write(writer);
writer->CGSetIndentPos(pos);
if (result != DS_OK)
{
return result;
}
}
InsertString(*out, ");", 0, CG_WITH_EOL);
return DS_OK;
}
/****************************************************************************
* WriteLabel(): Schreibt das optionale Label der SDL-Aktion
* -> writer
* Ergebnis: DS_OK, falls Aktion erfolgreich war, sonst
* enthaelt DSResult das Ergebnis
* Seiteneffekte: Zieldatei wird gefuellt
****************************************************************************/
DSResult CGOutput::WriteLabel(CGWriter *writer) const
{
DSResult result;
DSStringKeyList *label_list;
DSString *new_label;
CGFile *out;
CGPos pos;
if (GetLabel() == NULL) return DS_OK;
out = writer->CGGetFilePointer();
assert(out);
pos = writer->CGGetIndentPos();
label_list = writer->CGGetLabelList();
if (label_list == NULL || // kann innerhalb einer Starttransition passieren
!label_list->IsKeyInList(GetLabel()))
{
InsertString(*out, PREFIX_LABEL, pos, CG_NO_EOL);
InsertString(*out, GetLabel(), 0, CG_NO_EOL);
InsertString(*out, ":", 0, CG_WITH_EOL);
if (label_list != NULL)
{
new_label = new DSString(*GetLabel());
assert(new_label);
assert(label_list->Append(new DSString(*new_label), new_label) == DS_OK);
}
}
return DS_OK;
}