1
/*******************************************************************************
2
* Copyright (c) 2004, 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 Rational Software - Initial API and implementation
10
* Markus Schorn (Wind River Systems)
11
*******************************************************************************/
12
package org.eclipse.cdt.internal.core.dom.parser.c;
14
import org.eclipse.cdt.core.dom.ILinkage;
15
import org.eclipse.cdt.core.dom.ast.DOMException;
16
import org.eclipse.cdt.core.dom.ast.IASTCompoundStatement;
17
import org.eclipse.cdt.core.dom.ast.IASTDeclSpecifier;
18
import org.eclipse.cdt.core.dom.ast.IASTDeclaration;
19
import org.eclipse.cdt.core.dom.ast.IASTDeclarator;
20
import org.eclipse.cdt.core.dom.ast.IASTFunctionDeclarator;
21
import org.eclipse.cdt.core.dom.ast.IASTFunctionDefinition;
22
import org.eclipse.cdt.core.dom.ast.IASTName;
23
import org.eclipse.cdt.core.dom.ast.IASTNode;
24
import org.eclipse.cdt.core.dom.ast.IASTParameterDeclaration;
25
import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclaration;
26
import org.eclipse.cdt.core.dom.ast.IBinding;
27
import org.eclipse.cdt.core.dom.ast.IParameter;
28
import org.eclipse.cdt.core.dom.ast.IScope;
29
import org.eclipse.cdt.core.dom.ast.IType;
30
import org.eclipse.cdt.core.dom.ast.IValue;
31
import org.eclipse.cdt.core.dom.ast.gnu.c.ICASTKnRFunctionDeclarator;
32
import org.eclipse.cdt.core.parser.util.ArrayUtil;
33
import org.eclipse.cdt.internal.core.dom.Linkage;
34
import org.eclipse.cdt.internal.core.dom.parser.ProblemBinding;
35
import org.eclipse.core.runtime.PlatformObject;
38
* Represents the parameter of a function.
40
public class CParameter extends PlatformObject implements IParameter {
41
public static class CParameterProblem extends ProblemBinding implements IParameter {
42
public CParameterProblem( IASTNode node, int id, char[] arg ) {
43
super( node, id, arg );
45
public IType getType() throws DOMException {
46
throw new DOMException( this );
48
public boolean isStatic() throws DOMException {
49
throw new DOMException( this );
51
public boolean isExtern() throws DOMException {
52
throw new DOMException( this );
54
public boolean isAuto() throws DOMException {
55
throw new DOMException( this );
57
public boolean isRegister() throws DOMException {
58
throw new DOMException( this );
60
public IValue getInitialValue() {
65
private IASTName [] declarations;
66
private IType type = null;
68
public CParameter( IASTName parameterName ){
69
this.declarations = new IASTName [] { parameterName };
73
* @see org.eclipse.cdt.core.dom.ast.IVariable#getType()
76
public IType getType() {
77
if ( type == null && declarations[0].getParent() instanceof IASTDeclarator)
78
type = CVisitor.createType( (IASTDeclarator)declarations[0].getParent() );
83
private IASTName getPrimaryDeclaration(){
84
if( declarations != null ){
85
for( int i = 0; i < declarations.length && declarations[i] != null; i++ ){
86
IASTNode node = declarations[i].getParent();
87
while( !(node instanceof IASTDeclaration) )
88
node = node.getParent();
90
if( node.getPropertyInParent() == ICASTKnRFunctionDeclarator.FUNCTION_PARAMETER ||
91
node instanceof IASTFunctionDefinition )
93
return declarations[i];
96
return declarations[0];
102
* @see org.eclipse.cdt.core.dom.ast.IBinding#getName()
104
public String getName() {
105
IASTName name = getPrimaryDeclaration();
107
return name.toString();
108
return CVisitor.EMPTY_STRING;
110
public char[] getNameCharArray(){
111
IASTName name = getPrimaryDeclaration();
113
return name.toCharArray();
114
return CVisitor.EMPTY_CHAR_ARRAY;
118
* @see org.eclipse.cdt.core.dom.ast.IBinding#getScope()
120
public IScope getScope() {
121
//IASTParameterDeclaration or IASTSimpleDeclaration
122
for( int i = 0; i < declarations.length; i++ ){
123
IASTNode parent = declarations[i].getParent();
124
if( parent instanceof ICASTKnRFunctionDeclarator ){
125
parent = parent.getParent();
126
return ((IASTCompoundStatement)((IASTFunctionDefinition)parent).getBody()).getScope();
129
IASTNode fdtorNode = parent.getParent().getParent();
130
if (fdtorNode instanceof IASTFunctionDeclarator) {
131
IASTFunctionDeclarator fdtor = (IASTFunctionDeclarator)fdtorNode;
132
parent = fdtor.getParent();
133
if( parent instanceof IASTFunctionDefinition ) {
134
return ((IASTCompoundStatement)((IASTFunctionDefinition)parent).getBody()).getScope();
138
//TODO: if not definition, find definition
145
public void addDeclaration(IASTName name) {
146
if (name != null && name.isActive())
147
declarations = (IASTName[]) ArrayUtil.append(IASTName.class, declarations, name);
151
* @see org.eclipse.cdt.core.dom.ast.IVariable#isStatic()
153
public boolean isStatic(){
158
* @see org.eclipse.cdt.core.dom.ast.IVariable#isExtern()
160
public boolean isExtern() {
165
* @see org.eclipse.cdt.core.dom.ast.IVariable#isAuto()
167
public boolean isAuto() {
168
return hasStorageClass( IASTDeclSpecifier.sc_auto );
172
* @see org.eclipse.cdt.core.dom.ast.IVariable#isRegister()
174
public boolean isRegister() {
175
return hasStorageClass( IASTDeclSpecifier.sc_register );
178
public boolean hasStorageClass(int storage) {
179
if (declarations == null)
182
for (int i = 0; i < declarations.length && declarations[i] != null; i++) {
183
IASTNode parent= declarations[i].getParent();
184
while (!(parent instanceof IASTParameterDeclaration) && !(parent instanceof IASTDeclaration))
185
parent= parent.getParent();
187
IASTDeclSpecifier declSpec = null;
188
if (parent instanceof IASTSimpleDeclaration) {
189
declSpec = ((IASTSimpleDeclaration) parent).getDeclSpecifier();
190
} else if (parent instanceof IASTParameterDeclaration) {
191
declSpec = ((IASTParameterDeclaration) parent).getDeclSpecifier();
193
if (declSpec != null)
194
return declSpec.getStorageClass() == storage;
199
public ILinkage getLinkage() {
200
return Linkage.C_LINKAGE;
203
public IBinding getOwner() throws DOMException {
204
if (declarations == null || declarations.length == 0)
207
return CVisitor.findEnclosingFunction(declarations[0]);
210
public IValue getInitialValue() {