summaryrefslogtreecommitdiffstats
path: root/mcopidl/namespace.cc
blob: 1e863a0dd0e66b45f24a79d6729c510fc79b4a71 (plain)
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
    /*

    Copyright (C) 2000 Stefan Westerfeld
                       stefan@space.twc.de

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.
  
    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Library General Public License for more details.
   
    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to
    the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
    Boston, MA 02111-1307, USA.

    */

#include <string.h>
#include <stdio.h>
#include <list>
#include <string>
#include <map>
#include "namespace.h"
#include <assert.h>

using namespace std;

/* generic utilities */

static list<string> symbolToList(string symbol)
{
	list<string> result;
	string current;

	string::iterator si;
	for(si = symbol.begin(); si != symbol.end(); si++)
	{
		if(*si != ':')
		{
			current += *si;
		}
		else
		{
			if(!current.empty())
				result.push_back(current);

			current = "";
		}
	}

	result.push_back(current);
	return result;
}

static string listToSymbol(list<string>& symlist)
{
	string s;
	list<string>::iterator si;
	for(si = symlist.begin(); si != symlist.end(); si++)
	{
		if(!s.empty()) s += "::";
		s += *si;
	}
	return s;
}

/* ModuleHelper */
static list<string> modulePath;
static map<string,bool> moduleDefinitions;

void ModuleHelper::enter(const char *name)
{
	modulePath.push_back(name);
}

void ModuleHelper::leave()
{
	assert(!modulePath.empty());
	modulePath.pop_back();
}

string prependModulePath(string s)
{
	if(modulePath.empty())
		return s;
	else
		return listToSymbol(modulePath)+"::"+s;
}

void ModuleHelper::define(const char *name)
{
	moduleDefinitions[prependModulePath(name)] = true;
}

char *ModuleHelper::qualify(const char *name)
{
	char *result = 0;

	// TODO: nested namespaces

	string inCurrentModule = prependModulePath(name);
	if(moduleDefinitions[inCurrentModule])
	{
		result = strdup(inCurrentModule.c_str());
	}
	else if(moduleDefinitions[name])
	{
		result = strdup(name);
	}
	else
	{
		fprintf(stderr,"warning: qualifyName failed for %s\n",name);
		result = strdup(name);
	}

	return result;
}


/* NamespaceHelper */
NamespaceHelper::NamespaceHelper(FILE *outputfile) : out(outputfile)
{
}

NamespaceHelper::~NamespaceHelper()
{
	leaveAll();
}

void NamespaceHelper::setFromSymbol(string symbol)
{
	list<string> symlist = symbolToList(symbol);
	symlist.pop_back();

	/* check that the current namespace doesn't contain wrong parts at end */
	list<string>::iterator ni,si;
	ni = currentNamespace.begin();
	si = symlist.begin();
	long wrong = currentNamespace.size();
	while(ni != currentNamespace.end() && si != symlist.end() && *ni == *si)
	{
		ni++;
		si++;
		wrong--;
	}
	while(wrong--)
	{
		fprintf(out,"}\n");
	}

	/* enter new components at the end */
	while(si != symlist.end())
	{
		fprintf(out,"namespace %s {\n",(*si++).c_str());
	}
	currentNamespace = symlist;
}

void NamespaceHelper::leaveAll()
{
	setFromSymbol("unqualified");
}

string NamespaceHelper::printableForm(string symbol)
{
	list<string> symlist = symbolToList(symbol);
	list<string> current = currentNamespace;

	while(!current.empty())
	{
		// namespace longer than symbol?
		assert(!symlist.empty());

		if(*current.begin() == *symlist.begin())
		{
			current.pop_front();
			symlist.pop_front();
		}
		else
		{
			return "::"+symbol;
		}
	}

	return listToSymbol(symlist);
}

string NamespaceHelper::nameOf(string symbol)
{
	if(symbol.empty()) return "";

	list<string> symlist = symbolToList(symbol);
	return symlist.back();
}

string NamespaceHelper::namespaceOf(string symbol)
{
	list<string> symlist = symbolToList(symbol);
	if(symlist.size() < 2) return "";

	symlist.pop_back();
	return listToSymbol(symlist);
}