1 /*
2 * Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved.
3 * @LastModified: Oct 2017
4 */
5 /*
6 * Licensed to the Apache Software Foundation (ASF) under one or more
7 * contributor license agreements. See the NOTICE file distributed with
8 * this work for additional information regarding copyright ownership.
9 * The ASF licenses this file to You under the Apache License, Version 2.0
10 * (the "License"); you may not use this file except in compliance with
11 * the License. You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 */
21
22 package com.sun.org.apache.xalan.internal.xsltc.compiler;
23
426 }
427 }
428 }
429
430 /**
431 * Type check a call to a standard function. Insert CastExprs when needed.
432 * If as a result of the insertion of a CastExpr a type check error is
433 * thrown, then catch it and re-throw it with a new "this".
434 */
435 public Type typeCheckStandard(SymbolTable stable) throws TypeCheckError {
436 _fname.clearNamespace(); // HACK!!!
437
438 final int n = _arguments.size();
439 final List<Type> argsType = typeCheckArgs(stable);
440 final MethodType args = new MethodType(Type.Void, argsType);
441 final MethodType ptype =
442 lookupPrimop(stable, _fname.getLocalPart(), args);
443
444 if (ptype != null) {
445 for (int i = 0; i < n; i++) {
446 final Type argType = (Type) ptype.argsType().get(i);
447 final Expression exp = _arguments.get(i);
448 if (!argType.identicalTo(exp.getType())) {
449 try {
450 _arguments.set(i, new CastExpr(exp, argType));
451 }
452 catch (TypeCheckError e) {
453 throw new TypeCheckError(this); // invalid conversion
454 }
455 }
456 }
457 _chosenMethodType = ptype;
458 return _type = ptype.resultType();
459 }
460 throw new TypeCheckError(this);
461 }
462
463
464
465 public Type typeCheckConstructor(SymbolTable stable) throws TypeCheckError{
466 final List<Constructor<?>> constructors = findConstructors();
540 int nArgs = _arguments.size();
541 final String name = _fname.getLocalPart();
542
543 // check if function is a contructor 'new'
544 if (_fname.getLocalPart().equals("new")) {
545 return typeCheckConstructor(stable);
546 }
547 // check if we are calling an instance method
548 else {
549 boolean hasThisArgument = false;
550
551 if (nArgs == 0)
552 _isStatic = true;
553
554 if (!_isStatic) {
555 if (_namespace_format == NAMESPACE_FORMAT_JAVA
556 || _namespace_format == NAMESPACE_FORMAT_PACKAGE)
557 hasThisArgument = true;
558
559 Expression firstArg = _arguments.get(0);
560 Type firstArgType = (Type)firstArg.typeCheck(stable);
561
562 if (_namespace_format == NAMESPACE_FORMAT_CLASS
563 && firstArgType instanceof ObjectType
564 && _clazz != null
565 && _clazz.isAssignableFrom(((ObjectType)firstArgType).getJavaClass()))
566 hasThisArgument = true;
567
568 if (hasThisArgument) {
569 _thisArgument = _arguments.get(0);
570 _arguments.remove(0); nArgs--;
571 if (firstArgType instanceof ObjectType) {
572 _className = ((ObjectType) firstArgType).getJavaClassName();
573 }
574 else
575 throw new TypeCheckError(ErrorMsg.NO_JAVA_FUNCT_THIS_REF, name);
576 }
577 }
578 else if (_className.length() == 0) {
579 /*
580 * Warn user if external function could not be resolved.
591 return _type = Type.Int; // use "Int" as "unknown"
592 }
593 }
594
595 final List<Method> methods = findMethods();
596
597 if (methods == null) {
598 // Method not found in this class
599 throw new TypeCheckError(ErrorMsg.METHOD_NOT_FOUND_ERR, _className + "." + name);
600 }
601
602 Class<?> extType = null;
603 final int nMethods = methods.size();
604 final List<Type> argsType = typeCheckArgs(stable);
605
606 // Try all methods to identify the best fit
607 int bestMethodDistance = Integer.MAX_VALUE;
608 _type = null; // reset internal type
609 for (int j, i = 0; i < nMethods; i++) {
610 // Check if all paramteters to this method can be converted
611 final Method method = (Method)methods.get(i);
612 final Class<?>[] paramTypes = method.getParameterTypes();
613
614 int currMethodDistance = 0;
615 for (j = 0; j < nArgs; j++) {
616 // Convert from internal (translet) type to external (Java) type
617 extType = paramTypes[j];
618 final Type intType = argsType.get(j);
619 JavaType match = _internal2Java.maps(intType, new JavaType(extType, 0));
620 if (match != null) {
621 currMethodDistance += match.distance;
622 }
623 else {
624 // no mapping available
625 //
626 // Allow a Reference type to match any external (Java) type at
627 // the moment. The real type checking is performed at runtime.
628 if (intType instanceof ReferenceType) {
629 currMethodDistance += 1;
630 }
631 else if (intType instanceof ObjectType) {
|
1 /*
2 * Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved.
3 * @LastModified: Nov 2017
4 */
5 /*
6 * Licensed to the Apache Software Foundation (ASF) under one or more
7 * contributor license agreements. See the NOTICE file distributed with
8 * this work for additional information regarding copyright ownership.
9 * The ASF licenses this file to You under the Apache License, Version 2.0
10 * (the "License"); you may not use this file except in compliance with
11 * the License. You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 */
21
22 package com.sun.org.apache.xalan.internal.xsltc.compiler;
23
426 }
427 }
428 }
429
430 /**
431 * Type check a call to a standard function. Insert CastExprs when needed.
432 * If as a result of the insertion of a CastExpr a type check error is
433 * thrown, then catch it and re-throw it with a new "this".
434 */
435 public Type typeCheckStandard(SymbolTable stable) throws TypeCheckError {
436 _fname.clearNamespace(); // HACK!!!
437
438 final int n = _arguments.size();
439 final List<Type> argsType = typeCheckArgs(stable);
440 final MethodType args = new MethodType(Type.Void, argsType);
441 final MethodType ptype =
442 lookupPrimop(stable, _fname.getLocalPart(), args);
443
444 if (ptype != null) {
445 for (int i = 0; i < n; i++) {
446 final Type argType = ptype.argsType().get(i);
447 final Expression exp = _arguments.get(i);
448 if (!argType.identicalTo(exp.getType())) {
449 try {
450 _arguments.set(i, new CastExpr(exp, argType));
451 }
452 catch (TypeCheckError e) {
453 throw new TypeCheckError(this); // invalid conversion
454 }
455 }
456 }
457 _chosenMethodType = ptype;
458 return _type = ptype.resultType();
459 }
460 throw new TypeCheckError(this);
461 }
462
463
464
465 public Type typeCheckConstructor(SymbolTable stable) throws TypeCheckError{
466 final List<Constructor<?>> constructors = findConstructors();
540 int nArgs = _arguments.size();
541 final String name = _fname.getLocalPart();
542
543 // check if function is a contructor 'new'
544 if (_fname.getLocalPart().equals("new")) {
545 return typeCheckConstructor(stable);
546 }
547 // check if we are calling an instance method
548 else {
549 boolean hasThisArgument = false;
550
551 if (nArgs == 0)
552 _isStatic = true;
553
554 if (!_isStatic) {
555 if (_namespace_format == NAMESPACE_FORMAT_JAVA
556 || _namespace_format == NAMESPACE_FORMAT_PACKAGE)
557 hasThisArgument = true;
558
559 Expression firstArg = _arguments.get(0);
560 Type firstArgType = firstArg.typeCheck(stable);
561
562 if (_namespace_format == NAMESPACE_FORMAT_CLASS
563 && firstArgType instanceof ObjectType
564 && _clazz != null
565 && _clazz.isAssignableFrom(((ObjectType)firstArgType).getJavaClass()))
566 hasThisArgument = true;
567
568 if (hasThisArgument) {
569 _thisArgument = _arguments.get(0);
570 _arguments.remove(0); nArgs--;
571 if (firstArgType instanceof ObjectType) {
572 _className = ((ObjectType) firstArgType).getJavaClassName();
573 }
574 else
575 throw new TypeCheckError(ErrorMsg.NO_JAVA_FUNCT_THIS_REF, name);
576 }
577 }
578 else if (_className.length() == 0) {
579 /*
580 * Warn user if external function could not be resolved.
591 return _type = Type.Int; // use "Int" as "unknown"
592 }
593 }
594
595 final List<Method> methods = findMethods();
596
597 if (methods == null) {
598 // Method not found in this class
599 throw new TypeCheckError(ErrorMsg.METHOD_NOT_FOUND_ERR, _className + "." + name);
600 }
601
602 Class<?> extType = null;
603 final int nMethods = methods.size();
604 final List<Type> argsType = typeCheckArgs(stable);
605
606 // Try all methods to identify the best fit
607 int bestMethodDistance = Integer.MAX_VALUE;
608 _type = null; // reset internal type
609 for (int j, i = 0; i < nMethods; i++) {
610 // Check if all paramteters to this method can be converted
611 final Method method = methods.get(i);
612 final Class<?>[] paramTypes = method.getParameterTypes();
613
614 int currMethodDistance = 0;
615 for (j = 0; j < nArgs; j++) {
616 // Convert from internal (translet) type to external (Java) type
617 extType = paramTypes[j];
618 final Type intType = argsType.get(j);
619 JavaType match = _internal2Java.maps(intType, new JavaType(extType, 0));
620 if (match != null) {
621 currMethodDistance += match.distance;
622 }
623 else {
624 // no mapping available
625 //
626 // Allow a Reference type to match any external (Java) type at
627 // the moment. The real type checking is performed at runtime.
628 if (intType instanceof ReferenceType) {
629 currMethodDistance += 1;
630 }
631 else if (intType instanceof ObjectType) {
|