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
24package jdk.test.lib.jittester.factories;
25
26import java.util.Iterator;
27import java.util.LinkedList;
28import jdk.test.lib.jittester.IRNode;
29import jdk.test.lib.jittester.ProductionFailedException;
30import jdk.test.lib.jittester.ProductionParams;
31import jdk.test.lib.jittester.Symbol;
32import jdk.test.lib.jittester.SymbolTable;
33import jdk.test.lib.jittester.Type;
34import jdk.test.lib.jittester.TypeList;
35import jdk.test.lib.jittester.classes.Interface;
36import jdk.test.lib.jittester.functions.FunctionInfo;
37import jdk.test.lib.jittester.types.TypeKlass;
38import jdk.test.lib.jittester.utils.PseudoRandom;
39
40class InterfaceFactory extends Factory<Interface> {
41    private final String name;
42    private final int memberFunctionsLimit;
43    private final int memberFunctionsArgLimit;
44    private final int level;
45    private TypeKlass parent = null;
46
47    InterfaceFactory(String name, int memberFunctionsLimit, int memberFuncArgLimit, int lvl) {
48        this.name = name;
49        this.memberFunctionsLimit = memberFunctionsLimit;
50        this.memberFunctionsArgLimit = memberFuncArgLimit;
51        this.level = lvl;
52    }
53
54    @Override
55    public Interface produce() throws ProductionFailedException {
56        TypeKlass thisKlass;
57        // Do we want to inherit something?
58        if (!ProductionParams.disableInheritance.value()) {
59            // Grab all Klasses from the TypeList and select one to be a parent
60            LinkedList<Type> types = new LinkedList<>(TypeList.getAll());
61            for (Iterator<Type> i = types.iterator(); i.hasNext();) {
62                Type klass = i.next();
63                if (!(klass instanceof TypeKlass) || !((TypeKlass) klass).isInterface()) {
64                    i.remove();
65                }
66            }
67            PseudoRandom.shuffle(types);
68            if (!types.isEmpty()) {
69                parent = (TypeKlass) types.getFirst();
70            }
71        }
72        thisKlass = new TypeKlass(name, TypeKlass.INTERFACE);
73        if (parent != null) {
74            thisKlass.addParent(parent.getName());
75            thisKlass.setParent(parent);
76            parent.addChild(name);
77            for (Symbol symbol : SymbolTable.getAllCombined(parent, FunctionInfo.class)) {
78                FunctionInfo functionInfo = (FunctionInfo) symbol.deepCopy();
79                functionInfo.owner = thisKlass;
80                functionInfo.argTypes.get(0).type = thisKlass;
81                SymbolTable.add(functionInfo);
82            }
83        }
84        IRNode functionDeclarations = null;
85        try {
86            functionDeclarations = new IRNodeBuilder().setOwnerKlass(thisKlass)
87                    .setMemberFunctionsLimit(memberFunctionsLimit)
88                    .setMemberFunctionsArgLimit(memberFunctionsArgLimit)
89                    .setLevel(level + 1)
90                    .getFunctionDeclarationBlockFactory()
91                    .produce();
92        } finally {
93            TypeList.add(thisKlass);
94        }
95        return new Interface(parent, name, level, functionDeclarations);
96    }
97}
98