1 /*
   2  * Copyright (c) 2019, 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package java.foreign.layout;
  26 
  27 import java.util.Arrays;
  28 import java.util.List;
  29 import java.util.Map;
  30 import java.util.Optional;
  31 import java.util.stream.Collectors;
  32 import java.util.stream.Stream;
  33 
  34 /**
  35  * A function is made up of zero or more argument layouts and one return layout. A function descriptor
  36  * is used to model the signature of native functions. Note: a function is not a layout - it's merely
  37  * an aggregate for one or more layouts; as such it does not have a size.
  38  */
  39 public final class Function extends AbstractDescriptor<Function> {
  40     private final Optional<Layout> resLayout;
  41     private final Layout[] argLayouts;
  42     private final boolean variadic;
  43 
  44     private Function(Map<String, String> annotations, Optional<Layout> resLayout, boolean variadic, Layout... argLayouts) {
  45         super(annotations);
  46         this.resLayout = resLayout;
  47         this.variadic = variadic;
  48         this.argLayouts = argLayouts;
  49     }
  50 
  51     /**
  52      * Returns the return layout associated with this function.
  53      * @return the return layout.
  54      */
  55     public Optional<Layout> returnLayout() {
  56         return resLayout;
  57     }
  58 
  59     /**
  60      * Returns the argument layouts associated with this function.
  61      * @return the argument layouts.
  62      */
  63     public List<Layout> argumentLayouts() {
  64         return Arrays.asList(argLayouts);
  65     }
  66 
  67     /**
  68      * Does this function accept a variable-arity argument list?
  69      * @return true, if the function models a variadic function.
  70      */
  71     public boolean isVariadic() {
  72         return variadic;
  73     }
  74 
  75     /**
  76      * Does this function contain unresolved layouts?
  77      * @return true if this function contains (possibly nested) unresolved layouts.
  78      */
  79     public boolean isPartial() {
  80         return Stream.concat(resLayout.stream(), Stream.of(argLayouts))
  81                 .anyMatch(Layout::isPartial);
  82     }
  83 
  84     /**
  85      * Create a function descriptor with given return and argument layouts.
  86      * @param varargs is this a variadic function
  87      * @param resLayout the return layout.
  88      * @param argLayouts the argument layouts.
  89      * @return the new function descriptor.
  90      */
  91     public static Function of(Layout resLayout, boolean varargs, Layout... argLayouts) {
  92         return new Function(NO_ANNOS, Optional.of(resLayout), varargs, argLayouts);
  93     }
  94 
  95     /**
  96      * Create a void function descriptor with given argument layouts.
  97      * @param varargs is this a variadic function
  98      * @param argLayouts the argument layouts.
  99      * @return the new function descriptor.
 100      */
 101     public static Function ofVoid(boolean varargs, Layout... argLayouts) {
 102         return new Function(NO_ANNOS, Optional.empty(), varargs, argLayouts);
 103     }
 104 
 105     @Override
 106     public String toString() {
 107         return String.format("(%s%s)%s",
 108                 Stream.of(argLayouts)
 109                         .map(Object::toString)
 110                         .collect(Collectors.joining()),
 111                 variadic ? "*" : "",
 112                 resLayout.map(Object::toString).orElse("v"));
 113     }
 114 
 115     @Override
 116     public boolean equals(Object other) {
 117         if (this == other) {
 118             return true;
 119         }
 120         if (!super.equals(other)) {
 121             return false;
 122         }
 123         if (!(other instanceof Function)) {
 124             return false;
 125         }
 126         Function f = (Function)other;
 127         return resLayout.equals(f.resLayout) && Arrays.equals(argLayouts, f.argLayouts) &&
 128             variadic == f.variadic;
 129     }
 130 
 131     @Override
 132     public int hashCode() {
 133         return super.hashCode() ^ resLayout.hashCode() ^ Arrays.hashCode(argLayouts) ^ Boolean.hashCode(variadic);
 134     }
 135 
 136     @Override
 137     Function withAnnotations(Map<String, String> annotations) {
 138         return new Function(annotations, resLayout, variadic, argLayouts);
 139     }
 140 }