Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 973f467cb71f555f1c1323f799776c2e96d90a1e (plain) (blame)
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
/*******************************************************************************
 * Copyright (c) 2004, 2008 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 - Initial API and implementation
 *******************************************************************************/
package org.eclipse.cdt.internal.core.dom.parser.cpp;

import org.eclipse.cdt.core.dom.ast.ASTNodeProperty;
import org.eclipse.cdt.core.dom.ast.ASTVisitor;
import org.eclipse.cdt.core.dom.ast.IASTFunctionDefinition;
import org.eclipse.cdt.core.dom.ast.IASTInitializer;
import org.eclipse.cdt.core.dom.ast.IASTName;
import org.eclipse.cdt.core.dom.ast.IASTParameterDeclaration;
import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclaration;
import org.eclipse.cdt.core.dom.ast.IASTTypeId;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTConstructorChainInitializer;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTFunctionDeclarator;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPFunctionScope;
import org.eclipse.cdt.core.parser.util.ArrayUtil;

/**
 * @author jcamelon
 */
public class CPPASTFunctionDeclarator extends CPPASTDeclarator implements ICPPASTFunctionDeclarator {
    private IASTParameterDeclaration[] parameters = null;
    private int parametersPos = -1;
    private ICPPFunctionScope scope = null;
    private boolean varArgs;
    private boolean pureVirtual;
    private boolean isVolatile;
    private boolean isConst;
    private IASTTypeId[] typeIds = null;
    private int typeIdsPos = -1;
    private ICPPASTConstructorChainInitializer[] constructorChain = null;
    private int constructorChainPos = -1;

    public CPPASTFunctionDeclarator() {
	}

	public CPPASTFunctionDeclarator(IASTName name) {
		super(name);
	}

	public IASTParameterDeclaration[] getParameters() {
        if (parameters == null) return IASTParameterDeclaration.EMPTY_PARAMETERDECLARATION_ARRAY;
        parameters = (IASTParameterDeclaration[]) ArrayUtil.removeNullsAfter(IASTParameterDeclaration.class, parameters, parametersPos);
        return parameters;
    }

    public void addParameterDeclaration(IASTParameterDeclaration parameter) {
    	if (parameter != null) {
    		parameter.setParent(this);
			parameter.setPropertyInParent(FUNCTION_PARAMETER);
    		parameters = (IASTParameterDeclaration[]) ArrayUtil.append(IASTParameterDeclaration.class, parameters, ++parametersPos, parameter);
    	}
    }

    public boolean takesVarArgs() {
        return varArgs;
    }

    public void setVarArgs(boolean value) {
        varArgs = value;
    }

    public boolean isConst() {
        return isConst;
    }

    public void setConst(boolean value) {
        this.isConst = value;
    }

    public boolean isVolatile() {
        return isVolatile;
    }

    public void setVolatile(boolean value) {
        this.isVolatile = value;
    }

    public IASTTypeId[] getExceptionSpecification() {
        if (typeIds == null) return IASTTypeId.EMPTY_TYPEID_ARRAY;
        typeIds = (IASTTypeId[]) ArrayUtil.removeNullsAfter(IASTTypeId.class, typeIds, typeIdsPos);
        return typeIds;
    }

    public void addExceptionSpecificationTypeId(IASTTypeId typeId) {
    	if (typeId != null) {
    		typeIds = (IASTTypeId[]) ArrayUtil.append(IASTTypeId.class, typeIds, ++typeIdsPos, typeId);
    		typeId.setParent(this);
			typeId.setPropertyInParent(EXCEPTION_TYPEID);
    	}
    }

    public boolean isPureVirtual() {
        return pureVirtual;
    }

    public void setPureVirtual(boolean isPureVirtual) {
        this.pureVirtual = isPureVirtual;
    }

    public ICPPASTConstructorChainInitializer[] getConstructorChain() {
        if (constructorChain == null) return ICPPASTConstructorChainInitializer.EMPTY_CONSTRUCTORCHAININITIALIZER_ARRAY;
        constructorChain = (ICPPASTConstructorChainInitializer[]) ArrayUtil.removeNullsAfter(
        		ICPPASTConstructorChainInitializer.class, constructorChain, constructorChainPos);
        return constructorChain;
    }

    public void addConstructorToChain(ICPPASTConstructorChainInitializer initializer) {
    	if (initializer != null) {
    		constructorChain = (ICPPASTConstructorChainInitializer[]) ArrayUtil.append(
    				ICPPASTConstructorChainInitializer.class, constructorChain, ++constructorChainPos, initializer);
    		initializer.setParent(this);
			initializer.setPropertyInParent(CONSTRUCTOR_CHAIN_MEMBER);
    	}
    }

    public ICPPFunctionScope getFunctionScope() {
        if (scope != null)
            return scope;
        
        ASTNodeProperty prop = getPropertyInParent();
        if (prop == IASTSimpleDeclaration.DECLARATOR || prop == IASTFunctionDefinition.DECLARATOR) {
            scope = new CPPFunctionScope(this);
        }
        return scope;
    }
    
    @Override
	protected boolean postAccept(ASTVisitor action) {
        IASTParameterDeclaration[] params = getParameters();
        for (int i = 0; i < params.length; i++) {
            if (!params[i].accept(action)) return false;
        }
        
        ICPPASTConstructorChainInitializer[] chain = getConstructorChain();
        for (int i = 0; i < chain.length; i++) {
            if (!chain[i].accept(action)) return false;
        }
        
        IASTInitializer initializer = getInitializer();
        if (initializer != null && !initializer.accept(action)) return false;
        
        IASTTypeId[] ids = getExceptionSpecification();
        for (int i = 0; i < ids.length; i++) {
            if (!ids[i].accept(action)) return false;
        }
        return true;
    }
}

Back to the top