Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: d71416ec72f850dee867fcf952e7c0a35a6b737d (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
158
159
160
161
162
163
/*******************************************************************************
 * Copyright (c) 2005, 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
 *    Markus Schorn (Wind River Systems)
 *******************************************************************************/
package org.eclipse.cdt.internal.core.dom.parser.cpp;

import org.eclipse.cdt.core.dom.ast.IASTName;
import org.eclipse.cdt.core.dom.ast.IASTParameterDeclaration;
import org.eclipse.cdt.core.dom.ast.IBinding;
import org.eclipse.cdt.core.dom.ast.IFunctionType;
import org.eclipse.cdt.core.dom.ast.IParameter;
import org.eclipse.cdt.core.dom.ast.IScope;
import org.eclipse.cdt.core.dom.ast.IType;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTFunctionDeclarator;
import org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.CPPVisitor;

/**
 * The CPPImplicitFunction is used to represent implicit functions that exist on the translation
 * unit but are not actually part of the physical AST created by CDT.
 * 
 * An example is GCC built-in functions.
 * 
 * @author dsteffle
 */
public class CPPImplicitFunction extends CPPFunction {

	private IParameter[] parms=null;
	private IScope scope=null;
	private IType returnType=null;
    private IFunctionType functionType=null;
	private boolean takesVarArgs=false;
	private char[] name=null;
	
	public CPPImplicitFunction(char[] name, IScope scope, IType type, IParameter[] parms, boolean takesVarArgs) {
        super( null );
        this.name=name;
		this.scope=scope;
		this.returnType=type;
		this.parms=parms;
		this.takesVarArgs=takesVarArgs;
	}

    @Override
	public IParameter [] getParameters() {
        return parms;
    }
    
    @Override
	public IFunctionType getType() {
        if( functionType == null ){
            ICPPASTFunctionDeclarator primary = getPrimaryDeclaration();
            
            if( primary != null ){
                functionType = super.getType();
            } else {
                functionType = CPPVisitor.createImplicitFunctionType( returnType, parms );
            }
        }
        
        return functionType;
    }
    
    private ICPPASTFunctionDeclarator getPrimaryDeclaration() {
        if (definition != null)
            return definition;
        else if (declarations != null && declarations.length > 0)
            return declarations[0];
            
        return null;
    }

    @Override
	public String getName() {
        return String.valueOf( name );
    }

    /* (non-Javadoc)
     * @see org.eclipse.cdt.core.dom.ast.IBinding#getNameCharArray()
     */
    @Override
	public char[] getNameCharArray() {
        return name;
    }
    /* (non-Javadoc)
     * @see org.eclipse.cdt.core.dom.ast.IBinding#getScope()
     */
    @Override
	public IScope getScope() {
        return scope;
    }
    
    /* (non-Javadoc)
     * @see org.eclipse.cdt.core.dom.ast.IFunction#getFunctionScope()
     */
    @Override
	public IScope getFunctionScope() {
        return null;
    }
    
    @Override
	public IBinding resolveParameter( IASTParameterDeclaration param ){
        IASTName aName = param.getDeclarator().getName();
        IParameter binding = (IParameter) aName.getBinding();
        if( binding != null )
            return binding;
        
        //get the index in the parameter list
        ICPPASTFunctionDeclarator fdtor = (ICPPASTFunctionDeclarator) param.getParent();
        IASTParameterDeclaration [] ps = fdtor.getParameters();
        int i = 0;
        for( ; i < ps.length; i++ ){
            if( param == ps[i] )
                break;
        }
        
        //set the binding for the corresponding parameter in all known defns and decls
        binding = parms[i];
        IASTParameterDeclaration temp = null;
        if( definition != null ){
            temp = definition.getParameters()[i];
            IASTName n = temp.getDeclarator().getName();
            n.setBinding( binding );
            ((CPPParameter)binding).addDeclaration( n );
        }
        if( declarations != null ){
            for( int j = 0; j < declarations.length && declarations[j] != null; j++ ){
                temp = declarations[j].getParameters()[i];
                IASTName n = temp.getDeclarator().getName();
                n.setBinding( binding );
                ((CPPParameter)binding).addDeclaration( n );
            }
        }
        return binding;
    }
   
    @Override
	protected void updateParameterBindings( ICPPASTFunctionDeclarator fdtor ){
        if( parms != null ){
            IASTParameterDeclaration [] nps = fdtor.getParameters();
            if( nps.length != parms.length )
                return;

            for( int i = 0; i < nps.length; i++ ){
                IASTName aName = nps[i].getDeclarator().getName(); 
                aName.setBinding( parms[i] );
				if( parms[i] instanceof ICPPInternalBinding )
					((ICPPInternalBinding)parms[i]).addDeclaration( aName );
            }
        }
    }

    @Override
	public boolean takesVarArgs() {
        return takesVarArgs;
    }
}

Back to the top