1
/*******************************************************************************
2
* Copyright (c) 2009 IBM Corporation and others.
3
* All rights reserved. This program and the accompanying materials
4
* are made available under the terms of the Eclipse Public License v1.0
5
* which accompanies this distribution, and is available at
6
* http://www.eclipse.org/legal/epl-v10.html
9
* IBM Corporation - initial API and implementation
10
*******************************************************************************/
11
package org.eclipse.cdt.core.dom.lrparser.action.gnu;
13
import static org.eclipse.cdt.core.parser.util.CollectionUtils.findFirstAndRemove;
15
import java.util.List;
17
import lpg.lpgjavaruntime.IToken;
19
import org.eclipse.cdt.core.dom.ast.IASTDeclaration;
20
import org.eclipse.cdt.core.dom.ast.IASTExpression;
21
import org.eclipse.cdt.core.dom.ast.IASTInitializerList;
22
import org.eclipse.cdt.core.dom.ast.IASTPointer;
23
import org.eclipse.cdt.core.dom.ast.IASTTypeId;
24
import org.eclipse.cdt.core.dom.ast.IASTTypeIdInitializerExpression;
25
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTPointerToMember;
26
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTSimpleDeclSpecifier;
27
import org.eclipse.cdt.core.dom.ast.cpp.ICPPNodeFactory;
28
import org.eclipse.cdt.core.dom.ast.gnu.cpp.IGPPASTExplicitTemplateInstantiation;
29
import org.eclipse.cdt.core.dom.ast.gnu.cpp.IGPPASTPointer;
30
import org.eclipse.cdt.core.dom.ast.gnu.cpp.IGPPASTPointerToMember;
31
import org.eclipse.cdt.core.dom.ast.gnu.cpp.IGPPASTSimpleDeclSpecifier;
32
import org.eclipse.cdt.core.dom.lrparser.action.ITokenMap;
33
import org.eclipse.cdt.core.dom.lrparser.action.ITokenStream;
34
import org.eclipse.cdt.core.dom.lrparser.action.ParserUtil;
35
import org.eclipse.cdt.core.dom.lrparser.action.ScopedStack;
36
import org.eclipse.cdt.core.dom.lrparser.action.TokenMap;
37
import org.eclipse.cdt.core.dom.lrparser.action.cpp.CPPBuildASTParserAction;
38
import org.eclipse.cdt.core.dom.lrparser.action.cpp.ICPPSecondaryParserFactory;
39
import org.eclipse.cdt.internal.core.dom.lrparser.gpp.GPPParsersym;
41
public class GPPBuildASTParserAction extends CPPBuildASTParserAction {
43
private final ICPPNodeFactory nodeFactory;
45
private final ITokenMap gppTokenMap;
47
public GPPBuildASTParserAction(ITokenStream stream, ScopedStack<Object> astStack, ICPPNodeFactory nodeFactory, ICPPSecondaryParserFactory parserFactory) {
48
super(stream, astStack, nodeFactory, parserFactory);
49
this.nodeFactory = nodeFactory;
50
this.gppTokenMap = new TokenMap(GPPParsersym.orderedTerminalSymbols, stream.getOrderedTerminalSymbols());
55
* typeof_type_specifier
56
* ::= 'typeof' unary_expression
58
* typeof_declaration_specifiers
59
* ::= typeof_type_specifier
60
* | no_type_declaration_specifiers typeof_type_specifier
61
* | typeof_declaration_specifiers no_type_declaration_specifier
63
* declaration_specifiers
64
* ::= <openscope-ast> typeof_declaration_specifiers
66
public void consumeDeclarationSpecifiersTypeof() {
67
List<Object> topScope = astStack.closeScope();
69
// There's an expression somewhere on the stack, find it
70
IASTExpression expr = findFirstAndRemove(topScope, IASTExpression.class);
71
IGPPASTSimpleDeclSpecifier declSpec = nodeFactory.newSimpleDeclSpecifierGPP();
72
declSpec.setTypeofExpression(expr);
74
// now apply the rest of the specifiers
75
for(Object token : topScope) {
76
setSpecifier(declSpec, token);
79
setOffsetAndLength(declSpec);
80
astStack.push(declSpec);
85
* Replacement for the same method in CPPBuildASTParserAction
88
public void consumeDeclarationSpecifiersSimple() {
89
boolean isComplex = false;
90
boolean isImaginary = false;
93
List<Object> tokens = astStack.closeScope();
95
for(Object o : tokens) {
96
if(o instanceof IToken) {
97
IToken token = (IToken)o;
98
switch(gppTokenMap.mapKind(token.getKind())) {
99
case GPPParsersym.TK__Complex: isComplex = true; break;
100
case GPPParsersym.TK__Imaginary: isImaginary = true; break;
101
case GPPParsersym.TK_long : numLong++; break;
106
ICPPASTSimpleDeclSpecifier declSpec;
107
if(isComplex || isImaginary || numLong > 1) {
108
IGPPASTSimpleDeclSpecifier gppDeclSpec = nodeFactory.newSimpleDeclSpecifierGPP();
109
gppDeclSpec.setComplex(isComplex);
110
gppDeclSpec.setImaginary(isImaginary);
111
gppDeclSpec.setLongLong(numLong > 1);
112
gppDeclSpec.setLong(numLong == 1);
113
declSpec = gppDeclSpec;
116
declSpec = nodeFactory.newSimpleDeclSpecifier();
119
for(Object token : tokens) {
120
setSpecifier(declSpec, token);
123
setOffsetAndLength(declSpec);
124
astStack.push(declSpec);
129
private boolean hasRestrict(List<Object> tokens) {
130
for(Object o : tokens) {
131
IToken t = (IToken)o;
132
if(gppTokenMap.mapKind(t.getKind()) == GPPParsersym.TK_restrict) {
141
* Restrict is allowed as a keyword.
144
public void consumePointer() {
145
boolean hasRestrict = hasRestrict(astStack.topScope());
146
super.consumePointer();
149
IGPPASTPointer gppPointer = nodeFactory.newPointerGPP();
150
initializeGPPPointer((IASTPointer)astStack.pop(), gppPointer);
151
astStack.push(gppPointer);
156
private static void initializeGPPPointer(IASTPointer pointer, IGPPASTPointer gppPointer) {
157
gppPointer.setConst(pointer.isConst());
158
gppPointer.setVolatile(pointer.isVolatile());
159
gppPointer.setRestrict(true);
160
ParserUtil.setOffsetAndLength(gppPointer, pointer);
166
public void consumePointerToMember() {
167
boolean hasRestrict = hasRestrict(astStack.topScope());
168
super.consumePointerToMember();
171
ICPPASTPointerToMember pointer = (ICPPASTPointerToMember) astStack.pop();
172
IGPPASTPointerToMember gppPointer = nodeFactory.newPointerToMemberGPP(pointer.getName());
173
initializeGPPPointer(pointer, gppPointer);
174
astStack.push(gppPointer);
180
public void consumeTemplateExplicitInstantiationGCC(int modifier) {
181
IASTDeclaration declaration = (IASTDeclaration) astStack.pop();
182
IGPPASTExplicitTemplateInstantiation instantiation = nodeFactory.newExplicitTemplateInstantiationGPP(declaration);
183
instantiation.setModifier(modifier);
184
setOffsetAndLength(instantiation);
185
astStack.push(instantiation);
190
* postfix_expression ::= '(' type_id ')' initializer_list
192
public void consumeExpressionTypeIdInitializer() {
193
IASTInitializerList list = (IASTInitializerList) astStack.pop();
194
IASTTypeId typeId = (IASTTypeId) astStack.pop();
195
IASTTypeIdInitializerExpression expr = nodeFactory.newTypeIdInitializerExpression(typeId, list);
196
setOffsetAndLength(expr);