Mercurial > hg > truffle
diff graal/com.oracle.truffle.dsl.processor/src/com/oracle/truffle/dsl/processor/template/MethodSpec.java @ 10597:79041ab43660
Truffle-DSL: API-change: Renamed truffle.api.codegen to truffle.api.dsl for all projects and packages.
author | Christian Humer <christian.humer@gmail.com> |
---|---|
date | Mon, 01 Jul 2013 20:58:32 +0200 |
parents | |
children | 2b9fcffd6f36 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/graal/com.oracle.truffle.dsl.processor/src/com/oracle/truffle/dsl/processor/template/MethodSpec.java Mon Jul 01 20:58:32 2013 +0200 @@ -0,0 +1,228 @@ +/* + * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ +package com.oracle.truffle.dsl.processor.template; + +import java.util.*; + +import javax.lang.model.type.*; + +import com.oracle.truffle.dsl.processor.*; +import com.oracle.truffle.dsl.processor.node.NodeChildData.*; + +public class MethodSpec { + + private final List<TypeMirror> implicitRequiredTypes = new ArrayList<>(); + + private final ParameterSpec returnType; + private final List<ParameterSpec> optional = new ArrayList<>(); + private final List<ParameterSpec> required = new ArrayList<>(); + + private boolean variableRequiredArguments; + private List<TypeDef> typeDefinitions; + + public MethodSpec(ParameterSpec returnType) { + this.returnType = returnType; + } + + public void setVariableRequiredArguments(boolean variableArguments) { + this.variableRequiredArguments = variableArguments; + } + + public boolean isVariableRequiredArguments() { + return variableRequiredArguments; + } + + public void addImplicitRequiredType(TypeMirror type) { + this.implicitRequiredTypes.add(type); + } + + public void addOptional(ParameterSpec spec) { + optional.add(spec); + } + + public ParameterSpec addRequired(ParameterSpec spec) { + required.add(spec); + return spec; + } + + public List<TypeMirror> getImplicitRequiredTypes() { + return implicitRequiredTypes; + } + + public ParameterSpec getReturnType() { + return returnType; + } + + public List<ParameterSpec> getRequired() { + return required; + } + + public List<ParameterSpec> getOptional() { + return optional; + } + + public List<ParameterSpec> getAll() { + List<ParameterSpec> specs = new ArrayList<>(); + specs.add(getReturnType()); + specs.addAll(getOptional()); + specs.addAll(getRequired()); + return specs; + } + + public ParameterSpec findParameterSpec(String name) { + for (ParameterSpec spec : getAll()) { + if (spec.getName().equals(name)) { + return spec; + } + } + return null; + } + + public void applyTypeDefinitions(String prefix) { + this.typeDefinitions = createTypeDefinitions(prefix); + } + + private List<TypeDef> createTypeDefinitions(String prefix) { + List<TypeDef> typeDefs = new ArrayList<>(); + + int defIndex = 0; + for (ParameterSpec spec : getAll()) { + List<TypeMirror> allowedTypes = spec.getAllowedTypes(); + List<TypeMirror> types = spec.getAllowedTypes(); + if (types != null && allowedTypes.size() > 1) { + TypeDef foundDef = null; + for (TypeDef def : typeDefs) { + if (allowedTypes.equals(def.getTypes())) { + foundDef = def; + break; + } + } + if (foundDef == null) { + foundDef = new TypeDef(types, prefix + defIndex); + typeDefs.add(foundDef); + defIndex++; + } + + spec.setTypeDefinition(foundDef); + } + } + + return typeDefs; + } + + public String toSignatureString(String methodName) { + StringBuilder b = new StringBuilder(); + b.append(" "); + b.append(createTypeSignature(returnType, true)); + + b.append(" "); + b.append(methodName); + b.append("("); + + String sep = ""; + + for (ParameterSpec optionalSpec : getOptional()) { + b.append(sep); + b.append("["); + b.append(createTypeSignature(optionalSpec, false)); + b.append("]"); + sep = ", "; + } + + for (ParameterSpec requiredSpec : getRequired()) { + b.append(sep); + if (requiredSpec.getCardinality() == Cardinality.MANY) { + b.append("{"); + } + b.append(createTypeSignature(requiredSpec, false)); + if (requiredSpec.getCardinality() == Cardinality.MANY) { + b.append("}"); + } + sep = ", "; + } + + b.append(")"); + + if (typeDefinitions != null && !typeDefinitions.isEmpty()) { + b.append("\n\n"); + + String lineSep = ""; + for (TypeDef def : typeDefinitions) { + b.append(lineSep); + b.append(" <").append(def.getName()).append(">"); + b.append(" = {"); + String separator = ""; + for (TypeMirror type : def.getTypes()) { + b.append(separator).append(Utils.getSimpleName(type)); + separator = ", "; + } + b.append("}"); + lineSep = "\n"; + + } + } + return b.toString(); + } + + private static String createTypeSignature(ParameterSpec spec, boolean typeOnly) { + StringBuilder builder = new StringBuilder(); + TypeDef foundTypeDef = spec.getTypeDefinition(); + if (foundTypeDef != null) { + builder.append("<" + foundTypeDef.getName() + ">"); + } else if (spec.getAllowedTypes().size() >= 1) { + builder.append(Utils.getSimpleName(spec.getAllowedTypes().get(0))); + } else { + builder.append("void"); + } + if (!typeOnly) { + builder.append(" "); + builder.append(spec.getName()); + } + return builder.toString(); + } + + @Override + public String toString() { + return toSignatureString("methodName"); + } + + static class TypeDef { + + private final List<TypeMirror> types; + private final String name; + + public TypeDef(List<TypeMirror> types, String name) { + this.types = types; + this.name = name; + } + + public List<TypeMirror> getTypes() { + return types; + } + + public String getName() { + return name; + } + } + +}