use of com.redhat.ceylon.compiler.typechecker.tree.Visitor in project ceylon-compiler by ceylon.
the class ClassOrPackageDoc method getParametersAssertions.
private Map<Parameter, Map<Tree.Assertion, List<Tree.Condition>>> getParametersAssertions(final Declaration decl) {
final Map<Parameter, Map<Tree.Assertion, List<Tree.Condition>>> parametersAssertions = new LinkedHashMap<Parameter, Map<Tree.Assertion, List<Tree.Condition>>>();
if (((Functional) decl).getParameterLists().isEmpty()) {
return parametersAssertions;
}
Node node = tool.getNode(decl);
PhasedUnit pu = tool.getUnit(decl);
if (node == null || pu == null) {
return parametersAssertions;
}
Tree.Body body = null;
if (node instanceof Tree.MethodDefinition) {
body = ((Tree.MethodDefinition) node).getBlock();
} else if (node instanceof Tree.ClassDefinition) {
body = ((Tree.ClassDefinition) node).getClassBody();
}
if (body == null) {
return parametersAssertions;
}
final Map<String, Parameter> parametersNames = new HashMap<String, Parameter>();
for (ParameterList parameterList : ((Functional) decl).getParameterLists()) {
for (Parameter parameter : parameterList.getParameters()) {
parametersNames.put(parameter.getName(), parameter);
}
}
body.visitChildren(new Visitor() {
private boolean stop = false;
private Tree.Assertion assertion = null;
private Set<Parameter> referencedParameters = new HashSet<Parameter>();
@Override
public void visit(Tree.Assertion that) {
assertion = that;
super.visit(that);
assertion = null;
}
@Override
public void visit(Tree.Condition that) {
referencedParameters.clear();
super.visit(that);
if (assertion != null && !referencedParameters.isEmpty()) {
for (Parameter referencedParameter : referencedParameters) {
Map<Tree.Assertion, List<Tree.Condition>> parameterAssertions = parametersAssertions.get(referencedParameter);
if (parameterAssertions == null) {
parameterAssertions = new LinkedHashMap<Tree.Assertion, List<Tree.Condition>>();
parametersAssertions.put(referencedParameter, parameterAssertions);
}
List<Tree.Condition> parameterConditions = parameterAssertions.get(assertion);
if (parameterConditions == null) {
parameterConditions = new ArrayList<Tree.Condition>();
parameterAssertions.put(assertion, parameterConditions);
}
parameterConditions.add(that);
}
}
}
@Override
public void visit(Tree.BaseMemberExpression that) {
if (assertion != null) {
Declaration d = that.getDeclaration();
Scope realScope = com.redhat.ceylon.model.typechecker.model.ModelUtil.getRealScope(d.getScope());
if (parametersNames.containsKey(d.getName()) && realScope == decl) {
referencedParameters.add(parametersNames.get(d.getName()));
}
}
super.visit(that);
}
@Override
public void visit(Tree.Statement that) {
if (assertion == null) {
stop = true;
}
super.visit(that);
}
@Override
public void visitAny(Node that) {
if (!stop) {
super.visitAny(that);
}
}
});
return parametersAssertions;
}
use of com.redhat.ceylon.compiler.typechecker.tree.Visitor in project ceylon-compiler by ceylon.
the class LinkRenderer method findDocLink.
private Tree.DocLink findDocLink(final String docLinkText, Referenceable referenceable) {
final Tree.DocLink[] docLinks = new Tree.DocLink[1];
Node scopeNode = ceylonDocTool.getNode(referenceable);
if (scopeNode != null) {
scopeNode.visit(new Visitor() {
@Override
public void visit(Tree.DocLink docLink) {
String s1 = normalizeSpaces(docLinkText);
String s2 = normalizeSpaces(docLink.getText());
if (s1.equals(s2)) {
docLinks[0] = docLink;
return;
}
}
});
}
return docLinks[0];
}
use of com.redhat.ceylon.compiler.typechecker.tree.Visitor in project ceylon-compiler by ceylon.
the class CeylonVisitor method getHeaderDeclaration.
// Traverse the entire tree looking for the header node
// that belongs to the given implementation declaration
private Tree.Declaration getHeaderDeclaration(final Declaration decl) {
class ClassVisitor extends Visitor {
Tree.Declaration hdr = null;
@Override
public void visit(Tree.ClassOrInterface that) {
checkForHeader(that);
super.visit(that);
}
@Override
public void visit(Tree.ObjectDefinition that) {
checkForHeader(that);
super.visit(that);
}
private void checkForHeader(Tree.Declaration that) {
Declaration v = that.getDeclarationModel();
if (v.isNativeHeader() && v.getQualifiedNameString().equals(decl.getQualifiedNameString())) {
hdr = that;
}
}
}
;
ClassVisitor v = new ClassVisitor();
v.visit(currentCompilationUnit);
return v.hdr;
}
use of com.redhat.ceylon.compiler.typechecker.tree.Visitor in project ceylon-compiler by ceylon.
the class CeylonDocTool method buildNodesMaps.
private void buildNodesMaps() {
for (final PhasedUnit pu : phasedUnits) {
CompilationUnit cu = pu.getCompilationUnit();
Walker.walkCompilationUnit(new Visitor() {
public void visit(Tree.Declaration that) {
modelUnitMap.put(that.getDeclarationModel(), pu);
modelNodeMap.put(that.getDeclarationModel(), that);
super.visit(that);
}
public void visit(Tree.ObjectDefinition that) {
if (that.getDeclarationModel() != null && that.getDeclarationModel().getTypeDeclaration() != null) {
TypeDeclaration typeDecl = that.getDeclarationModel().getTypeDeclaration();
modelUnitMap.put(typeDecl, pu);
modelNodeMap.put(typeDecl, that);
}
super.visit(that);
}
public void visit(Tree.PackageDescriptor that) {
if (that.getImportPath() != null && that.getImportPath().getModel() != null) {
Referenceable model = that.getImportPath().getModel();
modelUnitMap.put(model, pu);
modelNodeMap.put(model, that);
}
super.visit(that);
}
public void visit(Tree.ModuleDescriptor that) {
if (that.getImportPath() != null && that.getImportPath().getModel() != null) {
Referenceable model = that.getImportPath().getModel();
modelUnitMap.put(model, pu);
modelNodeMap.put(model, that);
}
super.visit(that);
}
public void visit(Tree.SpecifierStatement that) {
modelUnitMap.put(that.getDeclaration(), pu);
modelNodeMap.put(that.getDeclaration(), that);
super.visit(that);
}
public void visit(Tree.Parameter param) {
parameterUnitMap.put(param.getParameterModel(), pu);
parameterNodeMap.put(param.getParameterModel(), param);
super.visit(param);
}
}, cu);
}
}
use of com.redhat.ceylon.compiler.typechecker.tree.Visitor in project ceylon-compiler by ceylon.
the class CeylonDocTool method warningMissingThrows.
protected void warningMissingThrows(Declaration d) {
if (ignoreMissingThrows) {
return;
}
final Scope scope = d.getScope();
final PhasedUnit unit = getUnit(d);
final Node node = getNode(d);
if (scope == null || unit == null || unit.getUnit() == null || node == null || !(d instanceof FunctionOrValue)) {
return;
}
List<Type> documentedExceptions = new ArrayList<Type>();
for (Annotation annotation : d.getAnnotations()) {
if (annotation.getName().equals("throws")) {
String exceptionName = annotation.getPositionalArguments().get(0);
Declaration exceptionDecl = scope.getMemberOrParameter(unit.getUnit(), exceptionName, null, false);
if (exceptionDecl instanceof TypeDeclaration) {
documentedExceptions.add(((TypeDeclaration) exceptionDecl).getType());
}
}
}
final List<Type> thrownExceptions = new ArrayList<Type>();
node.visitChildren(new Visitor() {
@Override
public void visit(Tree.Throw that) {
Expression expression = that.getExpression();
if (expression != null) {
thrownExceptions.add(expression.getTypeModel());
} else {
thrownExceptions.add(unit.getUnit().getExceptionType());
}
}
@Override
public void visit(Tree.Declaration that) {
// the end of searching
}
});
for (Type thrownException : thrownExceptions) {
boolean isDocumented = false;
for (Type documentedException : documentedExceptions) {
if (thrownException.isSubtypeOf(documentedException)) {
isDocumented = true;
break;
}
}
if (!isDocumented) {
log.warning(CeylondMessages.msg("warn.missingThrows", thrownException.asString(), getWhere(d), getPosition(getNode(d))));
}
}
}
Aggregations