Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: c53833c0db3fadb02c4b6f365a6e86c3376813c7 (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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*******************************************************************************
 * Copyright (c) 2003, 2006 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 Corp. - Rational Software - initial implementation
 *******************************************************************************/
package org.eclipse.cdt.internal.core.parser.ast.complete;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

import org.eclipse.cdt.core.parser.ParserMode;
import org.eclipse.cdt.core.parser.ast.ASTNotImplementedException;
import org.eclipse.cdt.core.parser.ast.IASTExpression;
import org.eclipse.cdt.core.parser.ast.IASTNode;
import org.eclipse.cdt.internal.core.parser.ast.SymbolIterator;
import org.eclipse.cdt.internal.core.parser.pst.IContainerSymbol;
import org.eclipse.cdt.internal.core.parser.pst.IExtensibleSymbol;
import org.eclipse.cdt.internal.core.parser.pst.ISymbol;
import org.eclipse.cdt.internal.core.parser.pst.ISymbolOwner;
import org.eclipse.cdt.internal.core.parser.pst.ParserSymbolTableError;
import org.eclipse.cdt.internal.core.parser.pst.ParserSymbolTableException;
import org.eclipse.cdt.internal.core.parser.pst.TypeFilter;

/**
 * @author aniefer
 */
public class ASTNode implements IASTNode {

	/* (non-Javadoc)
	 * @see org.eclipse.cdt.core.parser.ast.IASTNode#lookup(java.lang.String, org.eclipse.cdt.core.parser.ast.IASTNode.LookupKind, org.eclipse.cdt.core.parser.ast.IASTNode)
	 */
	public ILookupResult lookup(String prefix, LookupKind[] kind, IASTNode context, IASTExpression functionParameters) throws LookupError, ASTNotImplementedException {

		if( ! ( this instanceof ISymbolOwner ) ){
			return null;
		}
		
		IExtensibleSymbol symbol = ((ISymbolOwner)this).getSymbol();
		if( symbol == null || !(symbol instanceof IContainerSymbol) ){
			throw new LookupError();
		}
		IContainerSymbol thisContainer = (IContainerSymbol) symbol; 
		IContainerSymbol qualification = ( context != null ) ? ((ASTNode)context).getLookupQualificationSymbol() : null;
		
		// trying to dereference a context of unknown type
		if (context != null && qualification == null)
			return null;
		
		List parameters = createLookupParameterList( functionParameters );
		
		int paramIndex = ( parameters != null ) ? parameters.size() : 0;
		
		if( thisContainer.getSymbolTable().getParserMode() != ParserMode.COMPLETION_PARSE ){
			throw new ASTNotImplementedException();
		}
		
		TypeFilter filter = new TypeFilter();
		if( kind != null ){
			for( int i = 0; i < kind.length; i++ ){
				filter.addAcceptedType( kind[i] );
				if( kind[i] == LookupKind.THIS ){
					filter.setLookingInThis( true );
					if( kind.length == 1 ){
						filter.addAcceptedType( LookupKind.ALL );
					}
				} else {
					filter.addAcceptedType( kind[i] );
				}
			}	
		} else {
			filter.addAcceptedType( LookupKind.ALL );
		}
		
		List lookupResults = performPrefixLookup(prefix.toCharArray(), thisContainer, qualification, filter, parameters);
		
		if(lookupResults == null)
			return null;
		
		//filter out things that are not visible and things that don't have AST nodes attached
		ListIterator iter = lookupResults.listIterator();
		while( iter.hasNext() ){
			ISymbol s = (ISymbol) iter.next();
			if( !thisContainer.isVisible( s, qualification ) ||
			    s.getASTExtension() == null ||
				s.getASTExtension().getPrimaryDeclaration() == null )
			{
				iter.remove();
				continue;
			}
			
			if( context != null && ((ASTNode)context).shouldFilterLookupResult( s ) )
				iter.remove();
			
		}
		
		SymbolIterator iterator = new SymbolIterator( lookupResults.iterator() );

		return new Result( prefix, iterator, lookupResults.size(), paramIndex );
	}
	
	/**
	 * @param prefix
	 * @param thisContainer
	 * @param qualification
	 * @param filter
	 * @param paramList TODO
	 * @param lookInThis
	 * @param lookupResults
	 * @return
	 * @throws LookupError
	 */
	protected List performPrefixLookup(char[] prefix, IContainerSymbol thisContainer, IContainerSymbol qualification, TypeFilter filter, List paramList) throws LookupError {
		List results = null;
		try {
			if( qualification != null ){
				results = qualification.prefixLookup( filter, prefix, true, paramList );
			} else {
				results = thisContainer.prefixLookup( filter, prefix, false, paramList );
			}
		} catch (ParserSymbolTableException e) {
			throw new LookupError();
		} catch (ParserSymbolTableError e ){
			throw new LookupError();
		}
		return results;
	}

	/**
	 * @param context
	 * @param qualification
	 * @return
	 * @throws LookupError
	 */
	public IContainerSymbol getLookupQualificationSymbol() throws LookupError {
		throw new LookupError();
	}
	
	public boolean shouldFilterLookupResult( ISymbol symbol ){
		return false;
	}

	public List createLookupParameterList( IASTExpression parameterExpression ){
		if( parameterExpression == null )
			return null;
		
		List params = new ArrayList();
		ASTExpression exp = (ASTExpression) parameterExpression;
		while( exp != null ){
			params.add( exp.getResultType().getResult() );
			exp = (ASTExpression) exp.getRHSExpression();
		}
		return params;
	}
	
	private class Result implements ILookupResult{
		private String prefix;
		private Iterator iterator;
		private int resultsNumber;
		private int parameterIndex;

		public Result( String pref, Iterator iter, int resultsSize, int paramIndex ){
			prefix = pref;
			iterator = iter;
			resultsNumber = resultsSize;
			parameterIndex = paramIndex;
		}
		
		public String getPrefix() 	{	return prefix;	 }
		public Iterator getNodes() 	{	return iterator; }
		public int getResultsSize() { return resultsNumber; }
		public int getIndexOfNextParameter() { return parameterIndex; }
	}
}

Back to the top