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.IRNode;
  31 import jdk.test.lib.jittester.ProductionFailedException;
  32 import jdk.test.lib.jittester.Symbol;
  33 import jdk.test.lib.jittester.SymbolTable;
  34 import jdk.test.lib.jittester.Type;
  35 import jdk.test.lib.jittester.TypeList;
  36 import jdk.test.lib.jittester.VariableInfo;
  37 import jdk.test.lib.jittester.functions.ArgumentDeclaration;
  38 import jdk.test.lib.jittester.functions.FunctionDeclaration;
  39 import jdk.test.lib.jittester.functions.FunctionDefinition;
  40 import jdk.test.lib.jittester.functions.FunctionInfo;
  41 import jdk.test.lib.jittester.types.TypeKlass;
  42 import jdk.test.lib.jittester.types.TypeVoid;
  43 import jdk.test.lib.jittester.utils.PseudoRandom;
  44 
  45 class FunctionDeclarationFactory extends Factory {
  46     private final Type resultType;
  47     private final TypeKlass ownerClass;
  48     private final String name;
  49     private final int memberFunctionsArgLimit;
  50     private final int flags;
  51 
  52     FunctionDeclarationFactory(String name, TypeKlass ownerClass, Type resultType,
  53             int memberFunctionsArgLimit, int flags) {
  54         this.name = name;
  55         this.ownerClass = ownerClass;
  56         this.resultType = resultType;
  57         this.memberFunctionsArgLimit = memberFunctionsArgLimit;
  58         this.flags = flags;
  59     }
  60 
  61     @Override
  62     public IRNode produce() throws ProductionFailedException {
  63         Type resType = resultType;
  64         if (resType == null) {
  65             List<Type> types = new ArrayList<>(TypeList.getAll());
  66             types.add(new TypeVoid());
  67             resType = PseudoRandom.randomElement(types);
  68         }
  69         int argNumber = (int) (PseudoRandom.random() * memberFunctionsArgLimit);
  70         ArrayList<VariableInfo> argumentsInfo = new ArrayList<>(argNumber + 1);
  71         argumentsInfo.add(new VariableInfo("this", ownerClass, ownerClass,
  72                 VariableInfo.FINAL | VariableInfo.LOCAL | VariableInfo.INITIALIZED));
  73         ArrayList<ArgumentDeclaration> argumentsDeclaration = new ArrayList<>(argNumber);
  74         SymbolTable.push();
  75         FunctionInfo functionInfo;
  76         IRNodeBuilder builder = new IRNodeBuilder().setArgumentType(ownerClass);
  77         try {
  78             int i = 0;
  79             for (; i < argNumber; i++) {
  80                 ArgumentDeclaration d = builder.setVariableNumber(i)
  81                         .getArgumentDeclarationFactory().produce();
  82                 argumentsDeclaration.add(d);
  83                 argumentsInfo.add(d.variableInfo);
  84             }
  85             Collection<Symbol> thisKlassFuncs = SymbolTable
  86                     .getAllCombined(ownerClass, FunctionInfo.class);
  87             Collection<Symbol> parentFuncs = FunctionDefinition.getFuncsFromParents(ownerClass);
  88             while (true) {
  89                 functionInfo = new FunctionInfo(name, ownerClass, resType, 0, flags, argumentsInfo);
  90                 if (thisKlassFuncs.contains(functionInfo)
  91                         || FunctionDefinition.isInvalidOverride(functionInfo, parentFuncs)) {
  92                     // try changing the signature, and go checking again.
  93                     ArgumentDeclaration d = builder.setVariableNumber(i++)
  94                             .getArgumentDeclarationFactory().produce();
  95                     argumentsDeclaration.add(d);
  96                     argumentsInfo.add(d.variableInfo);
  97                 } else {
  98                     break;
  99                 }
 100             }
 101         } finally {
 102             SymbolTable.pop();
 103         }
 104         //addChildren(argumentsDeclaration); // not neccessary while complexity is 0
 105         functionInfo = new FunctionInfo(name, ownerClass, resType, 0, flags, argumentsInfo);
 106         // If it's all ok, add the function to the symbol table.
 107         SymbolTable.add(functionInfo);
 108         return new FunctionDeclaration(functionInfo, argumentsDeclaration);
 109     }
 110 }