1 /*
   2  * Copyright (c) 2005, 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.functions;
  25 
  26 import java.util.ArrayList;
  27 import java.util.Collection;
  28 import java.util.LinkedList;
  29 import jdk.test.lib.jittester.IRNode;
  30 import jdk.test.lib.jittester.Symbol;
  31 import jdk.test.lib.jittester.SymbolTable;
  32 import jdk.test.lib.jittester.types.TypeKlass;
  33 import jdk.test.lib.jittester.visitors.Visitor;
  34 
  35 public class FunctionDefinition extends IRNode {
  36     private final FunctionInfo functionInfo;
  37 
  38     public FunctionDefinition(FunctionInfo functionInfo,
  39             ArrayList<ArgumentDeclaration> argumentsDeclaration, IRNode body, IRNode ret) {
  40         this.functionInfo = functionInfo;
  41         addChild(body);
  42         addChild(ret);
  43         addChildren(argumentsDeclaration);
  44     }
  45 
  46     // get the list of all functions from all parents of the given class.
  47     public static Collection<Symbol> getFuncsFromParents(TypeKlass typeKlass) {
  48         LinkedList<Symbol> result = new LinkedList<>();
  49         for (TypeKlass parent : typeKlass.getAllParents()) {
  50             result.addAll(SymbolTable.getAllCombined(parent, FunctionInfo.class));
  51         }
  52         return result;
  53     }
  54 
  55     // Check if the given function prototype f1 is a valid overload of
  56     // prototypes in collection S.
  57     // The override is invalid if function f1 has the same signature as
  58     // function f2 in S, but has different return type.
  59     public static boolean isInvalidOverride(FunctionInfo f1, Collection<Symbol> symbols) {
  60         for (Symbol symbol : symbols) {
  61             FunctionInfo f2 = (FunctionInfo) symbol;
  62             if (f1.hasEqualSignature(f2)) {
  63                 if (!f1.type.equals(f2.type)) {
  64                     return true;
  65                 }
  66                 if ((f2.flags & FunctionInfo.NONRECURSIVE) > 0
  67                         || ((f1.flags & FunctionInfo.ABSTRACT) > 0 && (f2.flags & FunctionInfo.ABSTRACT) == 0)
  68                         || (f1.flags & FunctionInfo.STATIC) != (f2.flags & FunctionInfo.STATIC)
  69                         || (f2.flags & FunctionInfo.FINAL) > 0
  70                         || (f1.flags & FunctionInfo.ACCESS_ATTRS_MASK) < (f2.flags & FunctionInfo.ACCESS_ATTRS_MASK)) {
  71                     return true;
  72                 }
  73             }
  74         }
  75         return false;
  76     }
  77 
  78     @Override
  79     public long complexity() {
  80         IRNode body = getChild(0);
  81         IRNode ret = getChild(1);
  82         return body.complexity() + (ret != null ? ret.complexity() : 0);
  83     }
  84 
  85     @Override
  86     public<T> T accept(Visitor<T> v) {
  87         return v.visit(this);
  88     }
  89 
  90     public FunctionInfo getFunctionInfo() {
  91         return functionInfo;
  92     }
  93 }