/** Find a function that matches a function call */ public ScopeSymbol findFunction(String functionName, Args args) { // Retrieve all functions with the same name List<ScopeSymbol> ssfuncs = getFunctions(functionName); // Find best matching function... ScopeSymbol bestSsfunc = null; int bestScore = Integer.MAX_VALUE; for (ScopeSymbol ssfunc : ssfuncs) { boolean ok = false; int score = 0; TypeFunc sstype = (TypeFunc) ssfunc.getType(); // Find the ones with the same number of parameters int argc = args.size(); if (argc == sstype.getParameters().size()) { ok = true; // Find the ones with matching exact parameters for (int i = 0; i < args.size(); i++) { Type argType = args.getArguments()[i].getReturnType(); Type funcType = sstype.getParameters().getType(i); // Same argument? if ((argType != null) && !argType.equals(funcType)) { // Can we cast? if (argType.canCast(funcType)) score++; // Add a point if we can cast else ok = false; } } } // Found anything? if (ok) { // Perfect match? Don't look any further if (score == 0) return ssfunc; // Get the one with less argument casts if (score < bestScore) { bestScore = score; bestSsfunc = ssfunc; } } } return bestSsfunc; }
/** Class scope */ public static Scope getClassScope(Type type) { if (type == null) return null; return classScope.getOrCreate(type.toString()); }