Skip to main content
summaryrefslogtreecommitdiffstats
blob: 7dc492a1e8da2e75b1e20c650f088d689747a31d (plain) (blame)
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
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
/*******************************************************************************
 * Copyright (c) 2000, 2006 QNX Software Systems and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     QNX Software Systems - Initial API and implementation
 *     Matthias Spycher (matthias@coware.com) - bug 124966
 *******************************************************************************/

package org.eclipse.cdt.debug.mi.core;

import java.util.regex.Pattern;

/**
 * GDB Type Parser.
 * The code was lifted from: The C Programming Language
 * B. W. Kernighan and D. Ritchie
 */
public class GDBTypeParser {

	// GDB type parsing from whatis command
	// declarator: type dcl
	// type: (name)+
	// dcl: ('*' | '&')* direct-decl
	// direct-dcl: '(' dcl ')'
	//             direct-dcl '(' ')'
	//             direct-dcl '[' integer ']'
	// name: ([a-zA-z][0-9])+
	// integer ([0-9)+ 

	final static int EOF = -1;
	final static int NAME = 0;
	final static int PARENS = 1;
	final static int BRACKETS = 2;

	String line;
	int index;
	int tokenType;
	String token;
	String dataType;
	String name;
	GDBDerivedType gdbDerivedType;
	GDBType genericType;

	public GDBType getGDBType() {
		if (gdbDerivedType != null) {
			return gdbDerivedType;
		}
		return genericType;
	}

	public String getVariableName() {
		return name;
	}

	public GDBType parse(String s) {
		// Sanity.
		if (s == null) {
			s = new String();
		}
		s = Pattern.compile("\\bconst\\b").matcher(s).replaceAll("");  //$NON-NLS-1$//$NON-NLS-2$
		s = Pattern.compile("\\bvolatile\\b").matcher(s).replaceAll("");  //$NON-NLS-1$//$NON-NLS-2$
		s = s.trim();

		// Initialize.
		line = s;
		index = 0;
		tokenType = -1;
		token = ""; //$NON-NLS-1$
		dataType = ""; //$NON-NLS-1$
		name = ""; //$NON-NLS-1$
		gdbDerivedType = null;
		genericType = null;

		// Fetch the datatype.
		while (getToken() == NAME) {
			dataType += " " + token; //$NON-NLS-1$
		}

		// Hack for GDB, the typename can be something like
		// class A : public B, C { ... } *
		// We are only interested in "class A"
		// Carefull for class A::data or class ns::A<ns::data>
		int column = dataType.indexOf(':');
        while (column > 0) {
            if ((column + 2) < dataType.length() && dataType.charAt(column + 1) == ':') {
                column = dataType.indexOf(':', column+2);
                continue;
            }
            dataType = dataType.substring(0, column);
            break;
        }
		genericType = new GDBType(dataType);

		// Start the recursive parser.
		dcl(tokenType);
		return getGDBType();
	}

	public static String unParse (GDBType gdbType) {

		StringBuffer sb = new StringBuffer();
		// Fetch the datatype.
		while (gdbType != null) {
			GDBDerivedType derived = null;
			int type = gdbType.getType();
			if (gdbType instanceof GDBDerivedType) {
				derived = (GDBDerivedType)gdbType;
				gdbType = derived.getChild();
				// respect the precedence of operators.
				if (type == GDBType.FUNCTION) {
					sb.append("()"); //$NON-NLS-1$
				} else if (type == GDBType.ARRAY) {
					sb.append('[').append(derived.getDimension()).append(']');
				} else if (type == GDBType.POINTER) {
					int childType = (gdbType != null) ? gdbType.getType() : GDBType.GENERIC; 
					if (childType == GDBType.POINTER || childType == GDBType.REFERENCE) {
						sb.append('*');
					} else if (childType == GDBType.GENERIC) {
						sb.insert(0, '*');
					} else {
						sb.insert(0, "(*").append(')'); //$NON-NLS-1$
					}
				} else if (type == GDBType.REFERENCE) {
					int childType = (gdbType != null) ? gdbType.getType() : GDBType.GENERIC; 
					if (childType == GDBType.POINTER || childType == GDBType.REFERENCE) {
						sb.append("&"); //$NON-NLS-1$
					} else if (childType == GDBType.GENERIC) {
						sb.insert(0, '&');
					} else {
						sb.insert(0, "(&").append(')'); //$NON-NLS-1$
					}
				}
			} else {
				sb.insert(0, ' ');
				sb.insert(0, gdbType.nameType);
				gdbType = null;
			}
		}
		return sb.toString().trim();

	}

	public class GDBType {
		public final static int GENERIC = 0;
		public final static int POINTER = 1;
		public final static int REFERENCE = 2;
		public final static int ARRAY = 3;
		public final static int FUNCTION = 4;

		String nameType;
		int type;

		public GDBType(String n) {
			this(n, 0);
		}

		public GDBType(int t) {
			this("", t); //$NON-NLS-1$
		}

		public GDBType(String n, int t) {
			nameType = n;
			type = t;
		}

		public String toString() {
			return unParse(this);
		}

		public String verbose() {
			return nameType;
		}

		public int getType() {
			return type;
		}

		public String getTypeName() {
			return nameType;
		}
	}

	public class GDBDerivedType extends GDBType {
		int dimension;
		GDBType child;

		public GDBDerivedType(GDBType c, int i) {
			this(c, i, 0);
		}

		public GDBDerivedType(GDBType c, int t, int dim) {
			super(t);
			setChild(c);
			dimension = dim;
		}

		public int getDimension() {
			return dimension;
		}

		public void setChild(GDBType c) {
			child = c;
		}

		public GDBType getChild() {
			return child;
		}

		public boolean hasChild() {
			return child != null;
		}

		public String verbose() {
			StringBuffer sb = new StringBuffer();
			switch (getType()) {
				case FUNCTION :
					sb.append(" function returning " + (hasChild() ? child.verbose() : ""));  //$NON-NLS-1$//$NON-NLS-2$
					break;
				case ARRAY :
					sb.append(" array[" + dimension + "]" + " of " + (hasChild() ? child.verbose() : ""));  //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
					break;
				case REFERENCE :
					sb.append(" reference to " + (hasChild() ? child.verbose() : ""));  //$NON-NLS-1$//$NON-NLS-2$
					break;
				case POINTER :
					sb.append(" pointer to " + (hasChild() ? child.verbose() : ""));  //$NON-NLS-1$//$NON-NLS-2$
					break;
			}
			return sb.toString();
		}
	}

	int getch() {
		if (index >= line.length() || index < 0) {
			return EOF;
		}
		return line.charAt(index++);
	}

	void ungetch() {
		if (index > 0) {
			index--;
		}
	}

	// check if the character is an alphabet
	boolean isCIdentifierStart(int c) {
		if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || c == '_' || c == ':' || c == ',') {
			return true;
		}
		return false;
	}

	// check is the character is alpha numeric
	// [a-zA-Z0-9]
	// GDB hack accept ':' ',' part of the GDB hacks
	// when doing ptype gdb returns "class A : public C { ..}"
	boolean isCIdentifierPart(int c) {
		if ((c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || c == '_' || c == ':') {
			return true;
		}
		return false;
	}

	boolean isCSpace(int c) {
		if (c == ' ' || c == '\t' || c == '\f' || c == '\n') {
			return true;
		}
		return false;
	}

	void insertingChild(int kind) {
		insertingChild(kind, 0);
	}

	void insertingChild(int kind, int d) {
		if (gdbDerivedType == null) {
			gdbDerivedType = new GDBDerivedType(genericType, kind, d);
		} else {
			GDBDerivedType dType = gdbDerivedType;
			GDBType gdbType = gdbDerivedType.getChild();
			while (gdbType instanceof GDBDerivedType) {
				dType = (GDBDerivedType)gdbType;
				gdbType = dType.getChild();
			}				
			gdbType = new GDBDerivedType(gdbType, kind, d);
			dType.setChild(gdbType);
		}
	}

	// method returns the next token
	int getToken() {
		token = ""; //$NON-NLS-1$

		int c = getch();

		// Skip over any space
		while (isCSpace(c)) {
			c = getch();
		}

		//char character = (char) c;

		if (c == '(') {
			c = getch();
			if (c == ')') {
				token = "()"; //$NON-NLS-1$
				tokenType = PARENS;
			} else if (isCIdentifierStart(c)) {
				int i = 0;
				token += (char)c;
				while (i == 0 && c != ')') {
					if (c == EOF) {
						// Unbalanced parantheses.
						break;
					}
					c = getch();
					token += (char)c;
					if (c == '(') {
						++i;
					} else if (c == ')') {
						--i;
					}
				}
				tokenType = PARENS;
			} else {
				ungetch();
				tokenType = '(';
			}
			
			

		} else if (c == '[') {
			while ((c = getch()) != ']' && c != EOF) {
				token += (char) c;
			}
			tokenType = BRACKETS;
		} else if (isCIdentifierStart(c)) {
            StringBuffer sb = new StringBuffer();
            sb.append((char) c);
			while (isCIdentifierPart((c = getch())) && c != EOF) {
                sb.append((char) c);
			}
            if (c == '<') {
                // Swallow template args in types like "class foobar<A,B> : public C {..} *"
                // FIXME: if the bracket is not terminate do we throw exception?
                sb.append((char) c);
                int count = 1;
                do {
                    c = getch();
                    if (c == '<') {
                        count++;
                    } else if (c == '>') {
                        count--;
                    }
                    if (c != ' ') {
                    	sb.append((char)c);
                    }
                } while (count > 0 && c != EOF);
            } else if (c != EOF) {
				ungetch();
			}
            token = sb.toString();
			tokenType = NAME;
		} else if (c == '{') {
			// Swallow gdb sends things like "struct foobar {..} *"
			// FIXME: if the bracket is not terminate do we throw exception?
			int count = 1;
			do {
				c = getch();
				if (c == '{') {
					count++;
				} else if (c == '}') {
					count--;
				}
			} while (count > 0 && c != EOF);
		} else {
			tokenType = c;
		}
		return tokenType;
	}

	void dcl() {
		dcl(getToken());
	}

	// parse a declarator
	void dcl(int c) {
		int nstar = 0;
		int namp = 0;
		if (c == '*') {
			nstar++;
			for (; getToken() == '*'; nstar++) {
			}
		} else if (c == '&') {
			namp++;
			for (; getToken() == '&'; namp++) {
			}
		}
		dirdcl();
		while (nstar-- > 0) {
			insertingChild(GDBType.POINTER);
		}
		while (namp-- > 0) {
			insertingChild(GDBType.REFERENCE);
		}
	}

	// parse a direct declarator
	void dirdcl() {
		int type;

		if (tokenType == '(') {
			dcl();
			if (tokenType != ')' /*&& name.length() > 0*/) {
				// Do we throw an exception on unterminated parentheses
				// It should have been handle by getToken()
				return;
			}
		} else if (tokenType == NAME) {
			// Useless we do not need the name of the variable
			name = " " + token; //$NON-NLS-1$
		} else if (tokenType == PARENS) {
			insertingChild(GDBType.FUNCTION);
		} else if (tokenType == BRACKETS) {			
			int len = 0;
			if (token.length() > 0) {
				try {
					len = Integer.parseInt(token);
				} catch (NumberFormatException e) {
				}
			}
			insertingChild(GDBType.ARRAY, len);
		} else if (tokenType == '&') {
			insertingChild(GDBType.REFERENCE);
		} else {
			// oops bad declaration ?
			return;
		}

		while ((type = getToken()) == PARENS || type == BRACKETS) {
			if (type == PARENS) {
				insertingChild(GDBType.FUNCTION);
			} else { /* BRACKETS */
				int len = 0;
				if (token.length() > 0) {
					try {
						len = Integer.parseInt(token);
					} catch (NumberFormatException e) {
					}
				}
				insertingChild(GDBType.ARRAY, len);
			}
		}
	}

	public static void main(String[] args) {

		GDBTypeParser parser = new GDBTypeParser();

		System.out.println("int *&"); //$NON-NLS-1$
		parser.parse("int *&"); //$NON-NLS-1$
		System.out.println(GDBTypeParser.unParse(parser.getGDBType()));
		System.out.println(parser.getGDBType().verbose());
		System.out.println();

		System.out.println("int (&rg)(int)"); //$NON-NLS-1$
		parser.parse("int (&rg)(int)"); //$NON-NLS-1$
		System.out.println(GDBTypeParser.unParse(parser.getGDBType()));
		System.out.println(parser.getGDBType().verbose());
		System.out.println();

		System.out.println("int (&ra)[3]"); //$NON-NLS-1$
		parser.parse("int (&rg)[3]"); //$NON-NLS-1$
		System.out.println(GDBTypeParser.unParse(parser.getGDBType()));
		System.out.println(parser.getGDBType().verbose());
		System.out.println();

		System.out.println("struct link { int i; int j; struct link * next;} *"); //$NON-NLS-1$
		parser.parse("struct link { int i; int j; struct link * next} *"); //$NON-NLS-1$
		System.out.println(GDBTypeParser.unParse(parser.getGDBType()));
		System.out.println(parser.getGDBType().verbose());
		System.out.println();

        System.out.println("class ns::link<8, ns::A> : public ns::B { int i; int j; struct link * next;} *"); //$NON-NLS-1$
        parser.parse("class ns::link<8, ns::A> : public ns::B { int i; int j; struct link * next;} *"); //$NON-NLS-1$
        System.out.println(GDBTypeParser.unParse(parser.getGDBType()));
        System.out.println(parser.getGDBType().verbose());
        System.out.println();

        System.out.println("char **argv"); //$NON-NLS-1$
		parser.parse("char **argv"); //$NON-NLS-1$
		System.out.println(GDBTypeParser.unParse(parser.getGDBType()));
		System.out.println(parser.getGDBType().verbose());
		System.out.println();

		System.out.println("int (*daytab)[13]"); //$NON-NLS-1$
		parser.parse("int (*daytab)[13]"); //$NON-NLS-1$
		System.out.println(GDBTypeParser.unParse(parser.getGDBType()));
		System.out.println(parser.getGDBType().verbose());
		System.out.println();

		System.out.println("int *daytab[13]"); //$NON-NLS-1$
		parser.parse("int *daytab[13]"); //$NON-NLS-1$
		System.out.println(GDBTypeParser.unParse(parser.getGDBType()));
		System.out.println(parser.getGDBType().verbose());
		System.out.println();

		System.out.println("void *comp()"); //$NON-NLS-1$
		parser.parse("void *comp()"); //$NON-NLS-1$
		System.out.println(GDBTypeParser.unParse(parser.getGDBType()));
		System.out.println(parser.getGDBType().verbose());
		System.out.println();

		System.out.println("void (*comp)()"); //$NON-NLS-1$
		parser.parse("void (*comp)()"); //$NON-NLS-1$
		System.out.println(GDBTypeParser.unParse(parser.getGDBType()));
		System.out.println(parser.getGDBType().verbose());
		System.out.println();

		System.out.println("int (*func[15])()"); //$NON-NLS-1$
		parser.parse("int (*func[15])()"); //$NON-NLS-1$
		System.out.println(GDBTypeParser.unParse(parser.getGDBType()));
		System.out.println(parser.getGDBType().verbose());
		System.out.println();

		System.out.println("char (*(*x())[])()"); //$NON-NLS-1$
		parser.parse("char (*(*x())[])()"); //$NON-NLS-1$
		System.out.println(GDBTypeParser.unParse(parser.getGDBType()));
		System.out.println(parser.getGDBType().verbose());
		System.out.println();

		System.out.println("char (*(*x[3])())[5]"); //$NON-NLS-1$
		parser.parse("char (*(*x[3])())[5]"); //$NON-NLS-1$
		System.out.println(GDBTypeParser.unParse(parser.getGDBType()));
		System.out.println(parser.getGDBType().verbose());
		System.out.println();

		System.out.println("char *[5]"); //$NON-NLS-1$
		parser.parse("char *[5]"); //$NON-NLS-1$
		System.out.println(GDBTypeParser.unParse(parser.getGDBType()));
		System.out.println(parser.getGDBType().verbose());
		System.out.println();

		System.out.println("int [2][3]"); //$NON-NLS-1$
		parser.parse("int [2][3]"); //$NON-NLS-1$
		System.out.println(GDBTypeParser.unParse(parser.getGDBType()));
		System.out.println(parser.getGDBType().verbose());
		System.out.println();

		System.out.println("int (int, char **)"); //$NON-NLS-1$
		parser.parse("int (int, char **)"); //$NON-NLS-1$
		System.out.println(GDBTypeParser.unParse(parser.getGDBType()));
		System.out.println(parser.getGDBType().verbose());
		System.out.println();

		System.out.println("int (int)"); //$NON-NLS-1$
		parser.parse("int (int)"); //$NON-NLS-1$
		System.out.println(GDBTypeParser.unParse(parser.getGDBType()));
		System.out.println(parser.getGDBType().verbose());
		System.out.println();

		System.out.println("int (void)"); //$NON-NLS-1$
		parser.parse("int (void)"); //$NON-NLS-1$
		System.out.println(GDBTypeParser.unParse(parser.getGDBType()));
		System.out.println(parser.getGDBType().verbose());
		System.out.println();

		System.out.println("int ()"); //$NON-NLS-1$
		parser.parse("int ()"); //$NON-NLS-1$
		System.out.println(GDBTypeParser.unParse(parser.getGDBType()));
		System.out.println(parser.getGDBType().verbose());
		System.out.println();

	}
}

Back to the top