use of java.util.BitSet in project intellij-community by JetBrains.
the class GroovyInlineLocalHandler method createSettings.
/**
* Returns Settings object for referenced definition in case of local variable
*/
@Nullable
private static InlineLocalVarSettings createSettings(final GrVariable variable, Editor editor, boolean invokedOnReference) {
final String localName = variable.getName();
final Project project = variable.getProject();
GrExpression initializer = null;
Instruction writeInstr = null;
Instruction[] flow = null;
//search for initializer to inline
if (invokedOnReference) {
LOG.assertTrue(editor != null, "null editor but invokedOnReference==true");
final PsiReference ref = TargetElementUtil.findReference(editor);
LOG.assertTrue(ref != null);
PsiElement cur = ref.getElement();
if (cur instanceof GrReferenceExpression) {
GrControlFlowOwner controlFlowOwner;
do {
controlFlowOwner = ControlFlowUtils.findControlFlowOwner(cur);
if (controlFlowOwner == null)
break;
flow = controlFlowOwner.getControlFlow();
final List<BitSet> writes = ControlFlowUtils.inferWriteAccessMap(flow, variable);
final PsiElement finalCur = cur;
Instruction instruction = ControlFlowUtils.findInstruction(finalCur, flow);
LOG.assertTrue(instruction != null);
final BitSet prev = writes.get(instruction.num());
if (prev.cardinality() == 1) {
writeInstr = flow[prev.nextSetBit(0)];
final PsiElement element = writeInstr.getElement();
if (element instanceof GrVariable) {
initializer = ((GrVariable) element).getInitializerGroovy();
} else if (element instanceof GrReferenceExpression) {
initializer = TypeInferenceHelper.getInitializerFor((GrReferenceExpression) element);
}
}
PsiElement old_cur = cur;
if (controlFlowOwner instanceof GrClosableBlock) {
cur = controlFlowOwner;
} else {
PsiElement parent = controlFlowOwner.getParent();
if (parent instanceof GrMember)
cur = ((GrMember) parent).getContainingClass();
}
if (cur == old_cur)
break;
} while (initializer == null);
}
} else {
flow = ControlFlowUtils.findControlFlowOwner(variable).getControlFlow();
initializer = variable.getInitializerGroovy();
writeInstr = ContainerUtil.find(flow, instruction -> instruction.getElement() == variable);
}
if (initializer == null || writeInstr == null) {
String message = GroovyRefactoringBundle.message("cannot.find.a.single.definition.to.inline.local.var");
CommonRefactoringUtil.showErrorHint(variable.getProject(), editor, message, INLINE_VARIABLE, HelpID.INLINE_VARIABLE);
return null;
}
int writeInstructionNumber = writeInstr.num();
if (ApplicationManager.getApplication().isUnitTestMode()) {
return new InlineLocalVarSettings(initializer, writeInstructionNumber, flow);
}
final String question = GroovyRefactoringBundle.message("inline.local.variable.prompt.0.1", localName);
RefactoringMessageDialog dialog = new RefactoringMessageDialog(INLINE_VARIABLE, question, HelpID.INLINE_VARIABLE, "OptionPane.questionIcon", true, project);
if (dialog.showAndGet()) {
return new InlineLocalVarSettings(initializer, writeInstructionNumber, flow);
}
return null;
}
use of java.util.BitSet in project intellij-community by JetBrains.
the class PyFileElementType method readBitSet.
private static BitSet readBitSet(StubInputStream dataStream) throws IOException {
// NOTE: here we assume that bitset has no more than 32 bits so that the value fits into an int.
// see PyFileStubImpl: we assume that all bits fit into an int
BitSet ret = new BitSet(32);
int bits = dataStream.readInt();
for (int i = 0; i < 32; i += 1) {
boolean bit = (bits & (1 << i)) != 0;
ret.set(i, bit);
}
return ret;
}
use of java.util.BitSet in project intellij-community by JetBrains.
the class PyFileElementType method deserialize.
@NotNull
@Override
public PyFileStub deserialize(@NotNull StubInputStream dataStream, StubElement parentStub) throws IOException {
List<String> all = readNullableList(dataStream);
BitSet future_features = readBitSet(dataStream);
StringRef deprecationMessage = dataStream.readName();
return new PyFileStubImpl(all, future_features, deprecationMessage);
}
use of java.util.BitSet in project intellij-community by JetBrains.
the class GrIntroduceClosureParameterProcessor method findUsagesForLocal.
private static Collection<PsiReference> findUsagesForLocal(GrClosableBlock initializer, final GrVariable var) {
final Instruction[] flow = ControlFlowUtils.findControlFlowOwner(initializer).getControlFlow();
final List<BitSet> writes = ControlFlowUtils.inferWriteAccessMap(flow, var);
Instruction writeInstr = null;
final PsiElement parent = initializer.getParent();
if (parent instanceof GrVariable) {
writeInstr = ContainerUtil.find(flow, instruction -> instruction.getElement() == var);
} else if (parent instanceof GrAssignmentExpression) {
final GrReferenceExpression refExpr = (GrReferenceExpression) ((GrAssignmentExpression) parent).getLValue();
final Instruction instruction = ContainerUtil.find(flow, instruction1 -> instruction1.getElement() == refExpr);
LOG.assertTrue(instruction != null);
final BitSet prev = writes.get(instruction.num());
if (prev.cardinality() == 1) {
writeInstr = flow[prev.nextSetBit(0)];
}
}
LOG.assertTrue(writeInstr != null);
Collection<PsiReference> result = new ArrayList<>();
for (Instruction instruction : flow) {
if (!(instruction instanceof ReadWriteVariableInstruction))
continue;
if (((ReadWriteVariableInstruction) instruction).isWrite())
continue;
final PsiElement element = instruction.getElement();
if (element instanceof GrVariable && element != var)
continue;
if (!(element instanceof GrReferenceExpression))
continue;
final GrReferenceExpression ref = (GrReferenceExpression) element;
if (ref.isQualified() || ref.resolve() != var)
continue;
final BitSet prev = writes.get(instruction.num());
if (prev.cardinality() == 1 && prev.get(writeInstr.num())) {
result.add(ref);
}
}
return result;
}
use of java.util.BitSet in project smali by JesusFreke.
the class PostInstructionRegisterInfoMethodItem method writeTo.
@Override
public boolean writeTo(IndentingWriter writer) throws IOException {
int registerInfo = registerFormatter.options.registerInfo;
int registerCount = analyzedInstruction.getRegisterCount();
BitSet registers = new BitSet(registerCount);
if ((registerInfo & BaksmaliOptions.ALL) != 0) {
registers.set(0, registerCount);
} else {
if ((registerInfo & BaksmaliOptions.ALLPOST) != 0) {
registers.set(0, registerCount);
} else if ((registerInfo & BaksmaliOptions.DEST) != 0) {
addDestRegs(registers, registerCount);
}
}
return writeRegisterInfo(writer, registers);
}
Aggregations