File: EncodingUtil.cc

package info (click to toggle)
openc%2B%2B 2.8-4
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 5,464 kB
  • ctags: 3,333
  • sloc: cpp: 19,071; sh: 12,793; ansic: 3,549; makefile: 577
file content (172 lines) | stat: -rw-r--r-- 3,839 bytes parent folder | download | duplicates (2)
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
//@beginlicenses@
//@license{chiba-tokyo}{}@
//@license{Grzegorz Jakacki}{2004}@
//
//  Copyright (C) 1997-2001 Shigeru Chiba, Tokyo Institute of Technology.
//
//  Permission to use, copy, distribute and modify this software and
//  its documentation for any purpose is hereby granted without fee,
//  provided that the above copyright notice appears in all copies and that
//  both that copyright notice and this permission notice appear in
//  supporting documentation.
//
//  Shigeru Chiba makes no representations about the suitability of this
//  software for any purpose.  It is provided "as is" without express or
//  implied warranty.
//
//  -----------------------------------------------------------------
//
//  Permission to use, copy, distribute and modify this software and its  
//  documentation for any purpose is hereby granted without fee, provided that
//  the above copyright notice appears in all copies and that both that copyright
//  notice and this permission notice appear in supporting documentation.
// 
//  Grzegorz Jakacki make(s) no representations about the suitability of this
//  software for any purpose. It is provided "as is" without express or implied
//  warranty.
//  
//  Copyright (C) 2004 Grzegorz Jakacki
//
//@endlicenses@

#include <opencxx/EncodingUtil.h>
#include <opencxx/Class.h>
#include <opencxx/Bind.h>
#include <opencxx/Environment.h>
#include <opencxx/TypeInfo.h>

namespace Opencxx
{

namespace EncodingUtil {

unsigned char* 
GetTemplateArguments(unsigned char* name, int& len)
{
    int m = name[0] - 0x80;
    if(m <= 0){
	len = name[1] - 0x80;
	return &name[2];
    }
    else{
	len = name[m + 1] - 0x80;
	return &name[m + 2];
    }
}

int 
GetBaseNameIfTemplate(unsigned char* name, Environment*& e)
{
    int m = name[0] - 0x80;
    if(m <= 0)
	return name[1] - 0x80 + 2;

    Bind* b;
    if(e != 0 && e->LookupType((char*)&name[1], m, b))
	if(b != 0 && b->What() == Bind::isTemplateClass){
	    Class* c = b->ClassMetaobject();
	    if(c != 0){
		e = c->GetEnvironment();
		return m + (name[m + 1] - 0x80) + 2;
	    }
	}

    // the template name was not found.
    e = 0;
    return m + (name[m + 1] - 0x80) + 2;
}


// GetBaseName() returns "Foo" if ENCODE is "Q[2][1]X[3]Foo", for example.
// If an error occurs, the function returns 0.

char* 
GetBaseName(char* encode, int& len, Environment*& env)
{
    if(encode == 0){
	len = 0;
	return 0;
    }

    Environment* e = env;
    unsigned char* p = (unsigned char*)encode;
    if(*p == 'Q'){
	int n = p[1] - 0x80;
	p += 2;
	while(n-- > 1){
	    int m = *p++;
	    if(m == 'T')
		m = GetBaseNameIfTemplate(p, e);
	    else if(m < 0x80){		// error?
		len = 0;
		return 0;
	    }
	    else{			// class name
		m -= 0x80;
		if(m <= 0){		// if global scope (e.g. ::Foo)
		    if(e != 0)
			e = e->GetBottom();
		}
		else
		    e = ResolveTypedefName(e, (char*)p, m);
	    }

	    p += m;
	}

	env = e;
    }

    if(*p == 'T'){		// template class
	int m = p[1] - 0x80;
	int n = p[m + 2] - 0x80;
	len = m + n + 3;
	return (char*)p;
    }
    else if(*p < 0x80){		// error?
	len = 0;
	return 0;
    }
    else{
	len = *p - 0x80;
	return (char*)p + 1;
    }
}

Environment* 
ResolveTypedefName(Environment* env, char* name, int len)
{
    TypeInfo tinfo;
    Bind* bind;
    Class* c = 0;

    if(env != 0)
	if (env->LookupType(name, len, bind) && bind != 0)
	    switch(bind->What()){
	    case Bind::isClassName :
		c = bind->ClassMetaobject();
		break;
	    case Bind::isTypedefName :
		bind->GetType(tinfo, env);
		c = tinfo.ClassMetaobject();
		/* if (c == 0) */
		    env = 0;
		break;
	    default :
		break;
	    }
	else {
	    env = env->LookupNamespace(name, len);
	    /* env is 0 if name is an unknown typedef name or namespace.
	     */
	}

    if(c != 0)
	return c->GetEnvironment();
    else
	return env;
}

}

}