public boolean endvisit(TypeDeclaration type) throws Exception { if (type instanceof NamespaceDeclaration) { NamespaceDeclaration namespaceDecl = (NamespaceDeclaration) type; while (deferredNamespacedDeclarations != null && !deferredNamespacedDeclarations.isEmpty()) { final ASTNode[] declarations = deferredNamespacedDeclarations.toArray( new ASTNode[deferredNamespacedDeclarations.size()]); deferredNamespacedDeclarations.clear(); for (ASTNode deferred : declarations) { deferred.traverse(this); } } fLastNamespace = null; // there are no nested namespaces if (namespaceDecl.isGlobal()) { return true; } } declarations.pop(); // resolve more type member declarations resolveMagicMembers(type); for (PHPSourceElementRequestorExtension visitor : extensions) { visitor.endvisit(type); } return super.endvisit(type); }
public boolean visit(TypeDeclaration type) throws Exception { if (type instanceof NamespaceDeclaration) { NamespaceDeclaration namespaceDecl = (NamespaceDeclaration) type; fLastNamespace = namespaceDecl; fLastUseParts.clear(); if (namespaceDecl.isGlobal()) { return true; } } type.setModifiers(markAsDeprecated(type.getModifiers(), type)); // In case we are entering a nested element if (!declarations.empty() && declarations.peek() instanceof MethodDeclaration) { if (fLastNamespace == null) { deferredDeclarations.add(type); } else { deferredNamespacedDeclarations.add(type); } return false; } declarations.push(type); for (PHPSourceElementRequestorExtension visitor : extensions) { visitor.visit(type); } return super.visit(type); }
public boolean endvisit(TypeDeclaration type) throws Exception { if (type instanceof NamespaceDeclaration) { NamespaceDeclaration namespaceDecl = (NamespaceDeclaration) type; while (deferredNamespacedDeclarations != null && !deferredNamespacedDeclarations.isEmpty()) { final ASTNode[] declarations = deferredNamespacedDeclarations.toArray( new ASTNode[deferredNamespacedDeclarations.size()]); deferredNamespacedDeclarations.clear(); for (ASTNode deferred : declarations) { deferred.traverse(this); } } fCurrentNamespace = null; // there are no nested namespaces fCurrentQualifier = null; fLastUseParts.clear(); if (namespaceDecl.isGlobal()) { return visitGeneral(type); } } else { fCurrentParent = null; } declarations.pop(); // resolve more type member declarations resolveMagicMembers(type); for (PhpIndexingVisitorExtension visitor : extensions) { visitor.endvisit(type); } endvisitGeneral(type); return true; }
public boolean visit(MethodDeclaration method) throws Exception { methodGlobalVars.add(new HashSet<String>()); ASTNode parentDeclaration = null; if (!declarations.empty()) { parentDeclaration = declarations.peek(); } // In case we are entering a nested element - just add to the deferred // list // and get out of the nested element visiting process if (parentDeclaration instanceof MethodDeclaration) { if (fLastNamespace == null) { deferredDeclarations.add(method); } else { deferredNamespacedDeclarations.add(method); } return false; } if (parentDeclaration instanceof InterfaceDeclaration) { method.setModifier(Modifiers.AccAbstract); } method.setModifiers(markAsDeprecated(method.getModifiers(), method)); declarations.push(method); for (PHPSourceElementRequestorExtension visitor : extensions) { visitor.visit(method); } boolean visit = visitMethodDeclaration(method); if (visit) { // Process method argument (local variable) declarations: List<Argument> arguments = method.getArguments(); for (Argument arg : arguments) { ISourceElementRequestor.FieldInfo info = new ISourceElementRequestor.FieldInfo(); info.name = arg.getName(); info.modifiers = Modifiers.AccPublic; info.nameSourceStart = arg.getNameStart(); info.nameSourceEnd = arg.getNameEnd() - 1; info.declarationStart = arg.sourceStart(); fRequestor.enterField(info); fRequestor.exitField(arg.sourceEnd() - 1); } } return visit; }
public boolean visit(CallExpression call) throws Exception { FieldDeclaration constantDecl = ASTUtils.getConstantDeclaration(call); if (constantDecl != null) { // In case we are entering a nested element if (!declarations.empty() && declarations.peek() instanceof MethodDeclaration) { deferredDeclarations.add(constantDecl); return visitGeneral(call); } visit((FieldDeclaration) constantDecl); } else { int argsCount = 0; CallArgumentsList args = call.getArgs(); if (args != null && args.getChilds() != null) { argsCount = args.getChilds().size(); } modifyReference( call, new ReferenceInfo( IModelElement.METHOD, call.sourceStart(), call.sourceEnd() - call.sourceStart(), call.getName(), Integer.toString(argsCount), null)); } return visitGeneral(call); }
public boolean visit(CallExpression call) throws Exception { FieldDeclaration constantDecl = ASTUtils.getConstantDeclaration(call); if (constantDecl != null) { // In case we are entering a nested element if (!declarations.empty() && declarations.peek() instanceof MethodDeclaration) { deferredDeclarations.add(constantDecl); return false; } visit((FieldDeclaration) constantDecl); } else { int argsCount = 0; CallArgumentsList args = call.getArgs(); if (args != null && args.getChilds() != null) { argsCount = args.getChilds().size(); } fRequestor.acceptMethodReference( call.getName(), argsCount, call.getCallName().sourceStart(), call.getCallName().sourceEnd()); } return true; }
public boolean endvisit(ModuleDeclaration declaration) throws Exception { for (PHPSourceElementRequestorExtension visitor : extensions) { visitor.endvisit(declaration); } while (deferredDeclarations != null && !deferredDeclarations.isEmpty()) { final ASTNode[] declarations = deferredDeclarations.toArray(new ASTNode[deferredDeclarations.size()]); deferredDeclarations.clear(); for (ASTNode deferred : declarations) { deferred.traverse(this); } } fLastUseParts.clear(); return super.endvisit(declaration); }
public boolean visit(Assignment assignment) throws Exception { Expression left = assignment.getVariable(); if (left instanceof FieldAccess) { // class variable ($this->a = .) FieldAccess fieldAccess = (FieldAccess) left; Expression dispatcher = fieldAccess.getDispatcher(); if (dispatcher instanceof VariableReference && "$this" //$NON-NLS-1$ .equals(((VariableReference) dispatcher).getName())) { Expression field = fieldAccess.getField(); if (field instanceof SimpleReference) { SimpleReference ref = (SimpleReference) field; ISourceElementRequestor.FieldInfo info = new ISourceElementRequestor.FieldInfo(); info.modifiers = Modifiers.AccPublic; info.name = '$' + ref.getName(); info.nameSourceEnd = ref.sourceEnd() - 1; info.nameSourceStart = ref.sourceStart(); info.declarationStart = assignment.sourceStart(); fInfoStack.push(info); fRequestor.enterField(info); fNodes.push(assignment); } } } else if (left instanceof VariableReference && !(left instanceof ArrayVariableReference)) { if (!declarations.empty()) { ASTNode parentDeclaration = declarations.peek(); if (parentDeclaration instanceof MethodDeclaration && methodGlobalVars.peek().contains(((VariableReference) left).getName()) || parentDeclaration == fLastNamespace) { deferredDeclarations.add(assignment); return false; } } if (!fInfoStack.isEmpty() && fInfoStack.peek() instanceof ISourceElementRequestor.FieldInfo) { if (!fDeferredVariables.containsKey(fInfoStack.peek())) { fDeferredVariables.put(fInfoStack.peek(), new LinkedList<Assignment>()); } fDeferredVariables.get(fInfoStack.peek()).add(assignment); return false; } ISourceElementRequestor.FieldInfo info = new ISourceElementRequestor.FieldInfo(); info.modifiers = Modifiers.AccPublic; info.name = ((VariableReference) left).getName(); info.nameSourceEnd = left.sourceEnd() - 1; info.nameSourceStart = left.sourceStart(); info.declarationStart = assignment.sourceStart(); fInfoStack.push(info); if (assignment.getOperatorType() == Assignment.OP_EQUAL) { fRequestor.enterField(info); } else { ISourceElementRequestor sourceElementRequestor = (ISourceElementRequestor) fRequestor; sourceElementRequestor.enterFieldCheckDuplicates(info); } fNodes.push(assignment); } return true; }
public boolean endvisit(ModuleDeclaration declaration) throws Exception { while (deferredDeclarations != null && !deferredDeclarations.isEmpty()) { final ASTNode[] declarations = deferredDeclarations.toArray(new ASTNode[deferredDeclarations.size()]); deferredDeclarations.clear(); for (ASTNode deferred : declarations) { deferred.traverse(this); } } for (PhpIndexingVisitorExtension visitor : extensions) { visitor.endvisit(declaration); } fLastUseParts.clear(); endvisitGeneral(declaration); return true; }
public PhpIndexingVisitor(IIndexingRequestor requestor, ISourceModule module) { this.requestor = requestor; List<PhpIndexingVisitorExtension> extensions = new ArrayList<PhpIndexingVisitorExtension>(extensionElements.length); for (IConfigurationElement element : extensionElements) { try { PhpIndexingVisitorExtension ext = (PhpIndexingVisitorExtension) element.createExecutableExtension(CLASS_ATTR); ext.setRequestor(requestor); // pass the ISourceModule over to the extension // in case it needs it during indexing ext.setSourceModule(module); extensions.add(ext); } catch (CoreException e) { Logger.logException(e); } } this.extensions = extensions.toArray(new PhpIndexingVisitorExtension[extensions.size()]); }
private String[] processParameterTypes(MethodDeclaration methodDeclaration) { List<?> args = methodDeclaration.getArguments(); PHPDocBlock docBlock = ((PHPMethodDeclaration) methodDeclaration).getPHPDoc(); String[] parameterType = new String[args.size()]; for (int a = 0; a < args.size(); a++) { Argument arg = (Argument) args.get(a); if (arg instanceof FormalParameter) { SimpleReference type = ((FormalParameter) arg).getParameterType(); if (type != null) { parameterType[a] = type.getName(); } else if (docBlock != null) { for (PHPDocTag tag : docBlock.getTags(PHPDocTag.PARAM)) { if (tag.isValidParamTag() && tag.getVariableReference().getName().equals(arg.getName())) { parameterType[a] = tag.getSingleTypeReference().getName(); break; } } } } } return parameterType; }
private boolean visitMethodDeclaration(MethodDeclaration method) throws Exception { this.fNodes.push(method); List<?> args = method.getArguments(); String[] parameter = new String[args.size()]; String[] initializers = new String[args.size()]; for (int a = 0; a < args.size(); a++) { Argument arg = (Argument) args.get(a); parameter[a] = arg.getName(); if (arg.getInitialization() != null) { if (arg.getInitialization() instanceof Literal) { Literal scalar = (Literal) arg.getInitialization(); initializers[a] = scalar.getValue(); } else { initializers[a] = DEFAULT_VALUE; } } } ISourceElementRequestor.MethodInfo mi = new ISourceElementRequestor.MethodInfo(); mi.parameterNames = parameter; mi.name = method.getName(); mi.modifiers = method.getModifiers(); mi.nameSourceStart = method.getNameStart(); mi.nameSourceEnd = method.getNameEnd() - 1; mi.declarationStart = method.sourceStart(); mi.parameterInitializers = initializers; modifyMethodInfo(method, mi); fInfoStack.push(mi); this.fRequestor.enterMethod(mi); this.fInMethod = true; this.fCurrentMethod = method; return true; }
public PHPSourceElementRequestor(ISourceElementRequestor requestor, IModuleSource sourceModule) { super(requestor); // Load PHP source element requester extensions IConfigurationElement[] elements = Platform.getExtensionRegistry() .getConfigurationElementsFor( PHPCorePlugin.ID, "phpSourceElementRequestors"); // $NON-NLS-1$ List<PHPSourceElementRequestorExtension> requestors = new ArrayList<PHPSourceElementRequestorExtension>(elements.length); for (IConfigurationElement element : elements) { try { PHPSourceElementRequestorExtension extension = (PHPSourceElementRequestorExtension) element.createExecutableExtension("class"); // $NON-NLS-1$ extension.setRequestor(fRequestor); extension.setSourceModule(sourceModule); requestors.add(extension); } catch (CoreException e) { Logger.logException(e); } } extensions = requestors.toArray(new PHPSourceElementRequestorExtension[requestors.size()]); }
protected String[] processSuperClasses(TypeDeclaration type) { ASTListNode superClasses = type.getSuperClasses(); if (superClasses == null) { return new String[] {}; } List<ASTNode> superClassNames = superClasses.getChilds(); List<String> result = new ArrayList<String>(superClassNames.size()); Iterator<ASTNode> iterator = superClassNames.iterator(); while (iterator.hasNext()) { String name = processNameNode(iterator.next()); if (name != null) { result.add(name); } } return (String[]) result.toArray(new String[result.size()]); }
public boolean visit(AnonymousClassDeclaration anonymousClassDeclaration) throws Exception { ASTNode parentDeclaration = null; if (!declarations.empty()) { parentDeclaration = declarations.peek(); } if (parentDeclaration instanceof TypeDeclaration) { return false; } fNodes.push(anonymousClassDeclaration); declarations.push(anonymousClassDeclaration); for (PHPSourceElementRequestorExtension visitor : extensions) { visitor.visit(anonymousClassDeclaration); } List<String> superClasses = new ArrayList<String>(); String name = null; if (anonymousClassDeclaration.getSuperClass() != null) { name = String.format( ANONYMOUS_CLASS_TEMPLATE, anonymousClassDeclaration.getSuperClass().getName()); String superClass = processNameNode(anonymousClassDeclaration.getSuperClass()); if (superClass != null) { superClasses.add(superClass); } } if (anonymousClassDeclaration.getInterfaceList() != null && !anonymousClassDeclaration.getInterfaceList().isEmpty()) { if (name == null) { name = String.format( ANONYMOUS_CLASS_TEMPLATE, anonymousClassDeclaration.getInterfaceList().get(0).getName()); } for (TypeReference reference : anonymousClassDeclaration.getInterfaceList()) { String interfaceName = processNameNode(reference); if (interfaceName != null) { superClasses.add(interfaceName); } } } if (name == null) { name = String.format(ANONYMOUS_CLASS_TEMPLATE, PHPCoreConstants.ANONYMOUS); } ISourceElementRequestor.TypeInfo mi = new ISourceElementRequestor.TypeInfo(); mi.name = name; mi.modifiers = Modifiers.AccPrivate | IPHPModifiers.AccAnonymous; if (fLastInstanceCreation != null) { Expression className = fLastInstanceCreation.getClassName(); mi.nameSourceStart = className.sourceStart(); mi.nameSourceEnd = className.sourceEnd() - 1; } mi.declarationStart = mi.nameSourceStart; mi.superclasses = superClasses.toArray(new String[0]); fInfoStack.push(mi); this.fRequestor.enterType(mi); this.fInClass = true; return true; }
protected String[] processSuperClasses(TypeDeclaration type) { ASTListNode superClasses = type.getSuperClasses(); if (superClasses == null) { return new String[] {}; } List<ASTNode> superClassNames = superClasses.getChilds(); List<String> result = new ArrayList<String>(superClassNames.size()); Iterator<ASTNode> iterator = superClassNames.iterator(); while (iterator.hasNext()) { ASTNode nameNode = iterator.next(); String name; if (nameNode instanceof FullyQualifiedReference) { FullyQualifiedReference fullyQualifiedName = (FullyQualifiedReference) nameNode; name = fullyQualifiedName.getFullyQualifiedName(); if (fullyQualifiedName.getNamespace() != null) { String namespace = fullyQualifiedName.getNamespace().getName(); String subnamespace = ""; // $NON-NLS-1$ if (namespace.charAt(0) != NamespaceReference.NAMESPACE_SEPARATOR && namespace.indexOf(NamespaceReference.NAMESPACE_SEPARATOR) > 0) { int firstNSLocation = namespace.indexOf(NamespaceReference.NAMESPACE_SEPARATOR); subnamespace = namespace.substring(firstNSLocation); namespace = namespace.substring(0, firstNSLocation); } if (name.charAt(0) == NamespaceReference.NAMESPACE_SEPARATOR) { name = name.substring(1); } else if (fLastUseParts.containsKey(namespace)) { name = new StringBuilder( fLastUseParts.get(namespace).getNamespace().getFullyQualifiedName()) .append(subnamespace) .append(NamespaceReference.NAMESPACE_SEPARATOR) .append(fullyQualifiedName.getName()) .toString(); } else if (fCurrentNamespace != null) { name = new StringBuilder(fCurrentNamespace.getName()) .append(NamespaceReference.NAMESPACE_SEPARATOR) .append(name) .toString(); } } else if (fLastUseParts.containsKey(name)) { name = fLastUseParts.get(name).getNamespace().getFullyQualifiedName(); if (name.charAt(0) == NamespaceReference.NAMESPACE_SEPARATOR) { name = name.substring(1); } } else { if (fCurrentNamespace != null) { name = new StringBuilder(fCurrentNamespace.getName()) .append(NamespaceReference.NAMESPACE_SEPARATOR) .append(name) .toString(); } } result.add(name); } else if (nameNode instanceof SimpleReference) { result.add(((SimpleReference) nameNode).getName()); } } return (String[]) result.toArray(new String[result.size()]); }
public boolean visit(TypeDeclaration type) throws Exception { if (type instanceof NamespaceDeclaration) { NamespaceDeclaration namespaceDecl = (NamespaceDeclaration) type; fCurrentNamespace = namespaceDecl; fLastUseParts.clear(); if (namespaceDecl.isGlobal()) { return visitGeneral(type); } declarations.push(type); int modifiers = type.getModifiers() | Modifiers.AccNameSpace; fCurrentQualifier = type.getName(); Integer count = fCurrentQualifierCounts.get(fCurrentQualifier); count = count != null ? count + 1 : 1; fCurrentQualifierCounts.put(fCurrentQualifier, count); modifiers = markAsDeprecated(modifiers, type); StringBuilder metadata = new StringBuilder(); if (fCurrentQualifier != null) { metadata.append(fCurrentQualifierCounts.get(fCurrentQualifier)); metadata.append(";"); // $NON-NLS-1$ } modifyDeclaration( type, new DeclarationInfo( IModelElement.PACKAGE_DECLARATION, modifiers, type.sourceStart(), type.sourceEnd() - type.sourceStart(), type.getNameStart(), type.getNameEnd() - type.getNameStart(), type.getName(), metadata.length() == 0 ? null : metadata.toString(), encodeDocInfo(type), null, null)); } else { Declaration parentDeclaration = null; if (!declarations.empty()) { parentDeclaration = declarations.peek(); } declarations.push(type); if (!(parentDeclaration instanceof NamespaceDeclaration)) { type.setModifier(Modifiers.AccGlobal); } // In case we are entering a nested element if (parentDeclaration instanceof MethodDeclaration) { if (fCurrentNamespace == null) { deferredDeclarations.add(type); } else { deferredNamespacedDeclarations.add(type); } return visitGeneral(type); } int modifiers = type.getModifiers(); fCurrentParent = type.getName(); String[] superClasses = processSuperClasses(type); StringBuilder metadata = new StringBuilder(); if (fCurrentQualifier != null) { metadata.append(fCurrentQualifierCounts.get(fCurrentQualifier)); metadata.append(";"); // $NON-NLS-1$ } for (int i = 0; i < superClasses.length; ++i) { metadata.append(superClasses[i]); if (i < superClasses.length - 1) { metadata.append(","); // $NON-NLS-1$ } } modifiers = markAsDeprecated(modifiers, type); modifyDeclaration( type, new DeclarationInfo( IModelElement.TYPE, modifiers, type.sourceStart(), type.sourceEnd() - type.sourceStart(), type.getNameStart(), type.getNameEnd() - type.getNameStart(), type.getName(), metadata.length() == 0 ? null : metadata.toString(), encodeDocInfo(type), fCurrentQualifier, null)); } for (PhpIndexingVisitorExtension visitor : extensions) { visitor.visit(type); } return visitGeneral(type); }
@SuppressWarnings("unchecked") public boolean visit(MethodDeclaration method) throws Exception { fNodes.push(method); methodGlobalVars.add(new HashSet<String>()); int modifiers = method.getModifiers(); PHPDocBlock doc = null; if (method instanceof IPHPDocAwareDeclaration) { IPHPDocAwareDeclaration declaration = (IPHPDocAwareDeclaration) method; doc = declaration.getPHPDoc(); } Declaration parentDeclaration = null; if (!declarations.empty()) { parentDeclaration = declarations.peek(); } declarations.push(method); // In case we are entering a nested element - just add to the deferred // list // and get out of the nested element visiting process if (parentDeclaration instanceof MethodDeclaration) { if (fCurrentNamespace == null) { deferredDeclarations.add(method); } else { deferredNamespacedDeclarations.add(method); } return visitGeneral(method); } if (parentDeclaration instanceof InterfaceDeclaration) { method.setModifier(Modifiers.AccAbstract); } String methodName = method.getName(); // Determine whether this method represents constructor: if (methodName.equalsIgnoreCase(CONSTRUCTOR_NAME) || (parentDeclaration instanceof ClassDeclaration && methodName.equalsIgnoreCase(((ClassDeclaration) parentDeclaration).getName()))) { modifiers |= IPHPModifiers.Constructor; } if (parentDeclaration == null || (parentDeclaration instanceof TypeDeclaration && parentDeclaration == fCurrentNamespace)) { modifiers |= Modifiers.AccGlobal; } if (!Flags.isPrivate(modifiers) && !Flags.isProtected(modifiers) && !Flags.isPublic(modifiers)) { modifiers |= Modifiers.AccPublic; } modifiers = markAsDeprecated(modifiers, method); StringBuilder metadata = new StringBuilder(); if (fCurrentQualifier != null) { metadata.append(fCurrentQualifierCounts.get(fCurrentQualifier)); metadata.append(";"); // $NON-NLS-1$ } List<Argument> arguments = method.getArguments(); if (arguments != null) { Iterator<Argument> i = arguments.iterator(); while (i.hasNext()) { Argument arg = (Argument) i.next(); String type = NULL_VALUE; if (arg instanceof FormalParameter) { FormalParameter fp = (FormalParameter) arg; if (fp.getParameterType() != null) { if (fp.getParameterType().getName() != null) { type = fp.getParameterType().getName(); } } } if (type == NULL_VALUE && doc != null) { type = getParamType(doc, arg.getName(), type); } metadata.append(type); metadata.append(PARAMETER_SEPERATOR); metadata.append(arg.getName()); metadata.append(PARAMETER_SEPERATOR); String defaultValue = NULL_VALUE; if (arg.getInitialization() != null) { if (arg.getInitialization() instanceof Literal) { Literal scalar = (Literal) arg.getInitialization(); defaultValue = scalar.getValue(); } else { defaultValue = DEFAULT_VALUE; } } metadata.append(defaultValue); if (i.hasNext()) { metadata.append(","); // $NON-NLS-1$ } } } // Add method declaration: modifyDeclaration( method, new DeclarationInfo( IModelElement.METHOD, modifiers, method.sourceStart(), method.sourceEnd() - method.sourceStart(), method.getNameStart(), method.getNameEnd() - method.getNameStart(), methodName, metadata.length() == 0 ? null : metadata.toString(), encodeDocInfo(method), fCurrentQualifier, fCurrentParent)); for (PhpIndexingVisitorExtension visitor : extensions) { visitor.visit(method); } return visitGeneral(method); }