File: DotVisitor.hpp

package info (click to toggle)
fauhdlc 20180504-3.1
  • links: PTS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 3,064 kB
  • sloc: cpp: 23,188; ansic: 6,077; yacc: 3,764; lex: 763; makefile: 605; python: 412; xml: 403; sh: 61
file content (540 lines) | stat: -rw-r--r-- 14,920 bytes parent folder | download | duplicates (3)
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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
/* $Id$ 
 * DotVisitor: create a .dot graph representation of the abstract syntax
 * tree.
 *
 * Copyright (C) 2007-2009 FAUmachine Team <info@faumachine.org>.
 * This program is free software. You can redistribute it and/or modify it
 * under the terms of the GNU General Public License, either version 2 of
 * the License, or (at your option) any later version. See COPYING.
 */

#ifndef __DOT_VISITOR_HPP_INCLUDED
#define __DOT_VISITOR_HPP_INCLUDED

#include <list>
#include "frontend/visitor/TopDownVisitor.hpp"

namespace ast {

//! convert AST to a dot graph.
/** This visitor will convert the abstract syntax tree to a dot graph. 
 *  The output can then be converted to e.g. a postscript file with
 *    dot -Tps output.dot > output.ps
 *  This visitor is safe to be used during all frontend stages.
 */
class DotVisitor : public TopDownVisitor {
public:
	//! c'tor
	DotVisitor();

	//! d'tor
	~DotVisitor();
private:
	/** visit a ElementAssociation
         *  @param node node that get's visited.
         */
	virtual void visit(ElementAssociation& node);

	/** visit a ConstInteger
         *  @param node node that get's visited.
         */
	virtual void visit(ConstInteger& node);

	/** visit a ConstReal
         *  @param node node that get's visited.
         */
	virtual void visit(ConstReal& node);

	/** visit a ConstArray
         *  @param node node that get's visited.
         */
	virtual void visit(ConstArray &node);

	/** visit a SimpleName
         *  @param node node that get's visited.
         */
	virtual void visit(SimpleName& node);

	/** visit a SelectedName
         *  @param node node that get's visited.
         */
	virtual void visit(SelectedName& node);

	/** visit a AttributeName
         *  @param node node that get's visited.
         */
	virtual void visit(AttributeName& node);

	/** visit a TemporaryName
         *  @param node node that get's visited.
         */
	virtual void visit(TemporaryName& node);

	/** Visit an Entity declaration.
	 *  @param node Entity Declaration node that get's visited.
	 */
	virtual void visit(Entity& node);

	/** Visit an Signal declaration.
	 *  @param node SignalDeclaration node that get's visited.
	 */
	virtual void visit(SignalDeclaration& node);

	/** Visit an Constant declaration.
	 *  @param node ConstantDeclaration node that get's visited.
	 */
	virtual void visit(ConstantDeclaration& node);

	/** Visit an FunctionCall.
	 *  @param node FunctionCall node that get's visited.
	 */
	virtual void visit(FunctionCall& node);

	/** Visit an IfStat.
	 *  @param node IfStat node that get's visited.
	 */
	virtual void visit(IfStat& node);

	/** Visit an NullStat.
	 *  @param node IfStat node that get's visited.
	 */
	virtual void visit(NullStat& node);

	/** Visit a ForLoopStat
	 *  @param node ForLoopStat node that get's visited.
	 */
	virtual void visit(ForLoopStat& node);

	/** Visit a WhileLoopStat
	 *  @param node WhileLoopStat node that get's visited.
	 */
	virtual void visit(WhileLoopStat& node);

	/** Visit a NextStat
	 *  @param node NextStat node that get's visited.
	 */
	virtual void visit(NextStat& node);

	/** Visit a VarAssignStat
	 *  @param node VarAssignStat node that get's visited.
	 */
	virtual void visit(VarAssignStat& node);

	/** Visit a WaitStat
	 *  @param node WaitStat node that get's visited.
	 */
	virtual void visit(WaitStat& node);

	/** Visit a ExitStat
	 *  @param node ExitStat node that get's visited.
	 */
	virtual void visit(ExitStat& node);

	/** Visit a SigAssignStat
	 *  @param node SigAssignStat node that get's visited.
	 */
	virtual void visit(SigAssignStat& node);

	/** Visit a WaveFormElem
	 *  @param node WaveFormElem node that get's visited.
	 */
	virtual void visit(WaveFormElem& node);

	/** Visit a ReturnStat
	 *  @param node ReturnStat node that get's visited.
	 */
	virtual void visit(ReturnStat& node);

	/** Visit a ProcCallStat
	 *  @param node ReturnStat node that get's visited.
	 */
	virtual void visit(ProcCallStat& node);

	/** Visit a AssertStat
	 *  @param node AssertStat node that get's visited.
	 */
	virtual void visit(AssertStat& node);

	/** Visit a VarDeclaration
	 *  @param node VarDeclaration node that get's visited.
	 */
	virtual void visit(VarDeclaration& node);

	/** Visit a DiscreteRange
	 *  @param node DiscreteRange node that get's visited.
	 */
	virtual void visit(DiscreteRange& node);

	/** Visit a CaseStat
	 *  @param node CaseStat node that get's visited.
	 */
	virtual void visit(CaseStat& node);

	/** Visit a CaseAlternative
	 *  @param node CaseAlternative node that get's visited.
	 */
	virtual void visit(CaseAlternative& node);

	/** Visit a Others node.
	 *  @param node Others node that get's visited.
	 */
	virtual void visit(Others& node);

	/** Visit a Architecture node.
	 *  @param node Architecture node that get's visited.
	 */
	virtual void visit(Architecture& node);

	/** Visit a AssociationElement node.
	 *  @param node AssociationElement node that get's visited.
	 */
	virtual void visit(AssociationElement& node);

	/** Visit a FunctionDeclaration node.
	 *  @param node FunctionDeclaration node that get's visited.
	 */
	virtual void visit(FunctionDeclaration& node);

	/** Visit a ProcedureDeclaration node.
	 *  @param node ProcedureDeclaration node that get's visited.
	 */
	virtual void visit(ProcedureDeclaration& node);

	/** Visit a CompInstStat node.
	 *  @param node CompInstStat node that get's visited.
	 */
	virtual void visit(CompInstStat& node);

	/** Visit a Package node.
	 *  @param node Package node that get's visited.
	 */
	virtual void visit(Package& node);

	/** Visit a PackageBody node.
	 *  @param node PackageBody node that get's visited.
	 */
	virtual void visit(PackageBody& node);

	/** Visit a Process node.
	 *  @param node Process node that get's visited.
	 */
	virtual void visit(Process& node);

	/** Visit a SubprogBody node.
	 *  @param node SubprogBody node that get's visited.
	 */
	virtual void visit(SubprogBody& node);

	/** Visit a CondalSigAssign node.
	 *  @param node CondalSigAssign node that get's visited.
	 */
	virtual void visit(CondalSigAssign& node);

	/** Visit an EnumerationType node.
	 *  @param node EnumerationType node that get's visited.
	 */
	virtual void visit(EnumerationType& node);

	/** Visit an PhysicalType node.
	 *  @param node PhysicalType node that get's visited.
	 */
	virtual void visit(PhysicalType& node);

	/** Visit an PhysicalTypeUnit node.
	 *  @param node PhysicalTypeUnit node that get's visited.
	 */
	virtual void visit(PhysicalTypeUnit& node);

	/** Visit an RangeConstraintType node.
	 *  @param node RangeConstraintType node that get's visited.
	 */
	virtual void visit(RangeConstraintType& node);

	/** Visit an UnconstrainedArrayType node.
	 *  @param node UnconstrainedArrayType node that get's visited.
	 */
	virtual void visit(UnconstrainedArrayType& node);

	/** Visit an RecordType node.
	 *  @param node RecordType node that get's visited.
	 */
	virtual void visit(RecordType& node);

	/** Visit an RecordTypeElement node.
	 *  @param node RecordTypeElement node that get's visited.
	 */
	virtual void visit(RecordTypeElement &node);

	/** Visit an Aggregate node.
	 *  @param node Aggregate node that get's visited.
	 */
	virtual void visit(Aggregate &node);

	/** Visit a SubtypeIndication node.
	 *  @param node SubtypeIndication node that get's visited.
	 */
	virtual void visit(SubtypeIndication &node);

	/** Visit a Library node.
	 *  @param node Library node that get's visited.
	 */
	virtual void visit(Library &node);

	/** Visit a LibraryList node.
	 *  @param node LibraryList node that get's visited.
	 */
	virtual void visit(LibraryList &node);

	/** Visit a Subscript node.
	 *  @param node Subscript node that get's visited.
	 */
	virtual void visit(Subscript &node);

	/** Visit a Slice node.
	 *  @param node Slice node that get's visited.
	 */
	virtual void visit(Slice &node);

	/** Visit a TypeConversion node.
	 *  @param node TypeConversion node that get's visited.
	 */
	virtual void visit(TypeConversion &node);

	/** Visit an AttributeDeclaration node.
	 *  @param node AttributeDeclaration node that gets visited.
	 */
	virtual void visit(AttributeDeclaration &node);

	/** Visit an AttributeSpecification node.
	 *  @param node AttributeSpecification node that gets visited.
	 */
	virtual void visit(AttributeSpecification &node);

public:
	//! write the dot-graph to stream.
	/** @param stream to which the dot graph should get written to.
          */
	void put(std::ostream &stream) const;

private:
	using TopDownVisitor::process;

	//! Process a generic ValDeclaration.
        /** This function will get called for each ValDeclaration (or class 
         *  derived from ValDeclaration) that get's visited.
         *
         *  @param node ValDeclaration instance.
         */
	virtual void process(ValDeclaration& node);

	//! Process a generic SymbolDeclaration.
        /** This function will get called for each SymbolDeclaration (or class
         *  derived from SymbolDeclaration) that get's visited.
         *
         *  @param node SymbolDeclaration instance.
         */
	virtual void process(SymbolDeclaration& node);

	//! Process a generic Expression.
        /** This function will get called for each Expression (or class
         *  derived from Expression) that get's visited.
         *
         *  @param node Expression instance.
         */
	virtual void process(Expression& node);

	//! Process a generic SeqStat.
        /** This function will get called for each SeqStat (or class
         *  derived from SeqStat) that get's visited.
         *
         *  @param node SeqStat instance.
         */
	virtual void process(SeqStat& node);

	//! Process a generic LoopStat.
        /** This function will get called for each LoopStat (or class
         *  derived from LoopStat) that get's visited.
         *
         *  @param node LoopStat instance.
         */
	virtual void process(LoopStat& node);

	//! Process a generic ConditionedStat.
        /** This function will get called for each ConditionedStat (or class
         *  derived from ConditionedStat) that get's visited.
         *
         *  @param node ConditionedStat instance.
         */
	virtual void process(ConditionedStat& node);

	//! Process a generic Callable.
        /** This function will get called for each Callable (or class
         *  derived from Callable) that get's visited.
         *
         *  @param node Callable instance.
         */
	virtual void process(Callable& node);

	//! Process a generic LibUnit.
        /** This function will get called for each LibUnit (or class
         *  derived from LibUnit) that get's visited.
         *
         *  @param node LibUnit instance.
         */
	virtual void process(LibUnit& node);

	//! Process a generic TypeDeclaration.
        /** This function will get called for each TypeDeclaration (or class
         *  derived from TypeDeclaration) that get's visited.
         *
         *  @param node TypeDeclaration instance.
         */
	virtual void process(TypeDeclaration& node);

	//! Process a generic Name.
        /** This function will get called for each Name (or class
         *  derived from Name) that get's visited.
         *
         *  @param node Name instance.
         */
	virtual void process(Name &node);

	//! Process a generic PrefixedName.
        /** This function will get called for each PrefixedName (or class
         *  derived from PrefixedName) that get's visited.
         *
         *  @param node PrefixedName instance.
         */
	virtual void process(PrefixedName &node);

	/** one edge in the graph */
	class Edge {
	public:
		/** @param source source number of node.
		 *  @param destination destination number of node.
		 */
		Edge(
			int source, 
			int destination
			) : 	from(source), 
				to(destination),
				label(NULL) {}

		/** @param source source number of node.
		 *  @param destination destination number of node.
		 *  @param lbl label of the edge.
		 */
		Edge(
			int source, 
			int destination,
			const char* lbl
			) : 	from(source), 
				to(destination),
				label(new std::string(lbl)) {}

		//! copy c'tor
		/** need to copy the string as well.
		 *  @param other other Edge instance */
		Edge(const Edge& other) : from(other.from), to(other.to) {
			if (other.label) {
				this->label = new std::string(*other.label);
			} else {
				this->label = NULL;
			}
		}

		~Edge();

		/** put the edge to stream.
		 *  @param stream to which the edge should be written. */
		void put(std::ostream& stream) const;

	private:
		/** origin of the edge */
		int from;	
		/** destination of the edge */
		int to;
		/** optional label of the edge */
		std::string* label;
	};

	/** one description of a node. */
	class NodeDescription {
	public:
		/** @param num unique number of the Description
		 *  @param n name of the node.
		 */
		NodeDescription(int num, std::string n) : id(num), name(n) {}

		/** add a name/value pair to the description.
		 *  @param n name of the name/value-pair.
		 *  @param value value of the name/value-pair.
		 */
		void add(std::string n, std::string value);

		/** add a simple string to the description.
                  * @param s string to add.
                  */
		void add(std::string s);

		/** put the node to a stream.
		 *  @param stream stream on which the node should get written.
		 */
		void put(std::ostream& stream) const;

		/** unique number of the node. */
		int id;
	private:
		/** name of the node. */
		std::string name;
		/** list of attributes */
		std::list<std::string> attributes;
	};
	
	/** generate edges for list of children.
          * @param fromId id of parent node.
          * @param l list of pointers to child nodes.
          */
	template <typename T>
	void makeEdges(int fromId, T l);

	/** generate edges for list of children.
          * @param fromId id of parent node.
          * @param l list of pointers to child nodes.
	  * @param s label of the edge.
          */
	template <typename T>
	void makeEdges(int fromId, T l, const char* s);

	/** create a new node description, assign it an id and set it to be 
	 *  the current description.
	 *  @param node currently visited AstNode.
	 *  @param desc descriptive string.
	 *  @return newly created NodeDescription.
	 */
	NodeDescription* makeDescription(AstNode& node, const char* desc);


	/** assign a new Id to an AstNode.
          * @param node node to which the number should get assigned to.
          * @return newly created id.
          */
	int assignId(AstNode& node);

	/** edges of the graph */
	std::list<Edge> edges;
	/** node of the graph */
	std::list<NodeDescription*> nodes;

	/** last unique id that was handed out */
	int idCounter;

	/** current NodeDescription */
	NodeDescription* currentDesc;
};
	
}; /* namespace ast */

#include "DotVisitor.tpp"

#endif /* __DOT_VISITOR_HPP_INCLUDED */