File: Symbol.cpp

package info (click to toggle)
fauhdlc 20180504-3.1
  • links: PTS
  • area: main
  • in suites: bookworm, bullseye, forky, 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 (213 lines) | stat: -rw-r--r-- 4,553 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
/* $Id$
 *
 * Symbol: A symbol refers to a named entity.
 *
 * 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.
 */

#include "frontend/misc/Symbol.hpp"
#include "frontend/misc/DeclarativeRegion.hpp"
#include "frontend/ast/FunctionDeclaration.hpp"
#include "frontend/visitor/ResolveTypes.hpp"

namespace ast {

/*
 * same name and:
 * 	<= 1 is overloadable -> true
 * 	2 is overloadable and 
 * 		same parameter/result type profile -> true
 * 	falltrhough: false
 */
bool
Symbol::isHomograph(const Symbol& other) const {
	if (*(this->name) != *(other.name)) {
		return false;
	}

	unsigned int overloadable = SYMBOL_FUNCTION | SYMBOL_PROCEDURE;

	if ((this->type | other.type | overloadable) != overloadable) {
		//at most one is overloadable
		//i.o.w: at least on is not overloadable
		return true;
	}

	//both are overloadable
	//are both procedures?
	if ((this->type | other.type) == SYMBOL_PROCEDURE) {
		return this->sameParameterProfile(other);
	}

	//is only one a procedure?
	if (((this->type | other.type) & SYMBOL_PROCEDURE) != 0) {
		// cannot match on result type profile -> not homographic
		return false;
	}

	//are both functions?
	if ((this->type | other.type) == SYMBOL_FUNCTION) {
		bool ret = this->sameResultProfile(other);
		ret = ret && this->sameParameterProfile(other);
		return ret;
	}

	// not overloadable: -> homographs.
	return false;
}

bool
Symbol::sameParameterProfile(const Symbol& other) const
{
	Callable* c1 = dynamic_cast<Callable*>(&this->declaration);
	Callable* c2 = dynamic_cast<Callable*>(&other.declaration);
	assert(c1);
	assert(c2);

	if (c1->arguments == NULL) {
		return c2->arguments == NULL;
	}

	if (c2->arguments == NULL) {
		return false;
	}

	if (c1->arguments->size() != c2->arguments->size()) {
		return false;
	}
	
	std::list<ValDeclaration*>::const_iterator i = c1->arguments->begin();
	std::list<ValDeclaration*>::const_iterator j = c2->arguments->begin();

	// check base type of subtype of both
	while ((i != c1->arguments->end()) && (j != c2->arguments->end())) {
		assert((*i)->subtypeIndic);
		assert((*j)->subtypeIndic);

		if (! this->baseTypeEqual(
			*(*i)->subtypeIndic,
			*(*j)->subtypeIndic)) {

			return false;
		}
		
		i++; j++;
	}
	// same number of parameters?
	return true;
}

bool
Symbol::sameResultProfile(const Symbol& other) const
{
	FunctionDeclaration *f1 = 
		dynamic_cast<FunctionDeclaration*>(&this->declaration);
	FunctionDeclaration *f2 = 
		dynamic_cast<FunctionDeclaration*>(&other.declaration);

	assert(f1);
	assert(f2);

	// there must have been a compile error, otherwise the
	// returnType s must have been set.
	if (f1->returnType == NULL) {
		return false;
	}

	if (f2->returnType == NULL) {
		return false;
	}

	return this->baseTypeEqual(*(f1->returnType), *(f2->returnType));
}

bool
Symbol::baseTypeEqual(
	const SubtypeIndication& s1,
	const SubtypeIndication& s2
) 
{
	// must have thrown an error during resolving.
	if (s1.declaration == NULL) {
		return false;
	}
	// must have thrown an error during resolving.
	if (s2.declaration == NULL) {
		return false;
	}

	return ResolveTypes::baseTypeEqual(*s1.declaration,
					   *s2.declaration);
}

void 
Symbol::put(std::ostream& stream) const
{
	stream << *name << ": ";
	switch(this->type) {
	case SYMBOL_ENTITY:
		stream << "Entity";
		break;
	case SYMBOL_PROCEDURE:
		stream << "Procedure";
		break;
	case SYMBOL_FUNCTION:
		stream << "Function";
		break;
	case SYMBOL_SIGNAL:
		stream << "Signal";
		break;
	case SYMBOL_VARIABLE:
		stream << "Variable/Constant";
		break;
	case SYMBOL_TYPE:
		stream << "Type";
		break;
	case SYMBOL_UNIT:
		stream << "Physical Unit";
		break;
	case SYMBOL_ELEMENT:
		stream << "Record element";
		break;
	case SYMBOL_ATTRIBUTE:
		stream << "Attribute";
		break;
	case SYMBOL_ARCHITECTURE:
		stream << "Architecture";
		break;
	case SYMBOL_PROCESS:
		stream << "Process";
		break;
	case SYMBOL_LOOP:
		stream << "Loop";
		break;
	case SYMBOL_LIBRARY:
		stream << "Library";
		break;
	case SYMBOL_PACKAGE:
		stream << "Package";
		break;
	case SYMBOL_ALL:
		stream << "ALL token";
		break;
	case SYMBOL_PARAMETER:
		stream << "Parameter";
		break;
	case SYMBOL_PORT:
		stream << "Port";
		break;
	}
}


std::ostream& operator<<(std::ostream& stream, const Symbol& sym)
{
	sym.put(stream);
	return stream;
}

}; /* namespace ast */