1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-08-01 13:25:45 +02:00

Improved type safety.

This commit is contained in:
Sergey Prigogin 2014-04-23 11:26:35 -07:00
parent 8ce322ad37
commit f7f3825ef8
5 changed files with 36 additions and 27 deletions

View file

@ -23,6 +23,8 @@ public class CharArrayUtils {
/** @since 5.4 */ /** @since 5.4 */
public static final char[] EMPTY_CHAR_ARRAY = {}; public static final char[] EMPTY_CHAR_ARRAY = {};
public static final char[] EMPTY = EMPTY_CHAR_ARRAY; public static final char[] EMPTY = EMPTY_CHAR_ARRAY;
/** @since 5.7 */
public static final char[][] EMPTY_ARRAY_OF_CHAR_ARRAYS = {};
private CharArrayUtils() {} private CharArrayUtils() {}

View file

@ -169,16 +169,28 @@ public class CharTable extends HashTable {
return i; return i;
// Follow the next chain // Follow the next chain
for (i = nextTable[i] - 1; i >= 0 && nextTable[i] != i + 1; i = nextTable[i] - 1) for (i = nextTable[i] - 1; i >= 0 && nextTable[i] != i + 1; i = nextTable[i] - 1) {
if (CharArrayUtils.equals(buffer, start, len, keyTable[i])) if (CharArrayUtils.equals(buffer, start, len, keyTable[i]))
return i; return i;
}
return -1; return -1;
} }
public Object[] keyArray() { /**
Object[] keys = new Object[size()]; * @since 5.7
*/
public char[][] keys() {
char[][] keys = new char[size()][];
System.arraycopy(keyTable, 0, keys, 0, keys.length); System.arraycopy(keyTable, 0, keys, 0, keys.length);
return keys; return keys;
} }
/**
* @deprecated Use {@link #keys()} instead.
*/
@Deprecated
public Object[] keyArray() {
return keys();
}
} }

View file

@ -340,9 +340,8 @@ public class CScope implements ICScope, IASTInternalScope {
for (CharArrayObjectMap<?> map : mapsToNameOrBinding) { for (CharArrayObjectMap<?> map : mapsToNameOrBinding) {
if (lookup.isPrefixLookup()) { if (lookup.isPrefixLookup()) {
IContentAssistMatcher matcher = ContentAssistMatcherFactory.getInstance().createMatcher(c); IContentAssistMatcher matcher = ContentAssistMatcherFactory.getInstance().createMatcher(c);
Object[] keys = map.keyArray(); char[][] keys = map.keys();
for (Object key2 : keys) { for (char[] key : keys) {
char[] key = (char[]) key2;
if (matcher.match(key)) { if (matcher.match(key)) {
obj = ArrayUtil.append(obj, map.get(key)); obj = ArrayUtil.append(obj, map.get(key));
} }

View file

@ -38,6 +38,7 @@ import org.eclipse.cdt.core.index.IIndexFileSet;
import org.eclipse.cdt.core.index.IndexFilter; import org.eclipse.cdt.core.index.IndexFilter;
import org.eclipse.cdt.core.parser.util.ArrayUtil; import org.eclipse.cdt.core.parser.util.ArrayUtil;
import org.eclipse.cdt.core.parser.util.CharArrayObjectMap; import org.eclipse.cdt.core.parser.util.CharArrayObjectMap;
import org.eclipse.cdt.core.parser.util.CharArrayUtils;
import org.eclipse.cdt.core.parser.util.IContentAssistMatcher; import org.eclipse.cdt.core.parser.util.IContentAssistMatcher;
import org.eclipse.cdt.core.parser.util.ObjectSet; import org.eclipse.cdt.core.parser.util.ObjectSet;
import org.eclipse.cdt.internal.core.dom.parser.ProblemBinding; import org.eclipse.cdt.internal.core.dom.parser.ProblemBinding;
@ -94,7 +95,7 @@ abstract public class CPPScope implements ICPPASTInternalScope {
return; return;
if (bindings == null) if (bindings == null)
bindings = new CharArrayObjectMap<Object>(1); bindings = new CharArrayObjectMap<>(1);
if (name instanceof ICPPASTQualifiedName && if (name instanceof ICPPASTQualifiedName &&
!(physicalNode instanceof ICPPASTCompositeTypeSpecifier) && !(physicalNode instanceof ICPPASTCompositeTypeSpecifier) &&
!(physicalNode instanceof ICPPASTNamespaceDefinition)) { !(physicalNode instanceof ICPPASTNamespaceDefinition)) {
@ -109,7 +110,7 @@ abstract public class CPPScope implements ICPPASTInternalScope {
if (o instanceof ObjectSet) { if (o instanceof ObjectSet) {
((ObjectSet<Object>) o).put(name); ((ObjectSet<Object>) o).put(name);
} else { } else {
ObjectSet<Object> temp = new ObjectSet<Object>(2); ObjectSet<Object> temp = new ObjectSet<>(2);
temp.put(o); temp.put(o);
temp.put(name); temp.put(name);
bindings.put(c, temp); bindings.put(c, temp);
@ -229,11 +230,10 @@ abstract public class CPPScope implements ICPPASTInternalScope {
Object obj = null; Object obj = null;
if (lookup.isPrefixLookup()) { if (lookup.isPrefixLookup()) {
Object[] keys = bindings != null ? bindings.keyArray() : new Object[0]; char[][] keys = bindings != null ? bindings.keys() : CharArrayUtils.EMPTY_ARRAY_OF_CHAR_ARRAYS;
ObjectSet<Object> all= new ObjectSet<Object>(16); ObjectSet<Object> all= new ObjectSet<>(16);
IContentAssistMatcher matcher = ContentAssistMatcherFactory.getInstance().createMatcher(c); IContentAssistMatcher matcher = ContentAssistMatcherFactory.getInstance().createMatcher(c);
for (Object key2 : keys) { for (char[] key : keys) {
final char[] key = (char[]) key2;
if (key != CONSTRUCTOR_KEY && matcher.match(key)) { if (key != CONSTRUCTOR_KEY && matcher.match(key)) {
obj= bindings.get(key); obj= bindings.get(key);
if (obj instanceof ObjectSet<?>) { if (obj instanceof ObjectSet<?>) {
@ -335,9 +335,6 @@ abstract public class CPPScope implements ICPPASTInternalScope {
} }
} }
/* (non-Javadoc)
* @see org.eclipse.cdt.core.dom.ast.IScope#find(java.lang.String)
*/
@Override @Override
public IBinding[] find(String name) { public IBinding[] find(String name) {
return CPPSemantics.findBindings(this, name, false); return CPPSemantics.findBindings(this, name, false);
@ -347,7 +344,7 @@ abstract public class CPPScope implements ICPPASTInternalScope {
@SuppressWarnings({ "unchecked" }) @SuppressWarnings({ "unchecked" })
public void addBinding(IBinding binding) { public void addBinding(IBinding binding) {
if (bindings == null) if (bindings == null)
bindings = new CharArrayObjectMap<Object>(1); bindings = new CharArrayObjectMap<>(1);
char[] c = binding.getNameCharArray(); char[] c = binding.getNameCharArray();
if (c.length == 0) { if (c.length == 0) {
return; return;
@ -357,7 +354,7 @@ abstract public class CPPScope implements ICPPASTInternalScope {
if (o instanceof ObjectSet) { if (o instanceof ObjectSet) {
((ObjectSet<Object>) o).put(binding); ((ObjectSet<Object>) o).put(binding);
} else { } else {
ObjectSet<Object> set = new ObjectSet<Object>(2); ObjectSet<Object> set = new ObjectSet<>(2);
set.put(o); set.put(o);
set.put(binding); set.put(binding);
bindings.put(c, set); bindings.put(c, set);

View file

@ -1,5 +1,5 @@
/******************************************************************************* /*******************************************************************************
* Copyright (c) 2011 Google, Inc and others. * Copyright (c) 2011, 2014 Google, Inc and others.
* All rights reserved. This program and the accompanying materials * All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0 * are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at * which accompanies this distribution, and is available at
@ -22,18 +22,18 @@ import org.eclipse.cdt.core.parser.util.CharArrayUtils;
* it back. * it back.
* *
* The map is encoded as: * The map is encoded as:
* <code>&lt;number_of_entries&gt;,&lt;key1&gt;&lt;value1&gt;...&lt;keyN&gt;&lt;valueN&gt;</code>. * {@code <number_of_entries>,<key1><value1>...<keyN><valueN>}.
* <p> * <p>
* Each string is encoded as: <code>&lt;number_of_characters&gt;,&lt;characters&gt;</code>. * Each string is encoded as: {@code <number_of_characters>,<characters>}.
* A <code>null</code> string is encoded as a single comma. * A {@code null} string is encoded as a single comma.
*/ */
public class SignificantMacros implements ISignificantMacros { public class SignificantMacros implements ISignificantMacros {
public static final char[] DEFINED = {0}; public static final char[] DEFINED = {0};
public static final char[] UNDEFINED = {1}; public static final char[] UNDEFINED = {1};
private static final Comparator<Object> SORTER = new Comparator<Object>() { private static final Comparator<char[]> SORTER = new Comparator<char[]>() {
@Override @Override
public int compare(Object o1, Object o2) { public int compare(char[] s1, char[] s2) {
return CharArrayUtils.compare((char[])o1, (char[])o2); return CharArrayUtils.compare(s1, s2);
} }
}; };
@ -51,10 +51,9 @@ public class SignificantMacros implements ISignificantMacros {
private char[] encode(CharArrayObjectMap<char[]> sigMacros) { private char[] encode(CharArrayObjectMap<char[]> sigMacros) {
StringBuilder buffer= new StringBuilder(); StringBuilder buffer= new StringBuilder();
Object[] keys= sigMacros.keyArray(); char[][] keys= sigMacros.keys();
Arrays.sort(keys, SORTER); Arrays.sort(keys, SORTER);
for (Object key : keys) { for (char[] name : keys) {
char[] name= (char[]) key;
char[] value= sigMacros.get(name); char[] value= sigMacros.get(name);
buffer.append((char) name.length).append(name); buffer.append((char) name.length).append(name);
buffer.append((char) value.length).append(value); buffer.append((char) value.length).append(value);