@Override public Void visitVariableDeclaration(VariableDeclaration node) { VariableElementImpl element; if (inFieldContext) { SimpleIdentifier fieldName = node.getName(); element = new FieldElementImpl(fieldName); currentHolder.addField((FieldElementImpl) element); fieldName.setElement(element); } else { SimpleIdentifier variableName = node.getName(); element = new VariableElementImpl(variableName); currentHolder.addVariable(element); variableName.setElement(element); } Token keyword = ((VariableDeclarationList) node.getParent()).getKeyword(); boolean isFinal = matches(keyword, Keyword.FINAL); element.setConst(matches(keyword, Keyword.CONST)); element.setFinal(isFinal); if (node.getInitializer() != null) { ElementHolder holder = new ElementHolder(); boolean wasInFieldContext = inFieldContext; inFieldContext = false; try { visitChildren(holder, node.getInitializer()); } finally { inFieldContext = wasInFieldContext; } FunctionElementImpl initializer = new FunctionElementImpl(); initializer.setFunctions(holder.getFunctions()); initializer.setLabels(holder.getLabels()); initializer.setLocalVariables(holder.getVariables()); initializer.setSynthetic(true); element.setInitializer(initializer); } if (inFieldContext) { FieldElementImpl field = (FieldElementImpl) element; PropertyAccessorElementImpl getter = new PropertyAccessorElementImpl(field); getter.setGetter(true); field.setGetter(getter); if (!isFinal) { PropertyAccessorElementImpl setter = new PropertyAccessorElementImpl(field); setter.setSetter(true); field.setSetter(setter); } field.setStatic( matches(((FieldDeclaration) node.getParent().getParent()).getKeyword(), Keyword.STATIC)); } return null; }
@Override public Void visitMethodDeclaration(MethodDeclaration node) { ElementHolder holder = new ElementHolder(); visitChildren(holder, node); Token property = node.getPropertyKeyword(); if (property == null) { Identifier methodName = node.getName(); MethodElementImpl element = new MethodElementImpl(methodName); Token keyword = node.getModifierKeyword(); element.setAbstract(matches(keyword, Keyword.ABSTRACT)); element.setFunctions(holder.getFunctions()); element.setLabels(holder.getLabels()); element.setLocalVariables(holder.getVariables()); element.setParameters(holder.getParameters()); element.setStatic(matches(keyword, Keyword.STATIC)); currentHolder.addMethod(element); methodName.setElement(element); } else { Identifier propertyNameNode = node.getName(); String propertyName = propertyNameNode.getName(); FieldElementImpl field = (FieldElementImpl) currentHolder.getField(propertyName); if (field == null) { field = new FieldElementImpl(node.getName().getName()); field.setFinal(true); field.setStatic(matches(node.getModifierKeyword(), Keyword.STATIC)); currentHolder.addField(field); } if (matches(property, Keyword.GET)) { PropertyAccessorElementImpl getter = new PropertyAccessorElementImpl(propertyNameNode); getter.setField(field); getter.setGetter(true); field.setGetter(getter); currentHolder.addAccessor(getter); propertyNameNode.setElement(getter); } else { PropertyAccessorElementImpl setter = new PropertyAccessorElementImpl(propertyNameNode); setter.setField(field); setter.setSetter(true); field.setSetter(setter); field.setFinal(false); currentHolder.addAccessor(setter); propertyNameNode.setElement(setter); } } return null; }
public void test_visitLabeledStatement() { ElementHolder holder = new ElementHolder(); ElementBuilder builder = new ElementBuilder(holder); String labelName = "l"; LabeledStatement statement = labeledStatement(list(label(labelName)), breakStatement()); statement.accept(builder); LabelElement[] labels = holder.getLabels(); assertLength(1, labels); LabelElement label = labels[0]; assertNotNull(label); assertEquals(labelName, label.getName()); assertFalse(label.isSynthetic()); }
@Override public Void visitFunctionExpression(FunctionExpression node) { ElementHolder holder = new ElementHolder(); visitChildren(holder, node); FunctionElementImpl element = new FunctionElementImpl(node.getName()); element.setFunctions(holder.getFunctions()); element.setLabels(holder.getLabels()); element.setLocalVariables(holder.getVariables()); if (holder.getParameters() != null) { element.setParameters(holder.getParameters()); } currentHolder.addFunction(element); return null; }
@Override public Void visitFunctionExpression(FunctionExpression node) { ElementHolder holder = new ElementHolder(); visitChildren(holder, node); SimpleIdentifier functionName = null; FunctionElementImpl element = new FunctionElementImpl(functionName); element.setFunctions(holder.getFunctions()); element.setLabels(holder.getLabels()); element.setLocalVariables(holder.getVariables()); element.setParameters(holder.getParameters()); currentHolder.addFunction(element); node.setElement(element); return null; }
@Override public Void visitNamedFormalParameter(NamedFormalParameter node) { ElementHolder holder = new ElementHolder(); visitChildren(holder, node.getDefaultValue()); FunctionElementImpl initializer = new FunctionElementImpl(); initializer.setFunctions(holder.getFunctions()); initializer.setLabels(holder.getLabels()); initializer.setLocalVariables(holder.getVariables()); if (holder.getParameters() != null) { initializer.setParameters(holder.getParameters()); } VariableElementImpl parameter = new VariableElementImpl(node.getParameter().getIdentifier()); parameter.setInitializer(initializer); currentHolder.addVariable(parameter); return null; }
@Override public Void visitConstructorDeclaration(ConstructorDeclaration node) { ElementHolder holder = new ElementHolder(); visitChildren(holder, node); ConstructorElementImpl element = new ConstructorElementImpl(node.getName()); Token keyword = node.getKeyword(); if (keyword instanceof KeywordToken && ((KeywordToken) keyword).getKeyword() == Keyword.FACTORY) { element.setFactory(true); } element.setFunctions(holder.getFunctions()); element.setLabels(holder.getLabels()); element.setLocalVariables(holder.getVariables()); if (holder.getParameters() != null) { element.setParameters(holder.getParameters()); } currentHolder.addConstructor(element); return null; }
@Override public Void visitDefaultFormalParameter(DefaultFormalParameter node) { ElementHolder holder = new ElementHolder(); SimpleIdentifier parameterName = node.getParameter().getIdentifier(); ParameterElementImpl parameter; if (node.getParameter() instanceof FieldFormalParameter) { parameter = new DefaultFieldFormalParameterElementImpl(parameterName); } else { parameter = new DefaultParameterElementImpl(parameterName); } parameter.setConst(node.isConst()); parameter.setFinal(node.isFinal()); parameter.setParameterKind(node.getKind()); // set initializer, default value range Expression defaultValue = node.getDefaultValue(); if (defaultValue != null) { visit(holder, defaultValue); FunctionElementImpl initializer = new FunctionElementImpl(defaultValue.getBeginToken().getOffset()); initializer.setFunctions(holder.getFunctions()); initializer.setLabels(holder.getLabels()); initializer.setLocalVariables(holder.getLocalVariables()); initializer.setParameters(holder.getParameters()); initializer.setSynthetic(true); parameter.setInitializer(initializer); parameter.setDefaultValueRange(defaultValue.getOffset(), defaultValue.getLength()); } // visible range setParameterVisibleRange(node, parameter); currentHolder.addParameter(parameter); parameterName.setStaticElement(parameter); node.getParameter().accept(this); holder.validate(); return null; }
@Override public Void visitDefaultFormalParameter(DefaultFormalParameter node) { ElementHolder holder = new ElementHolder(); visitChildren(holder, node.getDefaultValue()); FunctionElementImpl initializer = new FunctionElementImpl(); initializer.setFunctions(holder.getFunctions()); initializer.setLabels(holder.getLabels()); initializer.setLocalVariables(holder.getVariables()); initializer.setParameters(holder.getParameters()); SimpleIdentifier parameterName = node.getParameter().getIdentifier(); ParameterElementImpl parameter = new ParameterElementImpl(parameterName); parameter.setConst(node.isConst()); parameter.setFinal(node.isFinal()); parameter.setInitializer(initializer); parameter.setParameterKind(node.getKind()); currentHolder.addParameter(parameter); parameterName.setElement(parameter); return null; }
@Override public Void visitConstructorDeclaration(ConstructorDeclaration node) { ElementHolder holder = new ElementHolder(); visitChildren(holder, node); SimpleIdentifier constructorName = node.getName(); ConstructorElementImpl element = new ConstructorElementImpl(constructorName); if (node.getFactoryKeyword() != null) { element.setFactory(true); } element.setFunctions(holder.getFunctions()); element.setLabels(holder.getLabels()); element.setLocalVariables(holder.getVariables()); element.setParameters(holder.getParameters()); currentHolder.addConstructor(element); node.setElement(element); if (constructorName != null) { constructorName.setElement(element); } return null; }
@Override public Void visitFunctionExpression(FunctionExpression node) { ElementHolder holder = new ElementHolder(); boolean wasInFunction = inFunction; inFunction = true; try { visitChildren(holder, node); } finally { inFunction = wasInFunction; } FunctionElementImpl element = new FunctionElementImpl(node.getBeginToken().getOffset()); element.setFunctions(holder.getFunctions()); element.setLabels(holder.getLabels()); element.setLocalVariables(holder.getLocalVariables()); element.setParameters(holder.getParameters()); if (inFunction) { Block enclosingBlock = node.getAncestor(Block.class); if (enclosingBlock != null) { int functionEnd = node.getOffset() + node.getLength(); int blockEnd = enclosingBlock.getOffset() + enclosingBlock.getLength(); element.setVisibleRange(functionEnd, blockEnd - functionEnd - 1); } } FunctionTypeImpl type = new FunctionTypeImpl(element); if (functionTypesToFix != null) { functionTypesToFix.add(type); } element.setType(type); currentHolder.addFunction(element); node.setElement(element); holder.validate(); return null; }
@Override public Void visitConstructorDeclaration(ConstructorDeclaration node) { isValidMixin = false; ElementHolder holder = new ElementHolder(); boolean wasInFunction = inFunction; inFunction = true; try { visitChildren(holder, node); } finally { inFunction = wasInFunction; } SimpleIdentifier constructorName = node.getName(); ConstructorElementImpl element = new ConstructorElementImpl(constructorName); if (node.getFactoryKeyword() != null) { element.setFactory(true); } element.setFunctions(holder.getFunctions()); element.setLabels(holder.getLabels()); element.setLocalVariables(holder.getLocalVariables()); element.setParameters(holder.getParameters()); element.setConst(node.getConstKeyword() != null); currentHolder.addConstructor(element); node.setElement(element); if (constructorName == null) { Identifier returnType = node.getReturnType(); if (returnType != null) { element.setNameOffset(returnType.getOffset()); } } else { constructorName.setStaticElement(element); } holder.validate(); return null; }
@Override public Void visitVariableDeclaration(VariableDeclaration node) { Token keyword = ((VariableDeclarationList) node.getParent()).getKeyword(); boolean isConst = matches(keyword, Keyword.CONST); boolean isFinal = matches(keyword, Keyword.FINAL); boolean hasInitializer = node.getInitializer() != null; VariableElementImpl element; if (inFieldContext) { SimpleIdentifier fieldName = node.getName(); FieldElementImpl field; if (isConst && hasInitializer) { field = new ConstFieldElementImpl(fieldName); } else { field = new FieldElementImpl(fieldName); } element = field; currentHolder.addField(field); fieldName.setStaticElement(field); } else if (inFunction) { SimpleIdentifier variableName = node.getName(); LocalVariableElementImpl variable; if (isConst && hasInitializer) { variable = new ConstLocalVariableElementImpl(variableName); } else { variable = new LocalVariableElementImpl(variableName); } element = variable; Block enclosingBlock = node.getAncestor(Block.class); int functionEnd = node.getOffset() + node.getLength(); int blockEnd = enclosingBlock.getOffset() + enclosingBlock.getLength(); // TODO(brianwilkerson) This isn't right for variables declared in a for loop. variable.setVisibleRange(functionEnd, blockEnd - functionEnd - 1); currentHolder.addLocalVariable(variable); variableName.setStaticElement(element); } else { SimpleIdentifier variableName = node.getName(); TopLevelVariableElementImpl variable; if (isConst && hasInitializer) { variable = new ConstTopLevelVariableElementImpl(variableName); } else { variable = new TopLevelVariableElementImpl(variableName); } element = variable; currentHolder.addTopLevelVariable(variable); variableName.setStaticElement(element); } element.setConst(isConst); element.setFinal(isFinal); if (hasInitializer) { ElementHolder holder = new ElementHolder(); boolean wasInFieldContext = inFieldContext; inFieldContext = false; try { visit(holder, node.getInitializer()); } finally { inFieldContext = wasInFieldContext; } FunctionElementImpl initializer = new FunctionElementImpl(node.getInitializer().getBeginToken().getOffset()); initializer.setFunctions(holder.getFunctions()); initializer.setLabels(holder.getLabels()); initializer.setLocalVariables(holder.getLocalVariables()); initializer.setSynthetic(true); element.setInitializer(initializer); holder.validate(); } if (element instanceof PropertyInducingElementImpl) { PropertyInducingElementImpl variable = (PropertyInducingElementImpl) element; if (inFieldContext) { ((FieldElementImpl) variable) .setStatic( matches( ((FieldDeclaration) node.getParent().getParent()).getStaticKeyword(), Keyword.STATIC)); } PropertyAccessorElementImpl getter = new PropertyAccessorElementImpl(variable); getter.setGetter(true); getter.setStatic(variable.isStatic()); currentHolder.addAccessor(getter); variable.setGetter(getter); if (!isFinal) { PropertyAccessorElementImpl setter = new PropertyAccessorElementImpl(variable); setter.setSetter(true); setter.setStatic(variable.isStatic()); ParameterElementImpl parameter = new ParameterElementImpl("_" + variable.getName(), variable.getNameOffset()); parameter.setSynthetic(true); parameter.setParameterKind(ParameterKind.REQUIRED); setter.setParameters(new ParameterElement[] {parameter}); currentHolder.addAccessor(setter); variable.setSetter(setter); } } return null; }
@Override public Void visitMethodDeclaration(MethodDeclaration node) { ElementHolder holder = new ElementHolder(); boolean wasInFunction = inFunction; inFunction = true; try { visitChildren(holder, node); } finally { inFunction = wasInFunction; } boolean isStatic = node.isStatic(); Token property = node.getPropertyKeyword(); if (property == null) { SimpleIdentifier methodName = node.getName(); String nameOfMethod = methodName.getName(); if (nameOfMethod.equals(TokenType.MINUS.getLexeme()) && node.getParameters().getParameters().size() == 0) { nameOfMethod = "unary-"; } MethodElementImpl element = new MethodElementImpl(nameOfMethod, methodName.getOffset()); element.setAbstract(node.isAbstract()); element.setFunctions(holder.getFunctions()); element.setLabels(holder.getLabels()); element.setLocalVariables(holder.getLocalVariables()); element.setParameters(holder.getParameters()); element.setStatic(isStatic); currentHolder.addMethod(element); methodName.setStaticElement(element); } else { SimpleIdentifier propertyNameNode = node.getName(); String propertyName = propertyNameNode.getName(); FieldElementImpl field = (FieldElementImpl) currentHolder.getField(propertyName); if (field == null) { field = new FieldElementImpl(node.getName().getName()); field.setFinal(true); field.setStatic(isStatic); field.setSynthetic(true); currentHolder.addField(field); } if (matches(property, Keyword.GET)) { PropertyAccessorElementImpl getter = new PropertyAccessorElementImpl(propertyNameNode); getter.setFunctions(holder.getFunctions()); getter.setLabels(holder.getLabels()); getter.setLocalVariables(holder.getLocalVariables()); getter.setVariable(field); getter.setAbstract( node.getBody() instanceof EmptyFunctionBody && node.getExternalKeyword() == null); getter.setGetter(true); getter.setStatic(isStatic); field.setGetter(getter); currentHolder.addAccessor(getter); propertyNameNode.setStaticElement(getter); } else { PropertyAccessorElementImpl setter = new PropertyAccessorElementImpl(propertyNameNode); setter.setFunctions(holder.getFunctions()); setter.setLabels(holder.getLabels()); setter.setLocalVariables(holder.getLocalVariables()); setter.setParameters(holder.getParameters()); setter.setVariable(field); setter.setAbstract( node.getBody() instanceof EmptyFunctionBody && !matches(node.getExternalKeyword(), Keyword.EXTERNAL)); setter.setSetter(true); setter.setStatic(isStatic); field.setSetter(setter); field.setFinal(false); currentHolder.addAccessor(setter); propertyNameNode.setStaticElement(setter); } } holder.validate(); return null; }
@Override public Void visitFunctionDeclaration(FunctionDeclaration node) { FunctionExpression expression = node.getFunctionExpression(); if (expression != null) { ElementHolder holder = new ElementHolder(); boolean wasInFunction = inFunction; inFunction = true; try { visitChildren(holder, expression); } finally { inFunction = wasInFunction; } Token property = node.getPropertyKeyword(); if (property == null) { SimpleIdentifier functionName = node.getName(); FunctionElementImpl element = new FunctionElementImpl(functionName); element.setFunctions(holder.getFunctions()); element.setLabels(holder.getLabels()); element.setLocalVariables(holder.getLocalVariables()); element.setParameters(holder.getParameters()); if (inFunction) { Block enclosingBlock = node.getAncestor(Block.class); if (enclosingBlock != null) { int functionEnd = node.getOffset() + node.getLength(); int blockEnd = enclosingBlock.getOffset() + enclosingBlock.getLength(); element.setVisibleRange(functionEnd, blockEnd - functionEnd - 1); } } currentHolder.addFunction(element); expression.setElement(element); functionName.setStaticElement(element); } else { SimpleIdentifier propertyNameNode = node.getName(); if (propertyNameNode == null) { // TODO(brianwilkerson) Report this internal error. return null; } String propertyName = propertyNameNode.getName(); TopLevelVariableElementImpl variable = (TopLevelVariableElementImpl) currentHolder.getTopLevelVariable(propertyName); if (variable == null) { variable = new TopLevelVariableElementImpl(node.getName().getName()); variable.setFinal(true); variable.setSynthetic(true); currentHolder.addTopLevelVariable(variable); } if (matches(property, Keyword.GET)) { PropertyAccessorElementImpl getter = new PropertyAccessorElementImpl(propertyNameNode); getter.setFunctions(holder.getFunctions()); getter.setLabels(holder.getLabels()); getter.setLocalVariables(holder.getLocalVariables()); getter.setVariable(variable); getter.setGetter(true); getter.setStatic(true); variable.setGetter(getter); currentHolder.addAccessor(getter); expression.setElement(getter); propertyNameNode.setStaticElement(getter); } else { PropertyAccessorElementImpl setter = new PropertyAccessorElementImpl(propertyNameNode); setter.setFunctions(holder.getFunctions()); setter.setLabels(holder.getLabels()); setter.setLocalVariables(holder.getLocalVariables()); setter.setParameters(holder.getParameters()); setter.setVariable(variable); setter.setSetter(true); setter.setStatic(true); variable.setSetter(setter); variable.setFinal(false); currentHolder.addAccessor(setter); expression.setElement(setter); propertyNameNode.setStaticElement(setter); } } holder.validate(); } return null; }