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
|
/*******************************************************************************
* Copyright (c) 2009 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.gnu;
import static org.eclipse.cdt.core.parser.util.CollectionUtils.findFirstAndRemove;
import java.util.List;
import lpg.lpgjavaruntime.IToken;
import org.eclipse.cdt.core.dom.ast.IASTDeclaration;
import org.eclipse.cdt.core.dom.ast.IASTExpression;
import org.eclipse.cdt.core.dom.ast.IASTInitializerList;
import org.eclipse.cdt.core.dom.ast.IASTPointer;
import org.eclipse.cdt.core.dom.ast.IASTTypeId;
import org.eclipse.cdt.core.dom.ast.IASTTypeIdInitializerExpression;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTPointerToMember;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTSimpleDeclSpecifier;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPNodeFactory;
import org.eclipse.cdt.core.dom.ast.gnu.cpp.IGPPASTExplicitTemplateInstantiation;
import org.eclipse.cdt.core.dom.ast.gnu.cpp.IGPPASTPointer;
import org.eclipse.cdt.core.dom.ast.gnu.cpp.IGPPASTPointerToMember;
import org.eclipse.cdt.core.dom.ast.gnu.cpp.IGPPASTSimpleDeclSpecifier;
import org.eclipse.cdt.core.dom.lrparser.action.ITokenMap;
import org.eclipse.cdt.core.dom.lrparser.action.ITokenStream;
import org.eclipse.cdt.core.dom.lrparser.action.ParserUtil;
import org.eclipse.cdt.core.dom.lrparser.action.ScopedStack;
import org.eclipse.cdt.core.dom.lrparser.action.TokenMap;
import org.eclipse.cdt.core.dom.lrparser.action.cpp.CPPBuildASTParserAction;
import org.eclipse.cdt.core.dom.lrparser.action.cpp.ICPPSecondaryParserFactory;
import org.eclipse.cdt.internal.core.dom.lrparser.gpp.GPPParsersym;
public class GPPBuildASTParserAction extends CPPBuildASTParserAction {
private final ICPPNodeFactory nodeFactory;
private final ITokenMap gppTokenMap;
public GPPBuildASTParserAction(ITokenStream stream, ScopedStack<Object> astStack, ICPPNodeFactory nodeFactory, ICPPSecondaryParserFactory parserFactory) {
super(stream, astStack, nodeFactory, parserFactory);
this.nodeFactory = nodeFactory;
this.gppTokenMap = new TokenMap(GPPParsersym.orderedTerminalSymbols, stream.getOrderedTerminalSymbols());
}
/**
* typeof_type_specifier
* ::= 'typeof' unary_expression
*
* typeof_declaration_specifiers
* ::= typeof_type_specifier
* | no_type_declaration_specifiers typeof_type_specifier
* | typeof_declaration_specifiers no_type_declaration_specifier
*
* declaration_specifiers
* ::= <openscope-ast> typeof_declaration_specifiers
*/
public void consumeDeclarationSpecifiersTypeof() {
List<Object> topScope = astStack.closeScope();
// There's an expression somewhere on the stack, find it
IASTExpression expr = findFirstAndRemove(topScope, IASTExpression.class);
IGPPASTSimpleDeclSpecifier declSpec = nodeFactory.newSimpleDeclSpecifierGPP();
declSpec.setTypeofExpression(expr);
// now apply the rest of the specifiers
for(Object token : topScope) {
setSpecifier(declSpec, token);
}
setOffsetAndLength(declSpec);
astStack.push(declSpec);
}
/**
* Replacement for the same method in CPPBuildASTParserAction
*/
@Override
public void consumeDeclarationSpecifiersSimple() {
boolean isComplex = false;
boolean isImaginary = false;
int numLong = 0;
List<Object> tokens = astStack.closeScope();
for(Object o : tokens) {
if(o instanceof IToken) {
IToken token = (IToken)o;
switch(token.getKind()) {
case GPPParsersym.TK__Complex: isComplex = true; break;
case GPPParsersym.TK__Imaginary: isImaginary = true; break;
case GPPParsersym.TK_long : numLong++; break;
}
}
}
ICPPASTSimpleDeclSpecifier declSpec;
if(isComplex || isImaginary || numLong > 1) {
IGPPASTSimpleDeclSpecifier gppDeclSpec = nodeFactory.newSimpleDeclSpecifierGPP();
gppDeclSpec.setComplex(isComplex);
gppDeclSpec.setImaginary(isImaginary);
gppDeclSpec.setLongLong(numLong > 1);
gppDeclSpec.setLong(numLong == 1);
declSpec = gppDeclSpec;
}
else {
declSpec = nodeFactory.newSimpleDeclSpecifier();
}
for(Object token : tokens) {
setSpecifier(declSpec, token);
}
setOffsetAndLength(declSpec);
astStack.push(declSpec);
}
private boolean hasRestrict(List<Object> tokens) {
for(Object o : tokens) {
IToken t = (IToken)o;
if(gppTokenMap.mapKind(t.getKind()) == GPPParsersym.TK_restrict) {
return true;
}
}
return false;
}
/**
* Restrict is allowed as a keyword.
*/
@Override
public void consumePointer() {
boolean hasRestrict = hasRestrict(astStack.topScope());
super.consumePointer();
if(hasRestrict) {
IGPPASTPointer gppPointer = nodeFactory.newPointerGPP();
initializeGPPPointer((IASTPointer)astStack.pop(), gppPointer);
astStack.push(gppPointer);
}
}
private static void initializeGPPPointer(IASTPointer pointer, IGPPASTPointer gppPointer) {
gppPointer.setConst(pointer.isConst());
gppPointer.setVolatile(pointer.isVolatile());
gppPointer.setRestrict(true);
ParserUtil.setOffsetAndLength(gppPointer, pointer);
}
@Override
public void consumePointerToMember() {
boolean hasRestrict = hasRestrict(astStack.topScope());
super.consumePointerToMember();
if(hasRestrict) {
ICPPASTPointerToMember pointer = (ICPPASTPointerToMember) astStack.pop();
IGPPASTPointerToMember gppPointer = nodeFactory.newPointerToMemberGPP(pointer.getName());
initializeGPPPointer(pointer, gppPointer);
astStack.push(gppPointer);
}
}
public void consumeTemplateExplicitInstantiationGCC(int modifier) {
IASTDeclaration declaration = (IASTDeclaration) astStack.pop();
IGPPASTExplicitTemplateInstantiation instantiation = nodeFactory.newExplicitTemplateInstantiationGPP(declaration);
instantiation.setModifier(modifier);
setOffsetAndLength(instantiation);
astStack.push(instantiation);
}
/**
* postfix_expression ::= '(' type_id ')' initializer_list
*/
public void consumeExpressionTypeIdInitializer() {
IASTInitializerList list = (IASTInitializerList) astStack.pop();
IASTTypeId typeId = (IASTTypeId) astStack.pop();
IASTTypeIdInitializerExpression expr = nodeFactory.newTypeIdInitializerExpression(typeId, list);
setOffsetAndLength(expr);
astStack.push(expr);
}
}
|