private Class<?> jitArrayCreationExpression(ArrayCreationExpression exp) { createArray(exp.getComponentType(), exp.items.size()); for (int i = 0; i < exp.items.size(); i++) { mv.visitInsn(DUP); mv.visitLdcInsn(i); jitExpression(exp.items.get(i)); mv.visitInsn(getCodeForType(exp.getComponentType(), IASTORE)); } return exp.getType(); }
/** array('foo' => 'bar', 'foo1' => 'bar', 1 => 'foo') */ public static HashMap<String, String> getArrayKeyValueMap( @NotNull ArrayCreationExpression arrayCreationExpression) { HashMap<String, String> keys = new HashMap<String, String>(); for (ArrayHashElement arrayHashElement : arrayCreationExpression.getHashElements()) { PhpPsiElement child = arrayHashElement.getKey(); if (child != null && ((child instanceof StringLiteralExpression) || PhpPatterns.psiElement(PhpElementTypes.NUMBER).accepts(child))) { String key; if (child instanceof StringLiteralExpression) { key = ((StringLiteralExpression) child).getContents(); } else { key = child.getText(); } if (StringUtils.isBlank(key)) { continue; } String value = null; if (arrayHashElement.getValue() instanceof StringLiteralExpression) { value = ((StringLiteralExpression) arrayHashElement.getValue()).getContents(); } keys.put(key, value); } } return keys; }
private static List createVBArrayCreationDeclatationsAndSubstituteParams( List oldParameters, List newParameters, InterpretationContext context) { List arrayCreationDeclarations = new ArrayList(); String s = ACE_VARIABLE_NAME; for (Iterator itr = oldParameters.iterator(); itr.hasNext(); ) { Object o = itr.next(); if (o instanceof ArrayCreationExpression) { ArrayCreationExpression ace = (ArrayCreationExpression) o; LocalVariableDeclaration ds = LocalVariableDeclaration.createVBLocalVariableDeclaration(context, ace, s + aceCounter); arrayCreationDeclarations.add(ds); newParameters.add(DNVariable.createVBVariable(s + aceCounter, ace.getName() + "()")); aceCounter++; } else { newParameters.add(o); } } return arrayCreationDeclarations; }
/** * Gets array key-value as single PsiElement map * * <p>['foo' => $bar] */ @NotNull public static Map<String, PsiElement> getArrayCreationKeyMap( @NotNull ArrayCreationExpression arrayCreationExpression) { Map<String, PsiElement> keys = new HashMap<String, PsiElement>(); for (ArrayHashElement arrayHashElement : arrayCreationExpression.getHashElements()) { PhpPsiElement child = arrayHashElement.getKey(); if (child instanceof StringLiteralExpression) { keys.put(((StringLiteralExpression) child).getContents(), child); } } return keys; }
@Nullable public static PhpPsiElement getArrayValue( ArrayCreationExpression arrayCreationExpression, String name) { for (ArrayHashElement arrayHashElement : arrayCreationExpression.getHashElements()) { PhpPsiElement child = arrayHashElement.getKey(); if (child instanceof StringLiteralExpression) { if (((StringLiteralExpression) child).getContents().equals(name)) { return arrayHashElement.getValue(); } } } return null; }
@Override public Void visitInvocationExpression(final InvocationExpression node, final Void data) { super.visitInvocationExpression(node, data); final AstNodeCollection<Expression> arguments = node.getArguments(); final Expression lastArgument = arguments.lastOrNullObject(); Expression arrayArg = lastArgument; if (arrayArg instanceof CastExpression) arrayArg = ((CastExpression) arrayArg).getExpression(); if (arrayArg == null || arrayArg.isNull() || !(arrayArg instanceof ArrayCreationExpression && node.getTarget() instanceof MemberReferenceExpression)) { return null; } final ArrayCreationExpression newArray = (ArrayCreationExpression) arrayArg; final MemberReferenceExpression target = (MemberReferenceExpression) node.getTarget(); if (!newArray.getAdditionalArraySpecifiers().hasSingleElement()) { return null; } final MethodReference method = (MethodReference) node.getUserData(Keys.MEMBER_REFERENCE); if (method == null) { return null; } final MethodDefinition resolved = method.resolve(); if (resolved == null || !resolved.isVarArgs()) { return null; } final List<MethodReference> candidates; final Expression invocationTarget = target.getTarget(); if (invocationTarget == null || invocationTarget.isNull()) { candidates = MetadataHelper.findMethods( context.getCurrentType(), MetadataFilters.matchName(resolved.getName())); } else { final ResolveResult targetResult = _resolver.apply(invocationTarget); if (targetResult == null || targetResult.getType() == null) { return null; } candidates = MetadataHelper.findMethods( targetResult.getType(), MetadataFilters.matchName(resolved.getName())); } final List<TypeReference> argTypes = new ArrayList<>(); for (final Expression argument : arguments) { final ResolveResult argResult = _resolver.apply(argument); if (argResult == null || argResult.getType() == null) { return null; } argTypes.add(argResult.getType()); } final MethodBinder.BindResult c1 = MethodBinder.selectMethod(candidates, argTypes); if (c1.isFailure() || c1.isAmbiguous()) { return null; } argTypes.remove(argTypes.size() - 1); final ArrayInitializerExpression initializer = newArray.getInitializer(); final boolean hasElements = !initializer.isNull() && !initializer.getElements().isEmpty(); if (hasElements) { for (final Expression argument : initializer.getElements()) { final ResolveResult argResult = _resolver.apply(argument); if (argResult == null || argResult.getType() == null) { return null; } argTypes.add(argResult.getType()); } } final MethodBinder.BindResult c2 = MethodBinder.selectMethod(candidates, argTypes); if (c2.isFailure() || c2.isAmbiguous() || !StringUtilities.equals( c2.getMethod().getErasedSignature(), c1.getMethod().getErasedSignature())) { return null; } lastArgument.remove(); if (!hasElements) { return null; } for (final Expression newArg : initializer.getElements()) { newArg.remove(); arguments.add(newArg); } return null; }