1 /* 2 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 package jdk.test.lib.jittester.factories; 25 26 import java.util.ArrayList; 27 import java.util.Collection; 28 import java.util.List; 29 30 import jdk.test.lib.jittester.ProductionFailedException; 31 import jdk.test.lib.jittester.Symbol; 32 import jdk.test.lib.jittester.SymbolTable; 33 import jdk.test.lib.jittester.Type; 34 import jdk.test.lib.jittester.TypeList; 35 import jdk.test.lib.jittester.VariableInfo; 36 import jdk.test.lib.jittester.functions.ArgumentDeclaration; 37 import jdk.test.lib.jittester.functions.FunctionDeclaration; 38 import jdk.test.lib.jittester.functions.FunctionDefinition; 39 import jdk.test.lib.jittester.functions.FunctionInfo; 40 import jdk.test.lib.jittester.types.TypeKlass; 41 import jdk.test.lib.jittester.utils.PseudoRandom; 42 43 class FunctionDeclarationFactory extends Factory<FunctionDeclaration> { 44 private final Type resultType; 45 private final TypeKlass ownerClass; 46 private final String name; 47 private final int memberFunctionsArgLimit; 48 private final int flags; 49 50 FunctionDeclarationFactory(String name, TypeKlass ownerClass, Type resultType, 51 int memberFunctionsArgLimit, int flags) { 52 this.name = name; 53 this.ownerClass = ownerClass; 54 this.resultType = resultType; 55 this.memberFunctionsArgLimit = memberFunctionsArgLimit; 56 this.flags = flags; 57 } 58 59 @Override 60 public FunctionDeclaration produce() throws ProductionFailedException { 61 Type resType = resultType; 62 if (resType == null) { 63 List<Type> types = new ArrayList<>(TypeList.getAll()); 64 types.add(TypeList.VOID); 65 resType = PseudoRandom.randomElement(types); 66 } 67 int argNumber = (int) (PseudoRandom.random() * memberFunctionsArgLimit); 68 ArrayList<VariableInfo> argumentsInfo = new ArrayList<>(argNumber + 1); 69 argumentsInfo.add(new VariableInfo("this", ownerClass, ownerClass, 70 VariableInfo.FINAL | VariableInfo.LOCAL | VariableInfo.INITIALIZED)); 71 ArrayList<ArgumentDeclaration> argumentsDeclaration = new ArrayList<>(argNumber); 72 SymbolTable.push(); 73 FunctionInfo functionInfo; 74 IRNodeBuilder builder = new IRNodeBuilder().setArgumentType(ownerClass); 75 try { 76 int i = 0; 77 for (; i < argNumber; i++) { 78 ArgumentDeclaration d = builder.setVariableNumber(i) 79 .getArgumentDeclarationFactory().produce(); 80 argumentsDeclaration.add(d); 81 argumentsInfo.add(d.variableInfo); 82 } 83 Collection<Symbol> thisKlassFuncs = SymbolTable 84 .getAllCombined(ownerClass, FunctionInfo.class); 85 Collection<Symbol> parentFuncs = FunctionDefinition.getFuncsFromParents(ownerClass); 86 while (true) { 87 functionInfo = new FunctionInfo(name, ownerClass, resType, 0, flags, argumentsInfo); 88 if (thisKlassFuncs.contains(functionInfo) 89 || FunctionDefinition.isInvalidOverride(functionInfo, parentFuncs)) { 90 // try changing the signature, and go checking again. 91 ArgumentDeclaration d = builder.setVariableNumber(i++) 92 .getArgumentDeclarationFactory().produce(); 93 argumentsDeclaration.add(d); 94 argumentsInfo.add(d.variableInfo); 95 } else { 96 break; 97 } 98 } 99 } finally { 100 SymbolTable.pop(); 101 } 102 //addChildren(argumentsDeclaration); // not neccessary while complexity is 0 103 functionInfo = new FunctionInfo(name, ownerClass, resType, 0, flags, argumentsInfo); 104 // If it's all ok, add the function to the symbol table. 105 SymbolTable.add(functionInfo); 106 return new FunctionDeclaration(functionInfo, argumentsDeclaration); 107 } 108 }