use of com.sun.tools.javac.code.Type in project lombok by rzwitserloot.
the class JavacResolution method typeToJCTree.
private static JCExpression typeToJCTree(Type type, JavacAST ast, boolean allowCompound, boolean allowVoid) throws TypeNotConvertibleException {
int dims = 0;
Type type0 = type;
while (type0 instanceof ArrayType) {
dims++;
type0 = ((ArrayType) type0).elemtype;
}
JCExpression result = typeToJCTree0(type0, ast, allowCompound, allowVoid);
while (dims > 0) {
result = ast.getTreeMaker().TypeArray(result);
dims--;
}
return result;
}
use of com.sun.tools.javac.code.Type in project ceylon-compiler by ceylon.
the class Attr method attribLazyConstantValue.
/**
* Attribute a "lazy constant value".
* @param env The env for the const value
* @param initializer The initializer for the const value
* @param type The expected type, or null
* @see VarSymbol#setlazyConstValue
*/
public Object attribLazyConstantValue(Env<AttrContext> env, JCTree.JCExpression initializer, Type type) {
// in case no lint value has been set up for this env, scan up
// env stack looking for smallest enclosing env for which it is set.
Env<AttrContext> lintEnv = env;
while (lintEnv.info.lint == null) lintEnv = lintEnv.next;
// Having found the enclosing lint value, we can initialize the lint value for this class
env.info.lint = lintEnv.info.lint.augment(env.info.enclVar.attributes_field, env.info.enclVar.flags());
Lint prevLint = chk.setLint(env.info.lint);
JavaFileObject prevSource = log.useSource(env.toplevel.sourcefile);
try {
Type itype = attribExpr(initializer, env, type);
if (itype.constValue() != null)
return coerce(itype, type).constValue();
else
return null;
} finally {
env.info.lint = prevLint;
log.useSource(prevSource);
}
}
use of com.sun.tools.javac.code.Type in project ceylon-compiler by ceylon.
the class Attr method visitUnary.
public void visitUnary(JCUnary tree) {
// Attribute arguments.
Type argtype = (JCTree.PREINC <= tree.getTag() && tree.getTag() <= JCTree.POSTDEC) ? attribTree(tree.arg, env, VAR, Type.noType) : chk.checkNonVoid(tree.arg.pos(), attribExpr(tree.arg, env));
// Find operator.
Symbol operator = tree.operator = rs.resolveUnaryOperator(tree.pos(), tree.getTag(), env, argtype);
Type owntype = types.createErrorType(tree.type);
if (operator.kind == MTH && !argtype.isErroneous()) {
owntype = (JCTree.PREINC <= tree.getTag() && tree.getTag() <= JCTree.POSTDEC) ? tree.arg.type : operator.type.getReturnType();
int opc = ((OperatorSymbol) operator).opcode;
// If the argument is constant, fold it.
if (argtype.constValue() != null) {
Type ctype = cfolder.fold1(opc, argtype);
if (ctype != null) {
owntype = cfolder.coerce(ctype, owntype);
// operands are constant identifiers.
if (tree.arg.type.tsym == syms.stringType.tsym) {
tree.arg.type = syms.stringType;
}
}
}
}
result = check(tree, owntype, VAL, pkind, pt);
}
use of com.sun.tools.javac.code.Type in project ceylon-compiler by ceylon.
the class Attr method visitTypeUnion.
public void visitTypeUnion(JCTypeUnion tree) {
ListBuffer<Type> multicatchTypes = ListBuffer.lb();
// lazy, only if needed
ListBuffer<Type> all_multicatchTypes = null;
for (JCExpression typeTree : tree.alternatives) {
Type ctype = attribType(typeTree, env);
ctype = chk.checkType(typeTree.pos(), chk.checkClassType(typeTree.pos(), ctype), syms.throwableType);
if (!ctype.isErroneous()) {
//unrelated w.r.t. subtyping
if (chk.intersects(ctype, multicatchTypes.toList())) {
for (Type t : multicatchTypes) {
boolean sub = types.isSubtype(ctype, t);
boolean sup = types.isSubtype(t, ctype);
if (sub || sup) {
//assume 'a' <: 'b'
Type a = sub ? ctype : t;
Type b = sub ? t : ctype;
log.error(typeTree.pos(), "multicatch.types.must.be.disjoint", a, b);
}
}
}
multicatchTypes.append(ctype);
if (all_multicatchTypes != null)
all_multicatchTypes.append(ctype);
} else {
if (all_multicatchTypes == null) {
all_multicatchTypes = ListBuffer.lb();
all_multicatchTypes.appendList(multicatchTypes);
}
all_multicatchTypes.append(ctype);
}
}
Type t = check(tree, types.lub(multicatchTypes.toList()), TYP, pkind, pt);
if (t.tag == CLASS) {
List<Type> alternatives = ((all_multicatchTypes == null) ? multicatchTypes : all_multicatchTypes).toList();
t = new UnionClassType((ClassType) t, alternatives);
}
tree.type = result = t;
}
use of com.sun.tools.javac.code.Type in project ceylon-compiler by ceylon.
the class Attr method visitSwitch.
public void visitSwitch(JCSwitch tree) {
Type seltype = attribExpr(tree.selector, env);
Env<AttrContext> switchEnv = env.dup(tree, env.info.dup(env.info.scope.dup()));
boolean enumSwitch = allowEnums && (seltype.tsym.flags() & Flags.ENUM) != 0;
boolean stringSwitch = false;
if (types.isSameType(seltype, syms.stringType)) {
if (allowStringsInSwitch) {
stringSwitch = true;
} else {
log.error(tree.selector.pos(), "string.switch.not.supported.in.source", sourceName);
}
}
if (!enumSwitch && !stringSwitch)
seltype = chk.checkType(tree.selector.pos(), seltype, syms.intType);
// Attribute all cases and
// check that there are no duplicate case labels or default clauses.
// The set of case labels.
Set<Object> labels = new HashSet<Object>();
// Is there a default label?
boolean hasDefault = false;
for (List<JCCase> l = tree.cases; l.nonEmpty(); l = l.tail) {
JCCase c = l.head;
Env<AttrContext> caseEnv = switchEnv.dup(c, env.info.dup(switchEnv.info.scope.dup()));
if (c.pat != null) {
if (enumSwitch) {
Symbol sym = enumConstant(c.pat, seltype);
if (sym == null) {
log.error(c.pat.pos(), "enum.label.must.be.unqualified.enum");
} else if (!labels.add(sym)) {
log.error(c.pos(), "duplicate.case.label");
}
} else {
Type pattype = attribExpr(c.pat, switchEnv, seltype);
if (pattype.tag != ERROR) {
if (pattype.constValue() == null) {
log.error(c.pat.pos(), (stringSwitch ? "string.const.req" : "const.expr.req"));
} else if (labels.contains(pattype.constValue())) {
log.error(c.pos(), "duplicate.case.label");
} else {
labels.add(pattype.constValue());
}
}
}
} else if (hasDefault) {
log.error(c.pos(), "duplicate.default.label");
} else {
hasDefault = true;
}
attribStats(c.stats, caseEnv);
caseEnv.info.scope.leave();
addVars(c.stats, switchEnv.info.scope);
}
switchEnv.info.scope.leave();
result = null;
}
Aggregations