|
Lines 1-5
Link Here
|
| 1 |
/******************************************************************************* |
1 |
/******************************************************************************* |
| 2 |
* Copyright (c) 2005, 2006 IBM Corporation and others. |
2 |
* Copyright (c) 2005 IBM Corporation and others. |
| 3 |
* All rights reserved. This program and the accompanying materials |
3 |
* All rights reserved. This program and the accompanying materials |
| 4 |
* are made available under the terms of the Eclipse Public License v1.0 |
4 |
* are made available under the terms of the Eclipse Public License v1.0 |
| 5 |
* which accompanies this distribution, and is available at |
5 |
* which accompanies this distribution, and is available at |
|
Lines 10-15
Link Here
|
| 10 |
*******************************************************************************/ |
10 |
*******************************************************************************/ |
| 11 |
package org.eclipse.cdt.internal.core.dom.parser; |
11 |
package org.eclipse.cdt.internal.core.dom.parser; |
| 12 |
|
12 |
|
|
|
13 |
import java.util.HashMap; |
| 14 |
import java.util.LinkedList; |
| 15 |
|
| 13 |
import org.eclipse.cdt.core.dom.ast.ASTCompletionNode; |
16 |
import org.eclipse.cdt.core.dom.ast.ASTCompletionNode; |
| 14 |
import org.eclipse.cdt.core.dom.ast.ASTVisitor; |
17 |
import org.eclipse.cdt.core.dom.ast.ASTVisitor; |
| 15 |
import org.eclipse.cdt.core.dom.ast.IASTASMDeclaration; |
18 |
import org.eclipse.cdt.core.dom.ast.IASTASMDeclaration; |
|
Lines 55-60
Link Here
|
| 55 |
import org.eclipse.cdt.core.dom.ast.IASTUnaryExpression; |
58 |
import org.eclipse.cdt.core.dom.ast.IASTUnaryExpression; |
| 56 |
import org.eclipse.cdt.core.dom.ast.IASTWhileStatement; |
59 |
import org.eclipse.cdt.core.dom.ast.IASTWhileStatement; |
| 57 |
import org.eclipse.cdt.core.dom.ast.IASTEnumerationSpecifier.IASTEnumerator; |
60 |
import org.eclipse.cdt.core.dom.ast.IASTEnumerationSpecifier.IASTEnumerator; |
|
|
61 |
import org.eclipse.cdt.core.dom.ast.c.ICASTCompositeTypeSpecifier; |
| 62 |
import org.eclipse.cdt.core.dom.ast.c.ICASTElaboratedTypeSpecifier; |
| 63 |
import org.eclipse.cdt.core.dom.ast.c.ICASTEnumerationSpecifier; |
| 64 |
import org.eclipse.cdt.core.dom.ast.c.ICASTSimpleDeclSpecifier; |
| 65 |
import org.eclipse.cdt.core.dom.ast.c.ICASTTypedefNameSpecifier; |
| 58 |
import org.eclipse.cdt.core.dom.ast.gnu.IGNUASTCompoundStatementExpression; |
66 |
import org.eclipse.cdt.core.dom.ast.gnu.IGNUASTCompoundStatementExpression; |
| 59 |
import org.eclipse.cdt.core.dom.ast.gnu.IGNUASTTypeIdExpression; |
67 |
import org.eclipse.cdt.core.dom.ast.gnu.IGNUASTTypeIdExpression; |
| 60 |
import org.eclipse.cdt.core.dom.ast.gnu.IGNUASTUnaryExpression; |
68 |
import org.eclipse.cdt.core.dom.ast.gnu.IGNUASTUnaryExpression; |
|
Lines 66-71
Link Here
|
| 66 |
import org.eclipse.cdt.core.parser.OffsetLimitReachedException; |
74 |
import org.eclipse.cdt.core.parser.OffsetLimitReachedException; |
| 67 |
import org.eclipse.cdt.core.parser.ParseError; |
75 |
import org.eclipse.cdt.core.parser.ParseError; |
| 68 |
import org.eclipse.cdt.core.parser.ParserMode; |
76 |
import org.eclipse.cdt.core.parser.ParserMode; |
|
|
77 |
import org.eclipse.cdt.internal.core.dom.parser.c.CASTTypeId; |
| 69 |
|
78 |
|
| 70 |
/** |
79 |
/** |
| 71 |
* @author jcamelon |
80 |
* @author jcamelon |
|
Lines 105-110
Link Here
|
| 105 |
this.supportKnRC = supportKnRC; |
114 |
this.supportKnRC = supportKnRC; |
| 106 |
this.supportGCCOtherBuiltinSymbols = supportGCCOtherBuiltinSymbols; |
115 |
this.supportGCCOtherBuiltinSymbols = supportGCCOtherBuiltinSymbols; |
| 107 |
this.supportAttributeSpecifiers = supportAttributeSpecifiers; |
116 |
this.supportAttributeSpecifiers = supportAttributeSpecifiers; |
|
|
117 |
this.typeScopeStack = new TypeScopeStack(); |
| 108 |
} |
118 |
} |
| 109 |
|
119 |
|
| 110 |
protected boolean parsePassed = true; |
120 |
protected boolean parsePassed = true; |
|
Lines 141-146
Link Here
|
| 141 |
} |
151 |
} |
| 142 |
|
152 |
|
| 143 |
/** |
153 |
/** |
|
|
154 |
* A scope class for holding type names as the source parsed and AST is constructed. |
| 155 |
* In the process of the AST it is not possible to use the CScope lookup names since |
| 156 |
* often the AST is not yet correctly connected. As a consequence it is difficult |
| 157 |
* to determine when an IASTName represents a type or variable. This is an important |
| 158 |
* property to be able to check in the case of cast expressions (see: castExpression()). |
| 159 |
* |
| 160 |
* Due to CScope not being available this TypeScope is used to store known types as |
| 161 |
* we go along so that it is possible to check typedness of an IASTName. |
| 162 |
* |
| 163 |
* @author jason |
| 164 |
*/ |
| 165 |
protected class TypeScope { |
| 166 |
private TypeScope parentScope; |
| 167 |
private HashMap types; |
| 168 |
public TypeScope(TypeScope parent) { |
| 169 |
parentScope = parent; |
| 170 |
types = new HashMap(); |
| 171 |
} |
| 172 |
|
| 173 |
public boolean addType(IASTName name) { |
| 174 |
if (!types.containsKey(name)) { |
| 175 |
types.put(name.toString(),null); |
| 176 |
return true; |
| 177 |
} |
| 178 |
else |
| 179 |
return false; |
| 180 |
} |
| 181 |
|
| 182 |
public boolean isType(IASTName name) { |
| 183 |
if (name != null) { |
| 184 |
if (types.containsKey(name.toString())) |
| 185 |
return true; |
| 186 |
else if (parentScope != null) |
| 187 |
return parentScope.isType(name); |
| 188 |
else |
| 189 |
return false; |
| 190 |
} |
| 191 |
else |
| 192 |
return false; |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
/** |
| 197 |
* A stack of TypeScopes |
| 198 |
* |
| 199 |
* @author jason |
| 200 |
*/ |
| 201 |
protected class TypeScopeStack { |
| 202 |
private LinkedList scopes; |
| 203 |
public TypeScopeStack() { |
| 204 |
scopes = new LinkedList(); |
| 205 |
} |
| 206 |
|
| 207 |
public void push(TypeScope typeScope) { |
| 208 |
if (typeScope != null) |
| 209 |
scopes.addFirst(typeScope); |
| 210 |
} |
| 211 |
|
| 212 |
public TypeScope top() { |
| 213 |
if (scopes.size() > 0) |
| 214 |
return (TypeScope)scopes.getFirst(); |
| 215 |
else |
| 216 |
return null; |
| 217 |
} |
| 218 |
|
| 219 |
public TypeScope pop() { |
| 220 |
if (scopes.size() > 0) |
| 221 |
return (TypeScope)scopes.removeFirst(); |
| 222 |
else |
| 223 |
return null; |
| 224 |
} |
| 225 |
|
| 226 |
public boolean isType(IASTName name) { |
| 227 |
if (scopes.size() > 0) |
| 228 |
return top().isType(name); |
| 229 |
else |
| 230 |
return false; |
| 231 |
} |
| 232 |
|
| 233 |
public boolean addType(IASTName name) { |
| 234 |
if (scopes.size() > 0) |
| 235 |
return top().addType(name); |
| 236 |
else |
| 237 |
return false; |
| 238 |
} |
| 239 |
} |
| 240 |
|
| 241 |
/** |
| 242 |
* @author jason |
| 243 |
*/ |
| 244 |
protected TypeScopeStack typeScopeStack; |
| 245 |
|
| 246 |
/** |
| 247 |
* @author jason |
| 248 |
* @param typeId |
| 249 |
* @return |
| 250 |
*/ |
| 251 |
protected boolean isValidTypeId(IASTTypeId typeId) { |
| 252 |
if (typeId instanceof CASTTypeId) { |
| 253 |
IASTDeclSpecifier declSpecifier = (IASTDeclSpecifier)typeId.getDeclSpecifier(); |
| 254 |
if (declSpecifier instanceof ICASTTypedefNameSpecifier) { |
| 255 |
ICASTTypedefNameSpecifier typedefNameDeclSpecifier = (ICASTTypedefNameSpecifier)declSpecifier; |
| 256 |
IASTName typedefName = typedefNameDeclSpecifier.getName(); |
| 257 |
// if the name is in the type scope then the typeId is valid, otherwise the |
| 258 |
// name is a variable and is not a typeid but an expression |
| 259 |
if (typeScopeStack.isType(typedefName)) |
| 260 |
return true; |
| 261 |
else |
| 262 |
return false; |
| 263 |
} |
| 264 |
else if (declSpecifier instanceof ICASTCompositeTypeSpecifier) |
| 265 |
return true; |
| 266 |
else if (declSpecifier instanceof ICASTSimpleDeclSpecifier) { |
| 267 |
ICASTSimpleDeclSpecifier simpleDeclSpecifier = (ICASTSimpleDeclSpecifier)declSpecifier; |
| 268 |
switch (simpleDeclSpecifier.getType()) |
| 269 |
{ |
| 270 |
case IASTSimpleDeclSpecifier.t_char: |
| 271 |
case IASTSimpleDeclSpecifier.t_double: |
| 272 |
case IASTSimpleDeclSpecifier.t_float: |
| 273 |
case IASTSimpleDeclSpecifier.t_int: |
| 274 |
case IASTSimpleDeclSpecifier.t_void: |
| 275 |
break; |
| 276 |
case IASTSimpleDeclSpecifier.t_unspecified: |
| 277 |
if (!simpleDeclSpecifier.isLong() |
| 278 |
&& !simpleDeclSpecifier.isLongLong() |
| 279 |
&& !simpleDeclSpecifier.isComplex() |
| 280 |
&& !simpleDeclSpecifier.isShort() |
| 281 |
&& !simpleDeclSpecifier.isSigned() |
| 282 |
&& !simpleDeclSpecifier.isUnsigned()) { |
| 283 |
return false; |
| 284 |
} |
| 285 |
break; |
| 286 |
default: |
| 287 |
return false; |
| 288 |
} |
| 289 |
return true; |
| 290 |
} |
| 291 |
else if (declSpecifier instanceof ICASTElaboratedTypeSpecifier) |
| 292 |
return true; |
| 293 |
else if (declSpecifier instanceof ICASTEnumerationSpecifier) { |
| 294 |
return true; |
| 295 |
} |
| 296 |
else |
| 297 |
return false; |
| 298 |
} |
| 299 |
else |
| 300 |
return false; |
| 301 |
} |
| 302 |
|
| 303 |
/** |
| 304 |
* @param simpleDeclaration |
| 305 |
* @author jason |
| 306 |
*/ |
| 307 |
protected void extractTypeNamesToStack(IASTSimpleDeclaration simpleDeclaration) { |
| 308 |
IASTDeclSpecifier declSpecifier = simpleDeclaration.getDeclSpecifier(); |
| 309 |
IASTDeclarator[] declarators = simpleDeclaration.getDeclarators(); |
| 310 |
if (declSpecifier.getStorageClass() == IASTDeclSpecifier.sc_typedef){ |
| 311 |
for(int i = 0; i < declarators.length; i++) { |
| 312 |
IASTDeclarator declarator = declarators[i]; |
| 313 |
IASTName declaratorName = declarators[i].getName(); |
| 314 |
if (declaratorName == null || declaratorName.toCharArray().length == 0) { |
| 315 |
// look at any nested declarators |
| 316 |
IASTDeclarator nestedDeclarator = declarators[i].getNestedDeclarator(); |
| 317 |
if (nestedDeclarator != null) { |
| 318 |
declaratorName = nestedDeclarator.getName(); |
| 319 |
} |
| 320 |
} |
| 321 |
|
| 322 |
if (declaratorName != null && (declaratorName.toCharArray().length > 0)) |
| 323 |
typeScopeStack.addType(declaratorName); |
| 324 |
} |
| 325 |
} |
| 326 |
} |
| 327 |
|
| 328 |
|
| 329 |
/** |
| 144 |
* Look Ahead in the token list to see what is coming. |
330 |
* Look Ahead in the token list to see what is coming. |
| 145 |
* |
331 |
* |
| 146 |
* @param i |
332 |
* @param i |
|
Lines 405-410
Link Here
|
| 405 |
private static final IASTNode[] EMPTY_NODE_ARRAY = new IASTNode[0]; |
591 |
private static final IASTNode[] EMPTY_NODE_ARRAY = new IASTNode[0]; |
| 406 |
|
592 |
|
| 407 |
public IASTTranslationUnit parse() { |
593 |
public IASTTranslationUnit parse() { |
|
|
594 |
// push a type scope for the file on to the type scope stack |
| 595 |
typeScopeStack.push(new TypeScope(null)); |
| 596 |
|
| 408 |
long startTime = System.currentTimeMillis(); |
597 |
long startTime = System.currentTimeMillis(); |
| 409 |
translationUnit(); |
598 |
translationUnit(); |
| 410 |
log.traceLog("Parse " //$NON-NLS-1$ |
599 |
log.traceLog("Parse " //$NON-NLS-1$ |
|
Lines 418-423
Link Here
|
| 418 |
); //$NON-NLS-1$ //$NON-NLS-2$ |
607 |
); //$NON-NLS-1$ //$NON-NLS-2$ |
| 419 |
IASTTranslationUnit result = getTranslationUnit(); |
608 |
IASTTranslationUnit result = getTranslationUnit(); |
| 420 |
nullifyTranslationUnit(); |
609 |
nullifyTranslationUnit(); |
|
|
610 |
|
| 611 |
// pop the file type scope stack before exiting |
| 612 |
typeScopeStack.pop(); |
| 421 |
return result; |
613 |
return result; |
| 422 |
} |
614 |
} |
| 423 |
|
615 |
|
|
Lines 511-516
Link Here
|
| 511 |
|
703 |
|
| 512 |
int startingOffset = consume(IToken.tLBRACE).getOffset(); |
704 |
int startingOffset = consume(IToken.tLBRACE).getOffset(); |
| 513 |
|
705 |
|
|
|
706 |
// push a type scope for the compound statement |
| 707 |
typeScopeStack.push(new TypeScope(typeScopeStack.top())); |
| 708 |
|
| 514 |
((ASTNode) result).setOffset(startingOffset); |
709 |
((ASTNode) result).setOffset(startingOffset); |
| 515 |
result.setPropertyInParent(IASTFunctionDefinition.FUNCTION_BODY); |
710 |
result.setPropertyInParent(IASTFunctionDefinition.FUNCTION_BODY); |
| 516 |
while (LT(1) != IToken.tRBRACE && LT(1) != IToken.tEOC) { |
711 |
while (LT(1) != IToken.tRBRACE && LT(1) != IToken.tEOC) { |
|
Lines 540-545
Link Here
|
| 540 |
int lastOffset = token.getEndOffset(); |
735 |
int lastOffset = token.getEndOffset(); |
| 541 |
((ASTNode) result).setLength(lastOffset - startingOffset); |
736 |
((ASTNode) result).setLength(lastOffset - startingOffset); |
| 542 |
|
737 |
|
|
|
738 |
// NOTE: need to move this pop IF a backtrack exception can occur in processing. |
| 739 |
// currently such an exception only occurs when trying to consume 'tLBRACE' on |
| 740 |
// entering this method. |
| 741 |
|
| 742 |
// pop compound statement type scope |
| 743 |
typeScopeStack.pop(); |
| 744 |
|
| 543 |
return result; |
745 |
return result; |
| 544 |
} |
746 |
} |
| 545 |
|
747 |
|
|
Lines 1587-1592
Link Here
|
| 1587 |
} |
1789 |
} |
| 1588 |
|
1790 |
|
| 1589 |
if (expressionStatement == null && ds != null) { |
1791 |
if (expressionStatement == null && ds != null) { |
|
|
1792 |
|
| 1793 |
// ADDED: jason |
| 1794 |
// a declaration statement has been found. process this declaration and add |
| 1795 |
// any type names to the type scope |
| 1796 |
if (ds.getDeclaration() instanceof IASTSimpleDeclaration) |
| 1797 |
extractTypeNamesToStack((IASTSimpleDeclaration)ds.getDeclaration()); |
| 1590 |
return ds; |
1798 |
return ds; |
| 1591 |
} |
1799 |
} |
| 1592 |
if (expressionStatement != null && ds == null) { |
1800 |
if (expressionStatement != null && ds == null) { |