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