Ejemplo n.º 1
0
 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".equals(((VariableReference) dispatcher).getName())) { // $NON-NLS-1$
       Expression field = fieldAccess.getField();
       if (field instanceof SimpleReference) {
         SimpleReference var = (SimpleReference) field;
         int modifiers = Modifiers.AccPublic;
         int offset = var.sourceStart();
         int length = var.sourceEnd() - offset;
         StringBuilder metadata = new StringBuilder();
         if (fCurrentQualifier != null) {
           metadata.append(fCurrentQualifierCounts.get(fCurrentQualifier));
           metadata.append(";"); // $NON-NLS-1$
         }
         modifyDeclaration(
             assignment,
             new DeclarationInfo(
                 IModelElement.FIELD,
                 modifiers,
                 offset,
                 length,
                 offset,
                 length,
                 '$' + var.getName(),
                 metadata.length() == 0 ? null : metadata.toString(),
                 null,
                 fCurrentQualifier,
                 fCurrentParent));
       }
     }
   } else if (left instanceof VariableReference) {
     int modifiers = Modifiers.AccPublic | Modifiers.AccGlobal;
     if (!declarations.empty()
         && declarations.peek() instanceof MethodDeclaration
         && !methodGlobalVars.peek().contains(((VariableReference) left).getName())) {
       return visitGeneral(assignment);
     }
     int offset = left.sourceStart();
     int length = left.sourceEnd() - offset;
     modifyDeclaration(
         assignment,
         new DeclarationInfo(
             IModelElement.FIELD,
             modifiers,
             offset,
             length,
             offset,
             length,
             ((VariableReference) left).getName(),
             null,
             null,
             null,
             null));
   }
   return visitGeneral(assignment);
 }
  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;
  }
Ejemplo n.º 3
0
  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(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);
  }
  protected void modifyMethodInfo(
      MethodDeclaration methodDeclaration, ISourceElementRequestor.MethodInfo mi) {
    ASTNode parentDeclaration = null;

    // find declaration that was before this method:
    declarations.pop();
    if (!declarations.empty()) {
      parentDeclaration = declarations.peek();
    }
    declarations.push(methodDeclaration);

    mi.isConstructor =
        mi.name.equalsIgnoreCase(CONSTRUCTOR_NAME)
            || (parentDeclaration instanceof ClassDeclaration
                && mi.name.equalsIgnoreCase(((ClassDeclaration) parentDeclaration).getName()));

    if (fCurrentClass == null || fCurrentClass == fLastNamespace) {
      mi.modifiers |= Modifiers.AccGlobal;
    }
    if (!Flags.isPrivate(mi.modifiers)
        && !Flags.isProtected(mi.modifiers)
        && !Flags.isPublic(mi.modifiers)) {
      mi.modifiers |= Modifiers.AccPublic;
    }

    mi.parameterTypes = processParameterTypes(methodDeclaration);
    mi.returnType = processReturnType(methodDeclaration);

    // modify method info if needed by extensions
    for (PHPSourceElementRequestorExtension extension : extensions) {
      extension.modifyMethodInfo(methodDeclaration, mi);
    }
  }
  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;
  }
Ejemplo n.º 7
0
 public boolean visit(GlobalStatement s) throws Exception {
   if (!declarations.empty() && declarations.peek() instanceof MethodDeclaration) {
     for (Expression var : s.getVariables()) {
       if (var instanceof ReferenceExpression) {
         var = ((ReferenceExpression) var).getVariable();
       }
       if (var instanceof SimpleReference) {
         methodGlobalVars.peek().add(((SimpleReference) var).getName());
       }
     }
   }
   return visitGeneral(s);
 }
  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;
  }
Ejemplo n.º 9
0
  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);
  }
Ejemplo n.º 10
0
  @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);
  }
  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;
  }