1
/*******************************************************************************
2
* Copyright (c) 2005, 2011 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
* John Camelon (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.ast.ASTVisitor;
15
import org.eclipse.cdt.core.dom.ast.EScopeKind;
16
import org.eclipse.cdt.core.dom.ast.IASTDeclaration;
17
import org.eclipse.cdt.core.dom.ast.IASTFunctionDefinition;
18
import org.eclipse.cdt.core.dom.ast.IASTName;
19
import org.eclipse.cdt.core.dom.ast.IASTNode;
20
import org.eclipse.cdt.core.dom.ast.IASTParameterDeclaration;
21
import org.eclipse.cdt.core.dom.ast.IASTStandardFunctionDeclarator;
22
import org.eclipse.cdt.core.dom.ast.IScope;
23
import org.eclipse.cdt.core.parser.util.ArrayUtil;
24
import org.eclipse.cdt.internal.core.dom.parser.ASTQueries;
27
* Models function declarators for plain c.
29
public class CASTFunctionDeclarator extends CASTDeclarator implements IASTStandardFunctionDeclarator {
31
private IASTParameterDeclaration [] parameters = null;
32
private int parametersPos=-1;
33
private boolean varArgs;
36
public CASTFunctionDeclarator() {
39
public CASTFunctionDeclarator(IASTName name) {
44
public CASTFunctionDeclarator copy() {
45
return copy(CopyStyle.withoutLocations);
49
public CASTFunctionDeclarator copy(CopyStyle style) {
50
CASTFunctionDeclarator copy = new CASTFunctionDeclarator();
51
copyBaseDeclarator(copy, style);
52
copy.varArgs = varArgs;
54
for(IASTParameterDeclaration param : getParameters())
55
copy.addParameterDeclaration(param == null ? null : param.copy(style));
57
if (style == CopyStyle.withLocations) {
58
copy.setCopyLocation(this);
63
public IASTParameterDeclaration[] getParameters() {
64
if( parameters == null ) return IASTParameterDeclaration.EMPTY_PARAMETERDECLARATION_ARRAY;
65
parameters = (IASTParameterDeclaration[]) ArrayUtil.removeNullsAfter( IASTParameterDeclaration.class, parameters, parametersPos );
69
public void addParameterDeclaration(IASTParameterDeclaration parameter) {
71
if (parameter != null) {
72
parameter.setParent(this);
73
parameter.setPropertyInParent(FUNCTION_PARAMETER);
74
parameters = (IASTParameterDeclaration[]) ArrayUtil.append( IASTParameterDeclaration.class, parameters, ++parametersPos, parameter );
78
public boolean takesVarArgs() {
82
public void setVarArgs(boolean value) {
88
protected boolean postAccept(ASTVisitor action) {
89
IASTParameterDeclaration[] params = getParameters();
90
for (int i = 0; i < params.length; i++) {
91
if (!params[i].accept(action))
94
return super.postAccept(action);
98
public void replace(IASTNode child, IASTNode other) {
99
if( parameters != null ) {
100
for (int i = 0; i < parameters.length; ++i) {
101
if (child == parameters[i]) {
102
other.setPropertyInParent(child.getPropertyInParent());
103
other.setParent(child.getParent());
104
parameters[i]= (IASTParameterDeclaration) other;
109
super.replace(child, other);
112
public IScope getFunctionScope() {
116
// introduce a scope for function declarations and definitions, only.
117
IASTNode node= getParent();
118
while(!(node instanceof IASTDeclaration)) {
121
node= node.getParent();
123
if (node instanceof IASTParameterDeclaration)
126
if (node instanceof IASTFunctionDefinition) {
127
scope= ((IASTFunctionDefinition) node).getScope();
128
} else if (ASTQueries.findTypeRelevantDeclarator(this) == this) {
129
scope = new CScope(this, EScopeKind.eLocal);