Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 0e0274fadbde711efa6015779bb632e0a8400697 (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
/*******************************************************************************
 * Copyright (c) 2004, 2005 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 Corporation - initial API and implementation
 *******************************************************************************/

/*
 * Created on Dec 13, 2004
 */
package org.eclipse.cdt.internal.core.dom.parser.cpp;

import org.eclipse.cdt.core.dom.ast.ASTTypeUtil;
import org.eclipse.cdt.core.dom.ast.DOMException;
import org.eclipse.cdt.core.dom.ast.IBasicType;
import org.eclipse.cdt.core.dom.ast.IPointerType;
import org.eclipse.cdt.core.dom.ast.IType;
import org.eclipse.cdt.core.dom.ast.ITypedef;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPFunctionType;

/**
 * @author aniefer
 */
public class CPPFunctionType implements ICPPFunctionType {
    private IType[] parameters;
    private IType returnType;
    private IPointerType thisType;
    
    /**
     * @param returnType
     * @param types
     */
    public CPPFunctionType(IType returnType, IType[] types) {
        this.returnType = returnType;
        this.parameters = types;
    }

	public CPPFunctionType(IType returnType, IType[] types, IPointerType thisType) {
        this.returnType = returnType;
        this.parameters = types;
        this.thisType = thisType;
    }

    public boolean isSameType(IType o) {
        if (o instanceof ITypedef)
            return o.isSameType(this);
        if (o instanceof ICPPFunctionType) {
            ICPPFunctionType ft = (ICPPFunctionType) o;
            IType[] fps;
            try {
                fps = ft.getParameterTypes();
            } catch (DOMException e) {
                return false;
            }
			try {
                //constructors & destructors have null return type
                if ((returnType == null) ^ (ft.getReturnType() == null))
                    return false;
                else if (returnType != null && ! returnType.isSameType(ft.getReturnType()))
                    return false;
            } catch (DOMException e1) {
                return false;
            }
			
			try {
				if (parameters.length == 1 && fps.length == 0) {
					if (!(parameters[0] instanceof IBasicType) || ((IBasicType) parameters[0]).getType() != IBasicType.t_void)
						return false;
				} else if (fps.length == 1 && parameters.length == 0) {
					if (!(fps[0] instanceof IBasicType) || ((IBasicType) fps[0]).getType() != IBasicType.t_void)
						return false;
				} else if (parameters.length != fps.length) {
	                return false;
	            } else {
					for (int i = 0; i < parameters.length; i++) {
		                if (parameters[i] == null || ! parameters[i].isSameType(fps[i]))
		                    return false;
		            }
	            }
			} catch (DOMException e) {
				return false;
			}
           
            if (isConst() != ft.isConst() || isVolatile() != ft.isVolatile()) {
                return false;
            }
                
            return true;
        }
        return false;
    }

    /* (non-Javadoc)
     * @see org.eclipse.cdt.core.dom.ast.IFunctionType#getReturnType()
     */
    public IType getReturnType() {
        return returnType;
    }

    /* (non-Javadoc)
     * @see org.eclipse.cdt.core.dom.ast.IFunctionType#getParameterTypes()
     */
    public IType[] getParameterTypes() {
        return parameters;
    }

    @Override
	public Object clone() {
        IType t = null;
   		try {
            t = (IType) super.clone();
        } catch (CloneNotSupportedException e) {
            //not going to happen
        }
        return t;
    }

    /* (non-Javadoc)
     * @see org.eclipse.cdt.core.dom.ast.ICPPFunctionType#getThisType()
     */
    public IPointerType getThisType() {
        return thisType;
    }

	public final boolean isConst() {
		return thisType != null && thisType.isConst();
	}

	public final boolean isVolatile() {
		return thisType != null && thisType.isVolatile();
	}

	@Override
	public String toString() {
		return ASTTypeUtil.getType(this);
	}
}

Back to the top