364 */
365 void newVar(VarSymbol sym) {
366 if (nextadr == vars.length) {
367 VarSymbol[] newvars = new VarSymbol[nextadr * 2];
368 System.arraycopy(vars, 0, newvars, 0, nextadr);
369 vars = newvars;
370 }
371 sym.adr = nextadr;
372 vars[nextadr] = sym;
373 inits.excl(nextadr);
374 uninits.incl(nextadr);
375 nextadr++;
376 }
377
378 /** Record an initialization of a trackable variable.
379 */
380 void letInit(DiagnosticPosition pos, VarSymbol sym) {
381 if (sym.adr >= firstadr && trackable(sym)) {
382 if ((sym.flags() & FINAL) != 0) {
383 if ((sym.flags() & PARAMETER) != 0) {
384 if ((sym.flags() & DISJUNCTION) != 0) { //multi-catch parameter
385 log.error(pos, "multicatch.parameter.may.not.be.assigned",
386 sym);
387 }
388 else {
389 log.error(pos, "final.parameter.may.not.be.assigned",
390 sym);
391 }
392 } else if (!uninits.isMember(sym.adr)) {
393 log.error(pos,
394 loopPassTwo
395 ? "var.might.be.assigned.in.loop"
396 : "var.might.already.be.assigned",
397 sym);
398 } else if (!inits.isMember(sym.adr)) {
399 // reachable assignment
400 uninits.excl(sym.adr);
401 uninitsTry.excl(sym.adr);
402 } else {
403 //log.rawWarning(pos, "unreachable assignment");//DEBUG
404 uninits.excl(sym.adr);
986 // where
987 /** Add any variables defined in stats to inits and uninits. */
988 private static void addVars(List<JCStatement> stats, Bits inits,
989 Bits uninits) {
990 for (;stats.nonEmpty(); stats = stats.tail) {
991 JCTree stat = stats.head;
992 if (stat.getTag() == JCTree.VARDEF) {
993 int adr = ((JCVariableDecl) stat).sym.adr;
994 inits.excl(adr);
995 uninits.incl(adr);
996 }
997 }
998 }
999
1000 public void visitTry(JCTry tree) {
1001 List<Type> caughtPrev = caught;
1002 List<Type> thrownPrev = thrown;
1003 thrown = List.nil();
1004 for (List<JCCatch> l = tree.catchers; l.nonEmpty(); l = l.tail) {
1005 List<JCExpression> subClauses = TreeInfo.isMultiCatch(l.head) ?
1006 ((JCTypeDisjunction)l.head.param.vartype).alternatives :
1007 List.of(l.head.param.vartype);
1008 for (JCExpression ct : subClauses) {
1009 caught = chk.incl(ct.type, caught);
1010 }
1011 }
1012 ListBuffer<JCVariableDecl> resourceVarDecls = ListBuffer.lb();
1013 Bits uninitsTryPrev = uninitsTry;
1014 ListBuffer<PendingExit> prevPendingExits = pendingExits;
1015 pendingExits = new ListBuffer<PendingExit>();
1016 Bits initsTry = inits.dup();
1017 uninitsTry = uninits.dup();
1018 for (JCTree resource : tree.resources) {
1019 if (resource instanceof JCVariableDecl) {
1020 JCVariableDecl vdecl = (JCVariableDecl) resource;
1021 visitVarDef(vdecl);
1022 unrefdResources.enter(vdecl.sym);
1023 resourceVarDecls.append(vdecl);
1024 } else if (resource instanceof JCExpression) {
1025 scanExpr((JCExpression) resource);
1026 } else {
1058 Bits initsEnd = inits;
1059 Bits uninitsEnd = uninits;
1060 int nextadrCatch = nextadr;
1061
1062 if (!resourceVarDecls.isEmpty() &&
1063 lint.isEnabled(Lint.LintCategory.TRY)) {
1064 for (JCVariableDecl resVar : resourceVarDecls) {
1065 if (unrefdResources.includes(resVar.sym)) {
1066 log.warning(Lint.LintCategory.TRY, resVar.pos(),
1067 "try.resource.not.referenced", resVar.sym);
1068 unrefdResources.remove(resVar.sym);
1069 }
1070 }
1071 }
1072
1073 List<Type> caughtInTry = List.nil();
1074 for (List<JCCatch> l = tree.catchers; l.nonEmpty(); l = l.tail) {
1075 alive = true;
1076 JCVariableDecl param = l.head.param;
1077 List<JCExpression> subClauses = TreeInfo.isMultiCatch(l.head) ?
1078 ((JCTypeDisjunction)l.head.param.vartype).alternatives :
1079 List.of(l.head.param.vartype);
1080 List<Type> ctypes = List.nil();
1081 List<Type> rethrownTypes = chk.diff(thrownInTry, caughtInTry);
1082 for (JCExpression ct : subClauses) {
1083 Type exc = ct.type;
1084 if (exc != syms.unknownType) {
1085 ctypes = ctypes.append(exc);
1086 if (types.isSameType(exc, syms.objectType))
1087 continue;
1088 checkCaughtType(l.head.pos(), exc, thrownInTry, caughtInTry);
1089 caughtInTry = chk.incl(exc, caughtInTry);
1090 }
1091 }
1092 inits = initsTry.dup();
1093 uninits = uninitsTry.dup();
1094 scan(param);
1095 inits.incl(param.sym.adr);
1096 uninits.excl(param.sym.adr);
1097 preciseRethrowTypes.put(param.sym, chk.intersect(ctypes, rethrownTypes));
1098 scanStat(l.head.body);
|
364 */
365 void newVar(VarSymbol sym) {
366 if (nextadr == vars.length) {
367 VarSymbol[] newvars = new VarSymbol[nextadr * 2];
368 System.arraycopy(vars, 0, newvars, 0, nextadr);
369 vars = newvars;
370 }
371 sym.adr = nextadr;
372 vars[nextadr] = sym;
373 inits.excl(nextadr);
374 uninits.incl(nextadr);
375 nextadr++;
376 }
377
378 /** Record an initialization of a trackable variable.
379 */
380 void letInit(DiagnosticPosition pos, VarSymbol sym) {
381 if (sym.adr >= firstadr && trackable(sym)) {
382 if ((sym.flags() & FINAL) != 0) {
383 if ((sym.flags() & PARAMETER) != 0) {
384 if ((sym.flags() & UNION) != 0) { //multi-catch parameter
385 log.error(pos, "multicatch.parameter.may.not.be.assigned",
386 sym);
387 }
388 else {
389 log.error(pos, "final.parameter.may.not.be.assigned",
390 sym);
391 }
392 } else if (!uninits.isMember(sym.adr)) {
393 log.error(pos,
394 loopPassTwo
395 ? "var.might.be.assigned.in.loop"
396 : "var.might.already.be.assigned",
397 sym);
398 } else if (!inits.isMember(sym.adr)) {
399 // reachable assignment
400 uninits.excl(sym.adr);
401 uninitsTry.excl(sym.adr);
402 } else {
403 //log.rawWarning(pos, "unreachable assignment");//DEBUG
404 uninits.excl(sym.adr);
986 // where
987 /** Add any variables defined in stats to inits and uninits. */
988 private static void addVars(List<JCStatement> stats, Bits inits,
989 Bits uninits) {
990 for (;stats.nonEmpty(); stats = stats.tail) {
991 JCTree stat = stats.head;
992 if (stat.getTag() == JCTree.VARDEF) {
993 int adr = ((JCVariableDecl) stat).sym.adr;
994 inits.excl(adr);
995 uninits.incl(adr);
996 }
997 }
998 }
999
1000 public void visitTry(JCTry tree) {
1001 List<Type> caughtPrev = caught;
1002 List<Type> thrownPrev = thrown;
1003 thrown = List.nil();
1004 for (List<JCCatch> l = tree.catchers; l.nonEmpty(); l = l.tail) {
1005 List<JCExpression> subClauses = TreeInfo.isMultiCatch(l.head) ?
1006 ((JCTypeUnion)l.head.param.vartype).alternatives :
1007 List.of(l.head.param.vartype);
1008 for (JCExpression ct : subClauses) {
1009 caught = chk.incl(ct.type, caught);
1010 }
1011 }
1012 ListBuffer<JCVariableDecl> resourceVarDecls = ListBuffer.lb();
1013 Bits uninitsTryPrev = uninitsTry;
1014 ListBuffer<PendingExit> prevPendingExits = pendingExits;
1015 pendingExits = new ListBuffer<PendingExit>();
1016 Bits initsTry = inits.dup();
1017 uninitsTry = uninits.dup();
1018 for (JCTree resource : tree.resources) {
1019 if (resource instanceof JCVariableDecl) {
1020 JCVariableDecl vdecl = (JCVariableDecl) resource;
1021 visitVarDef(vdecl);
1022 unrefdResources.enter(vdecl.sym);
1023 resourceVarDecls.append(vdecl);
1024 } else if (resource instanceof JCExpression) {
1025 scanExpr((JCExpression) resource);
1026 } else {
1058 Bits initsEnd = inits;
1059 Bits uninitsEnd = uninits;
1060 int nextadrCatch = nextadr;
1061
1062 if (!resourceVarDecls.isEmpty() &&
1063 lint.isEnabled(Lint.LintCategory.TRY)) {
1064 for (JCVariableDecl resVar : resourceVarDecls) {
1065 if (unrefdResources.includes(resVar.sym)) {
1066 log.warning(Lint.LintCategory.TRY, resVar.pos(),
1067 "try.resource.not.referenced", resVar.sym);
1068 unrefdResources.remove(resVar.sym);
1069 }
1070 }
1071 }
1072
1073 List<Type> caughtInTry = List.nil();
1074 for (List<JCCatch> l = tree.catchers; l.nonEmpty(); l = l.tail) {
1075 alive = true;
1076 JCVariableDecl param = l.head.param;
1077 List<JCExpression> subClauses = TreeInfo.isMultiCatch(l.head) ?
1078 ((JCTypeUnion)l.head.param.vartype).alternatives :
1079 List.of(l.head.param.vartype);
1080 List<Type> ctypes = List.nil();
1081 List<Type> rethrownTypes = chk.diff(thrownInTry, caughtInTry);
1082 for (JCExpression ct : subClauses) {
1083 Type exc = ct.type;
1084 if (exc != syms.unknownType) {
1085 ctypes = ctypes.append(exc);
1086 if (types.isSameType(exc, syms.objectType))
1087 continue;
1088 checkCaughtType(l.head.pos(), exc, thrownInTry, caughtInTry);
1089 caughtInTry = chk.incl(exc, caughtInTry);
1090 }
1091 }
1092 inits = initsTry.dup();
1093 uninits = uninitsTry.dup();
1094 scan(param);
1095 inits.incl(param.sym.adr);
1096 uninits.excl(param.sym.adr);
1097 preciseRethrowTypes.put(param.sym, chk.intersect(ctypes, rethrownTypes));
1098 scanStat(l.head.body);
|