Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 106d031b68c0cd1e1b5793e63903c02379b2980d (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
/*******************************************************************************
 * Copyright (c) 2006, 2008 IBM Corporation 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:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.cdt.core.dom.lrparser.action.c99;

import java.util.LinkedList;

import lpg.lpgjavaruntime.IToken;

import org.eclipse.cdt.core.dom.lrparser.IParserActionTokenProvider;
import org.eclipse.cdt.core.parser.util.DebugUtil;
/**
 * A simple set of trial and undo actions that just keep track
 * of typedef names. This information is then fed back to the parser
 * in order to disambiguate certain parser grammar rules.
 * 
 * The command design pattern is used to implement undo actions.
 * 
 * @author Mike Kucera
 */
public class C99TypedefTrackerParserAction {

	private static final boolean DEBUG = true;
	
	  
	// provides limited access to the token stream
	private final IParserActionTokenProvider parser;
	
	// The symbolTable currently in use 
	private TypedefSymbolTable symbolTable = TypedefSymbolTable.EMPTY_TABLE;
	
	// A stack that keeps track of scopes in the symbol table, used to "close" scopes and to undo the opening of scopes
	private final LinkedList<TypedefSymbolTable> symbolTableScopeStack = new LinkedList<TypedefSymbolTable>();
	
	// keeps track of nested declarations
	private final LinkedList<DeclaratorFrame> declarationStack = new LinkedList<DeclaratorFrame>();


	// "For every action there is an equal and opposite reaction." - Newton's third law
	private final LinkedList<IUndoAction> undoStack = new LinkedList<IUndoAction>();
	
	
	/**
	 * A command object that provides undo functionality.
	 */
	private interface IUndoAction {
		void undo();
	}
	
	
	/**
	 * Undoes the last fired action.
	 */
	public void undo() {
		undoStack.removeLast().undo();
	}
	
	
	public C99TypedefTrackerParserAction(IParserActionTokenProvider parser) {
		this.parser = parser;
	}
	

	/**
	 * Lexer feedback hack, used by the parser to identify typedefname tokens.
	 */
	public boolean isTypedef(String ident) {
		return symbolTable.contains(ident);
	}
	
	
	/**
	 * Methods used by tests, package local access.
	 */
	TypedefSymbolTable getSymbolTable() {
		return symbolTable;
	}
	
	int undoStackSize() {
		return undoStack.size();
	}
	
	LinkedList<DeclaratorFrame> getDeclarationStack() {
		return declarationStack;
	}
	
	
	/**
	 * Called from the grammar file in places where a scope is created.
	 * 
	 * Scopes are created by compound statements, however special care
	 * must also be taken with for loops because they may contain
	 * declarations.
	 * 
	 * TODO: scope object now need to be handled explicitly
	 */
	public void openSymbolScope() {
		if(DEBUG) DebugUtil.printMethodTrace();
		
		symbolTableScopeStack.add(symbolTable);
		
		undoStack.add(new IUndoAction() {
			public void undo() {
				if(DEBUG) DebugUtil.printMethodTrace();
				
				symbolTable = symbolTableScopeStack.removeLast();
			}
		});
	}
	

	public void closeSymbolScope() {
		if(DEBUG) DebugUtil.printMethodTrace();
		
		final TypedefSymbolTable undoTable = symbolTable;
		symbolTable = symbolTableScopeStack.removeLast(); // close the scope

		undoStack.add(new IUndoAction() {
			public void undo() {
				if(DEBUG) DebugUtil.printMethodTrace();
				
				symbolTableScopeStack.add(symbolTable);
				symbolTable = undoTable;
			}
		});
	}
	
	
	
	/**
	 * Called from the grammar before a declaration is about to be reduced.
	 */
	public void openDeclarationScope() {
		if(DEBUG) DebugUtil.printMethodTrace();
		
		declarationStack.add(new DeclaratorFrame());
		
		undoStack.add(new IUndoAction() {
			public void undo() {
				if(DEBUG) DebugUtil.printMethodTrace();
				
				declarationStack.removeLast();
			}
		});
	}
	
	
	public void closeDeclarationScope() {
		if(DEBUG) DebugUtil.printMethodTrace();
		
		final DeclaratorFrame undoFrame = declarationStack.removeLast();
		
		undoStack.add(new IUndoAction() {
			public void undo() {
				if(DEBUG) DebugUtil.printMethodTrace();
				
				declarationStack.add(undoFrame);
			}
		});
	}
	
	
	public void consumeFunctionDefinition() {
		if(DEBUG) DebugUtil.printMethodTrace();

		final DeclaratorFrame frame = declarationStack.removeLast();
		
		final TypedefSymbolTable undoTable = symbolTable;
		symbolTable = symbolTableScopeStack.removeLast();
		
		
		undoStack.add(new IUndoAction() {
			public void undo() {
				if(DEBUG) DebugUtil.printMethodTrace();
				
				symbolTableScopeStack.add(symbolTable);
				symbolTable = undoTable;
				
				declarationStack.add(frame);
			}
		});
	}




	public void consumeDeclSpecToken() {
		if(DEBUG) DebugUtil.printMethodTrace();
		
		IToken token = parser.getRightIToken();
		final int kind = token.getKind();
		
		// creates a DeclSpec if there isn't one already
		DeclaratorFrame frame = declarationStack.getLast();
		final DeclSpec declSpec = frame.getDeclSpec();
		declSpec.add(kind);
		
		undoStack.add(new IUndoAction() {
			public void undo() {
				if(DEBUG) DebugUtil.printMethodTrace();
				
				declSpec.remove(kind);
			}
		});
	}
	
	
	
	public void consumeDirectDeclaratorIdentifier() {
		if(DEBUG) DebugUtil.printMethodTrace();
		
		final DeclaratorFrame frame = declarationStack.getLast();
		frame.setDeclaratorName(parser.getRightIToken());
		
		undoStack.add(new IUndoAction() {
			public void undo() {
				if(DEBUG) DebugUtil.printMethodTrace();
				
				frame.setDeclaratorName(null);
			}
		});
	}
	
	
	public void  consumeDeclaratorComplete() {
		if(DEBUG) DebugUtil.printMethodTrace();
		
		final DeclaratorFrame frame = declarationStack.getLast();
		
		IToken token       = frame.getDeclaratorName();
		DeclSpec declSpec  = frame.getDeclSpec();

		String ident = (token == null) ? null : token.toString();
		//System.out.println("declarator complete: " + ident);
		
		final TypedefSymbolTable oldTable = symbolTable;
		if(declSpec.isTypedef()) {
			//System.out.println("adding typedef: " + ident);
			symbolTable = symbolTable.add(ident);
		}
		
		declarationStack.removeLast();
		declarationStack.add(new DeclaratorFrame(frame.getDeclSpec())); // reset the declarator
		
		undoStack.add(new IUndoAction() {
			public void undo() {
				if(DEBUG) DebugUtil.printMethodTrace();
				
				declarationStack.removeLast();
				declarationStack.add(frame);
				symbolTable = oldTable;
			}
		});
	}
	
	
	
	
	public void consumeDeclaratorCompleteParameter() {
		if(DEBUG) DebugUtil.printMethodTrace();
		
		final DeclaratorFrame frame = declarationStack.removeLast();
		
		//declarationStack.getLast().addNestedDeclaration(parameterBinding);
		
		// parameter declarations can only have one declarator, so don't reset
		//declarationStack.add(new DeclaratorFrame()); // reset

		
		undoStack.add(new IUndoAction() {
			public void undo() {
				if(DEBUG) DebugUtil.printMethodTrace();
				//declarationStack.removeLast();
				//declarationStack.getLast().removeLastNestedDeclaration();
				declarationStack.add(frame);
			}
		});
	}
	
	
	/**
	 * This is a special case for the rule:
	 *     parameter_declaration ::= declaration_specifiers
	 *     
     * In this case there is no declarator at all
     * 
     * TODO: creating bindings that have no identifier seems really dumb,
     * why does it need to be done? Why not just have a null binding or
     * for that matter don't even have a name node
     * 
	 */
	public void consumeParameterDeclarationWithoutDeclarator() {
		if(DEBUG) DebugUtil.printMethodTrace();
		
		final DeclaratorFrame frame = declarationStack.removeLast();
		
		undoStack.add(new IUndoAction() {
			public void undo() {
				if(DEBUG) DebugUtil.printMethodTrace();
				
				declarationStack.add(frame);
			}
		});
	}
	
	
	public void consumeDeclaratorCompleteField() {
		if(DEBUG) DebugUtil.printMethodTrace();
		
		final DeclaratorFrame frame = declarationStack.removeLast();

		declarationStack.add(new DeclaratorFrame(frame.getDeclSpec()));  // reset the declarator

		undoStack.add(new IUndoAction() {
			public void undo() {
				if(DEBUG) DebugUtil.printMethodTrace();
				
				declarationStack.removeLast();

				declarationStack.add(frame);
			}
		});
	}

	
	/**
	 * An abstract declarator used as part of an expression, eg) a cast.
	 * Only need the type.
	 * 
	 * TODO: this isn't enough, I need a binding for the abstract declarator
	 * what I really need is a consumeDeclaratorCompleteTypeId similar to above
	 */
	public void consumeTypeId() {
		if(DEBUG) DebugUtil.printMethodTrace();
		
		final DeclaratorFrame frame = declarationStack.removeLast();
		
		undoStack.add(new IUndoAction() {
			public void undo() {
				if(DEBUG) DebugUtil.printMethodTrace();

				declarationStack.add(frame);
			}
		});
	}
	
}

Back to the top