Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 878c59f13f3bc7835c510206b86d3413a531261c (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
/*******************************************************************************
 * Copyright (c) 2010, 2015 Wind River Systems, Inc. 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:
 *     Markus Schorn - initial API and implementation
 *******************************************************************************/
package org.eclipse.cdt.internal.core.dom.parser.cpp.semantics;

import org.eclipse.cdt.core.dom.ILinkage;
import org.eclipse.cdt.core.dom.ast.DOMException;
import org.eclipse.cdt.core.dom.ast.IASTName;
import org.eclipse.cdt.core.dom.ast.IBinding;
import org.eclipse.cdt.core.dom.ast.IScope;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPFunction;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPTemplateArgument;
import org.eclipse.cdt.core.parser.util.ArrayUtil;
import org.eclipse.cdt.core.parser.util.CharArrayUtils;
import org.eclipse.cdt.internal.core.dom.Linkage;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTNameBase;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPDeferredFunction;
import org.eclipse.cdt.internal.core.dom.parser.cpp.ICPPTwoPhaseBinding;

/**
 * Used as intermediate binding for names nominating a function without calling it.
 * The actual function can be resolved in certain contexts.
 */
public class CPPFunctionSet implements ICPPTwoPhaseBinding {
	private final ICPPFunction[] fBindings;
	private final IASTName fName;
	private final ICPPTemplateArgument[] fTemplateArguments;

	public CPPFunctionSet(ICPPFunction[] bindingList, ICPPTemplateArgument[] args, IASTName name) {
		fBindings = ArrayUtil.removeNulls(bindingList);
		fTemplateArguments= args;
		fName= name;
	}

	@Override
	public String getName() {
		return fBindings[0].getName();
	}

	@Override
	public char[] getNameCharArray() {
		return fBindings[0].getNameCharArray();
	}

	@Override
	public IScope getScope() throws DOMException {
		return fBindings[0].getScope();
	}

	@Override
	public IBinding getOwner() {
		return fBindings[0].getOwner();
	}

	public ICPPFunction[] getBindings() {
		return fBindings;
	}

	@Override
	public ILinkage getLinkage() {
		return Linkage.CPP_LINKAGE;
	}

	@Override
	public IBinding resolveFinalBinding(CPPASTNameBase astName) {
		return CPPSemantics.resolveTargetedFunction(astName, this);
	}

	@Override
	@SuppressWarnings("unchecked")
	public Object getAdapter(@SuppressWarnings("rawtypes") Class adapter) {
		if (adapter.isAssignableFrom(getClass()))
			return this;
		return null;
	}

	/**
	 * Returns the template arguments, or {@code null} if the function set doesn't represent a template
	 * specialization.
	 */
	public ICPPTemplateArgument[] getTemplateArguments() {
		return fTemplateArguments;
	}

	public void applySelectedFunction(ICPPFunction selectedFunction) {
		if (selectedFunction != null && fName != null) {
			fName.setBinding(selectedFunction);
		}
	}

	public void setToUnknown() {
		if (fName != null) {
			fName.setBinding(new CPPDeferredFunction(null, fName.toCharArray(), fBindings));
		}
	}

	/** For debugging only */
	@Override
	public String toString() {
		if (fName != null)
			return fName.toString();
		try {
			return String.join("::", fBindings[0].getQualifiedName()); //$NON-NLS-1$
		} catch (DOMException e) {
			return super.toString();
		}
	}
	
	@Override
	public boolean equals(Object other) {
		if (!(other instanceof CPPFunctionSet)) {
			return false;
		}
		CPPFunctionSet o = (CPPFunctionSet) other;
		return CPPEvaluation.areEquivalentBindings(fBindings, o.fBindings)
			&& fName == o.fName
			&& CPPEvaluation.areEquivalentArguments(fTemplateArguments, o.fTemplateArguments);
	}
}

Back to the top