2009-01-09 02:52:56 +00:00
|
|
|
/******************************************************************************
|
|
|
|
* Copyright (c) 2002 - 2006 IBM Corporation.
|
|
|
|
* 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 Sep 28, 2005
|
|
|
|
*/
|
|
|
|
package com.ibm.wala.cast.java.translator.polyglot;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Collection;
|
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.Iterator;
|
|
|
|
|
|
|
|
import polyglot.types.ArrayType;
|
|
|
|
import polyglot.types.ClassType;
|
2011-04-27 14:55:30 +00:00
|
|
|
import polyglot.types.ObjectType;
|
2009-01-09 02:52:56 +00:00
|
|
|
import polyglot.types.PrimitiveType;
|
|
|
|
import polyglot.types.Type;
|
|
|
|
import polyglot.types.TypeSystem;
|
|
|
|
|
|
|
|
import com.ibm.wala.cast.java.types.JavaPrimitiveTypeMap;
|
|
|
|
import com.ibm.wala.cast.tree.CAstType;
|
|
|
|
import com.ibm.wala.cast.tree.impl.CAstTypeDictionaryImpl;
|
|
|
|
import com.ibm.wala.util.debug.Assertions;
|
|
|
|
|
|
|
|
public class PolyglotTypeDictionary extends CAstTypeDictionaryImpl {
|
|
|
|
private final class PolyglotJavaArrayType implements CAstType.Array {
|
|
|
|
private final Type fEltPolyglotType;
|
|
|
|
|
|
|
|
private final CAstType fEltCAstType;
|
|
|
|
|
|
|
|
private PolyglotJavaArrayType(ArrayType arrayType) {
|
|
|
|
super();
|
|
|
|
fEltPolyglotType = arrayType.base();
|
|
|
|
fEltCAstType = getCAstTypeFor(fEltPolyglotType);
|
|
|
|
}
|
|
|
|
|
2013-06-25 15:57:37 +00:00
|
|
|
@Override
|
2009-01-09 02:52:56 +00:00
|
|
|
public int getNumDimensions() {
|
|
|
|
return 1; // always 1 for Java
|
|
|
|
}
|
|
|
|
|
2013-06-25 15:57:37 +00:00
|
|
|
@Override
|
2009-01-09 02:52:56 +00:00
|
|
|
public CAstType getElementType() {
|
|
|
|
return fEltCAstType;
|
|
|
|
}
|
|
|
|
|
2013-06-25 15:57:37 +00:00
|
|
|
@Override
|
2009-01-09 02:52:56 +00:00
|
|
|
public String getName() {
|
|
|
|
return "[" + fEltCAstType.getName();
|
|
|
|
}
|
|
|
|
|
2013-06-25 15:57:37 +00:00
|
|
|
@Override
|
2011-04-27 16:46:41 +00:00
|
|
|
public Collection<CAstType> getSupertypes() {
|
2009-01-09 02:52:56 +00:00
|
|
|
if (fEltPolyglotType.isPrimitive())
|
|
|
|
return Collections.singleton(getCAstTypeFor(fTypeSystem.Object()));
|
2011-04-27 16:20:21 +00:00
|
|
|
Assertions.productionAssertion(fEltPolyglotType.isReference(), "Non-primitive, non-reference array element type!");
|
|
|
|
ObjectType baseRefType = (ObjectType) fEltPolyglotType;
|
2009-01-09 02:52:56 +00:00
|
|
|
Collection<CAstType> supers = new ArrayList<CAstType>();
|
2011-04-27 16:46:41 +00:00
|
|
|
for (Iterator<Type> superIter = baseRefType.interfaces().iterator(); superIter.hasNext(); ) {
|
2009-01-09 02:52:56 +00:00
|
|
|
supers.add(getCAstTypeFor(superIter.next()));
|
|
|
|
}
|
2011-04-27 14:55:30 +00:00
|
|
|
if (baseRefType instanceof ClassType) {
|
|
|
|
ClassType baseClassType = (ClassType) baseRefType;
|
|
|
|
if (baseClassType.superClass() != null)
|
|
|
|
supers.add(getCAstTypeFor(baseRefType.superClass()));
|
|
|
|
}
|
2009-01-09 02:52:56 +00:00
|
|
|
return supers;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected final TypeSystem fTypeSystem;
|
|
|
|
|
2011-04-27 14:55:30 +00:00
|
|
|
protected final PolyglotJava2CAstTranslator fTranslator;
|
2009-01-09 02:52:56 +00:00
|
|
|
|
|
|
|
public PolyglotTypeDictionary(TypeSystem typeSystem, PolyglotJava2CAstTranslator translator) {
|
|
|
|
fTypeSystem = typeSystem;
|
|
|
|
fTranslator = translator;
|
|
|
|
}
|
|
|
|
|
2013-06-25 15:57:37 +00:00
|
|
|
@Override
|
2009-01-09 02:52:56 +00:00
|
|
|
public CAstType getCAstTypeFor(Object astType) {
|
|
|
|
CAstType type = super.getCAstTypeFor(astType);
|
|
|
|
// Handle the case where we haven't seen an AST decl for some type before
|
|
|
|
// processing a reference. This can certainly happen with classes in byte-
|
|
|
|
// code libraries, for which we never see an AST decl.
|
|
|
|
// In this case, just create a new type and return that.
|
|
|
|
if (type == null) {
|
|
|
|
final Type polyglotType = (Type) astType;
|
|
|
|
|
|
|
|
if (polyglotType.isClass())
|
|
|
|
type = fTranslator.new PolyglotJavaType((ClassType) astType, this, fTypeSystem);
|
|
|
|
else if (polyglotType.isPrimitive()) {
|
2011-04-27 14:55:30 +00:00
|
|
|
type = JavaPrimitiveTypeMap.lookupType(((PrimitiveType) polyglotType).name().toString());
|
2009-01-09 02:52:56 +00:00
|
|
|
} else if (polyglotType.isArray()) {
|
|
|
|
type = new PolyglotJavaArrayType((ArrayType) polyglotType);
|
|
|
|
} else
|
|
|
|
Assertions.UNREACHABLE("getCAstTypeFor() passed type that is not primitive, array, or class?");
|
|
|
|
super.map(astType, type);
|
|
|
|
}
|
|
|
|
return type;
|
|
|
|
}
|
|
|
|
}
|