home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / fchek284.zip / exprtype.c < prev    next >
C/C++ Source or Header  |  1994-10-10  |  41KB  |  1,477 lines

  1. /* exprtype.c -- propagates datatype thru expressions.
  2.  
  3.     Copyright (C) 1992 by Robert K. Moniot.
  4.     This program is free software.  Permission is granted to
  5.     modify it and/or redistribute it, retaining this notice.
  6.     No guarantees accompany this software.
  7.  
  8.  
  9. */
  10.  
  11. /* I. */
  12.  
  13. /*  exprtype.c:
  14.  
  15.     Routines to propagate datatype through expressions.
  16.  
  17.     binexpr_type()        Yields result type of binary expression.
  18.     unexpr_type()        Yields result type of unary expression.
  19.     assignment_stmt_type()    Checks assignment statement type.
  20.     func_ref_expr(id,args,result) Forms token for a function invocation.
  21.     primary_id_expr()    Forms token for primary which is an identifier.
  22.     stmt_fun_arg_cmp(t1,t2) Checks agreement between stmt func args.
  23.     int    int_power(x,n)        Computes x**n for value propagation.
  24.         init_typesizes(wdsize)    Sets standard type sizes
  25. */
  26.  
  27. #include <stdio.h>
  28. #include <string.h>
  29. #include <ctype.h>
  30. #include "ftnchek.h"
  31. #define EXPRTYPE
  32. #include "symtab.h"
  33. #include "tokdefs.h"
  34.  
  35. PRIVATE int eval_intrins();
  36. PRIVATE int int_power();
  37. PRIVATE char *sized_typename();
  38. PRIVATE void report_mismatch(),report_type();
  39.  
  40. extern int in_assignment_stmt;    /* shared with fortran.y */
  41.  
  42. #define max(x,y) ((y)>(x)?(y):(x))
  43.  
  44.     /* shorthand for datatypes.  must match those in symtab.h */
  45.     /* N.B. Also, the fact that type_DEFAULT=0 is assumed in size
  46.        propagation code. */
  47. #define E 0    /*  Error for invalid type combos  */
  48. #define I 1
  49. #define R 2
  50. #define D 3
  51. #define C 4
  52. #define Z 5
  53. #define L 6
  54. #define S 7
  55. #define H 8
  56. #define NumT (H+1)        /* number of types in tables below */
  57.  
  58. #define W 10        /*  Warning for nonstandard type combos: W>NumT */
  59.  
  60.             /* for  + - / * **    ANSI book pp. 6-5,6-6    */
  61.                 /* Mixed double+complex = double complex with
  62.                    warning, double + double complex is OK */
  63. PRIVATE unsigned char arith_expr_type[NumT][NumT]={
  64. /*E   I   R   D   C   Z   L   S   H   */
  65. { E,  E,  E,  E,  E,  E,  E,  E,  E },    /* E */
  66. { E,  I,  R,  D,  C,  Z,  E,  E,  E },    /* I */
  67. { E,  R,  R,  D,  C,  Z,  E,  E,  E },    /* R */
  68. { E,  D,  D,  D,W+Z,  Z,  E,  E,  E },    /* D */
  69. { E,  C,  C,W+Z,  C,  Z,  E,  E,  E },    /* C */
  70. { E,  Z,  Z,  Z,  Z,  Z,  E,  E,  E },    /* Z */
  71. { E,  E,  E,  E,  E,  E,  E,  E,  E },    /* L */
  72. { E,  E,  E,  E,  E,  E,  E,  E,  E },    /* S */
  73. { E,  E,  E,  E,  E,  E,  E,  E,  E }    /* H */
  74. };
  75.  
  76.             /* for  relops.  Corresponds to arith type table
  77.                except that nonstandard comparisons of like
  78.                types have warning, not error. */
  79. PRIVATE unsigned char rel_expr_type[NumT][NumT]={
  80. /*E   I   R   D   C   Z   L   S   H   */
  81. { E,  E,  E,  E,  E,  E,  E,  E,  E },    /* E */
  82. { E,  L,  L,  L,  L,  L,  E,  E,W+L },    /* I */
  83. { E,  L,  L,  L,  L,  L,  E,  E,  E },    /* R */
  84. { E,  L,  L,  L,W+L,  L,  E,  E,  E },    /* D */
  85. { E,  L,  L,W+L,  L,  L,  E,  E,  E },    /* C */
  86. { E,  L,  L,  L,  L,  L,  E,  E,  E },    /* Z */
  87. { E,  E,  E,  E,  E,  E,W+L,  E,W+L },    /* L */
  88. { E,  E,  E,  E,  E,  E,  E,  L,  E },    /* S */
  89. { E,W+L,  E,  E,  E,  E,W+L,  E,W+L }    /* H */
  90. };
  91.  
  92.             /* Result of assignment:  lvalue = expr.  Here rows
  93.                correspond to type of lvalue, columns to type
  94.                of expr */
  95. PRIVATE unsigned char assignment_type[NumT][NumT]={
  96. /*E   I   R   D   C   Z   L   S   H   */
  97. { E,  E,  E,  E,  E,  E,  E,  E,  E },    /* E */
  98. { E,  I,  I,  I,  I,  I,  E,  E,W+I },    /* I */
  99. { E,  R,  R,  R,  R,  R,  E,  E,W+R },    /* R */
  100. { E,  D,  D,  D,  D,  D,  E,  E,W+D },    /* D */
  101. { E,  C,  C,  C,  C,  C,  E,  E,W+C },    /* C */
  102. { E,  Z,  Z,  Z,  Z,  Z,  E,  E,W+Z },    /* Z */
  103. { E,  E,  E,  E,  E,  E,  L,  E,W+L },    /* L */
  104. { E,  E,  E,  E,  E,  E,  E,  S,  E },    /* S */
  105. { E,  E,  E,  E,  E,  E,  E,  E,  E }    /* H not possible for lvalue */
  106. };
  107.  
  108.  
  109. #define INTRINS_ARGS (op == ',') /* Flag to modify behavior of binexpr_type */
  110.  
  111.     /* Routine used in printing diagnostics: returns string "type" for
  112.        unsized objects, "type*size" for explicitly sized things.  Due
  113.        to use of local static variable, cannot be invoked twice in the
  114.        same expression.  */
  115. PRIVATE char*
  116. sized_typename(type,size)
  117.   int type; long size;
  118. {
  119.   static char strbuf[]="type*000000"; /* template */
  120.   static char *char_unk="char*(?)";
  121.   static char *char_adj="char*(*)";
  122.   if(size == size_DEFAULT) {
  123.     return type_name[type];    /* no explicit size */
  124.   }
  125.   else {
  126.     if(type != S || size > 0) {
  127.       (void)sprintf(strbuf,"%4s*%ld",    /* type*size */
  128.         type_name[type],
  129.         size%1000000);
  130.     }
  131.     else {            /* handle special character size codes */
  132.       if(size == size_ADJUSTABLE)
  133.     return char_adj;
  134.       else /*size_UNKNOWN*/
  135.     return char_unk;
  136.     }
  137.   }
  138.   return strbuf;
  139. }
  140.  
  141.  
  142. void
  143. init_typesizes()
  144.         /* Only executes once.  Thus cannot change wordsize
  145.            after processing starts. */
  146. {
  147.   static int trapdoor=FALSE;
  148.   if(trapdoor) {
  149.     if(given_wordsize != local_wordsize) {
  150.       (void)fprintf(stderr,
  151.           "\nSorry-Cannot change wordsize after processing starts");
  152.     }
  153.     given_wordsize = local_wordsize;
  154.   }
  155.   else {
  156.     trapdoor = TRUE;
  157.     local_wordsize = given_wordsize;
  158.     if(given_wordsize != 0) {
  159.       if(given_wordsize != BpW) {
  160.     type_size[I] = type_size[R] = type_size[L] = (BYTE)given_wordsize;
  161.     type_size[D] = type_size[C] = (BYTE)(2*given_wordsize);
  162.     type_size[Z] = (BYTE)(4*given_wordsize);
  163.       }
  164.     }
  165.   }
  166. }
  167.  
  168.     /* this routine propagates type in binary expressions */
  169.  
  170. void
  171. binexpr_type(term1,operator,term2,result)
  172.     Token *term1, *operator, *term2, *result;
  173. {
  174.     int    op = operator->class,
  175.     type1 = datatype_of(term1->TOK_type),
  176.     type2 = datatype_of(term2->TOK_type),
  177.     result_type;
  178.     long
  179.     size1 = term1->size,
  180.     size2 = term2->size,
  181.         result_size;
  182.     if( ! is_computational_type(type1) ) {
  183.         syntax_error(term1->line_num,term1->col_num,
  184.             "noncomputational primary in expression:");
  185.         report_type(term1);
  186.         result_type = E;
  187.     }
  188.     else if( ! is_computational_type(type2) ) {
  189.         syntax_error(term2->line_num,term2->col_num,
  190.             "noncomputational primary in expression:");
  191.         report_type(term2);
  192.         result_type = E;
  193.     }
  194.     else {
  195.     switch(op) {
  196.                 /* arithmetic operators: use lookup table */
  197.         case '+':
  198.         case '-':
  199.         case '*':
  200.         case '/':
  201.         case tok_power:
  202.         result_type = (unsigned)arith_expr_type[type1][type2];
  203.         break;
  204.  
  205.                 /* relational operators: use lookup table */
  206.          case tok_relop:
  207.         result_type = (unsigned)rel_expr_type[type1][type2];
  208.         break;
  209.  
  210.                 /*  logical operators: operands should be
  211.                     logical, but allow integers with a
  212.                     warning. */
  213.         case tok_AND:
  214.         case tok_OR:
  215.         case tok_EQV:
  216.         case tok_NEQV:
  217.         if(type1 == L && type2 == L)
  218.             result_type = L;
  219.         else if(type1 == I && type2 == I)
  220.             result_type = W+I;
  221.         else
  222.             result_type = E;
  223.         break;
  224.  
  225.                 /*  // operator: operands must be strings */
  226.         case tok_concat:
  227.         if(type1 == S && type2 == S)
  228.             result_type = S;
  229.         else
  230.             result_type = E;
  231.         break;
  232.  
  233.             /* Intrinsic function argument list: no promotion
  234.                across type categories.  Accept matching type
  235.                categories: size match will be checked later. */
  236.         case ',':
  237.         if( type_category[type1] != type_category[type2] )
  238.           result_type = E;
  239.         else if(type1 == S)
  240.           result_type = S;
  241.         else
  242.           result_type = (unsigned)arith_expr_type[type1][type2];
  243.         break;
  244.  
  245.         default:
  246.         oops_message(OOPS_NONFATAL,
  247.                  operator->line_num,operator->col_num,
  248.                  "operator unknown: type not propagated");
  249.         result_type = type1;
  250.         break;
  251.     }
  252.  
  253.     if( (type1 != E && type2 != E) ) {
  254.         if( result_type == E) {
  255.         syntax_error(operator->line_num,operator->col_num,
  256.             "type mismatch");
  257.         if(INTRINS_ARGS) {
  258.           msg_tail("between intrinsic function arguments:");
  259.         }
  260.         else {
  261.           msg_tail("in expression:");
  262.         }
  263.         report_mismatch(term1,operator,term2);
  264.         }
  265.         else if(result_type >= W) {    /* W result */
  266.           if(f77_standard) {
  267.         warning(operator->line_num,operator->col_num,
  268.             "nonstandard type combination in expression:");
  269.         report_mismatch(term1,operator,term2);
  270.           }
  271.           result_type -= W;
  272.         }
  273.                 /* Obscure standard rule */
  274.         else if(f77_standard && op == tok_concat && !in_assignment_stmt &&
  275.           (size1 == size_ADJUSTABLE || size2 == size_ADJUSTABLE) ) {
  276.         nonstandard(operator->line_num,operator->col_num);
  277.         msg_tail("adjustable size cannot be concatenated here");
  278.         }
  279.     }
  280.     }
  281.  
  282.                 /* Figure out the size of result */
  283.     result_size = size_DEFAULT;
  284.     if(result_type != E &&    /* Error type gets DEFAULT size */
  285.        op != tok_relop) {    /* Result of compare gets DEFAULT size */
  286.  
  287.       if(op == tok_concat) {    /* string//string yields sum of lengths */
  288.     if(size1 == size_UNKNOWN || size2 == size_UNKNOWN)
  289.       result_size = size_UNKNOWN;
  290.     else
  291.       if(size1 == size_ADJUSTABLE || size2 == size_ADJUSTABLE)
  292.         result_size = size_ADJUSTABLE;
  293.       else
  294.         result_size = size1 + size2;
  295.       }
  296.             /* DEFAULT op DEFAULT always yields DEFAULT. So need
  297.                to handle only explicitly sized expressions,
  298.                except intrinsic arglists, where no promotion
  299.                of plain real to dble or plain complex to dcpx,
  300.                and check for promotions of real types.
  301.              */
  302.       else if(INTRINS_ARGS?
  303.           (type1 != type2 || 
  304.            (type1 == type2  && is_numeric_type(type1) &&
  305.         (size1 != size_DEFAULT || size2 != size_DEFAULT))) :
  306.           ((size1 != size_DEFAULT || size2 != size_DEFAULT) ||
  307.             (trunc_check && is_float_type(type1) && is_float_type(type2))))
  308.      {
  309.                 /* Local variables for convenience.
  310.                    N.B. Use tc1/2,ls1/2 for tests,
  311.                    t1/2,s1/2 for assigning result.
  312.                  */
  313.     int t1,t2;    /* sorted types: t1 <= t2. */
  314.     long s1,s2;    /* sizes of t1 and t2. */
  315.     int tc1,tc2;    /* type categories: D->R and Z->C */
  316.     long ls1,ls2;    /* local sizes = declared size else type_size */
  317.     int defsize1,defsize2; /* flags for default size */
  318.  
  319.                 /* Sort so that t1 <= t2 */
  320.     if(type1 <= type2) {
  321.       t1 = type1; s1 = size1;
  322.       t2 = type2; s2 = size2;
  323.     }
  324.     else {
  325.       t1 = type2; s1 = size2;
  326.       t2 = type1; s2 = size1;
  327.     }
  328.                 /* Assign type categories and local sizes */
  329.     tc1 = type_category[t1];
  330.     tc2 = type_category[t2];
  331.  
  332.     defsize1 = (s1 == size_DEFAULT);
  333.     defsize2 = (s2 == size_DEFAULT);
  334.     ls1 = (defsize1? type_size[t1]: s1);
  335.     ls2 = (defsize2? type_size[t2]: s2);
  336.  
  337. #ifdef DEBUG_EXPRTYPE
  338. if(debug_latest)
  339.   (void)fprintf(list_fd,"\nt1=%s s1=%d ls1=%d t2=%s s2=%d ls2=%d",
  340.       type_name[t1],s1,ls1, type_name[t2], s2, ls2);
  341. #endif
  342.     if(tc1 == tc2) {/* same type category */
  343.                 /* Intrins args: size promotion illegal */
  344.       if(INTRINS_ARGS && ls1 != ls2) {
  345.         syntax_error(operator->line_num,operator->col_num,
  346.              "precision mismatch in intrinsic argument list:");
  347.         report_mismatch(term1,operator,term2);
  348.       }
  349.                 /* Give -port warning if e.g. plain I+I*2
  350.                    (variables only) */
  351.       else if(port_check || local_wordsize==0) {
  352.         if(defsize1 != defsize2
  353.             && !is_true(CONST_EXPR,term1->TOK_flags)
  354.             && !is_true(CONST_EXPR,term2->TOK_flags))
  355.         {
  356.           nonportable(operator->line_num,operator->col_num,
  357.               INTRINS_ARGS?"intrinsic argument list":"expr");
  358.           msg_tail("mixes default and explicit");
  359.           msg_tail((is_numeric_type(t1)&&is_numeric_type(t2))?
  360.              "precision":"size");
  361.           msg_tail("operands:");
  362.           report_mismatch(term1,operator,term2);
  363.         }
  364.       }
  365.  
  366.         /* If same type category, use the larger of the two sizes if
  367.            both declared.  If only one size declared, use the
  368.            larger of the declared size and the default size.
  369.            If result is equal in size to default, use size_DEFAULT.
  370.         */
  371.       if(ls1 > ls2) {
  372.         result_size = s1;
  373.       }
  374.       else if(ls2 > ls1) {
  375.         result_size = s2;
  376.       }
  377.       else /*ls1 == ls2*/{
  378.         if(!defsize1 && !defsize2)
  379.           result_size = s1;    /* otherwise DEFAULT */
  380.       }
  381.     }/* end(tc1==tc2) */
  382.     else /* tc1!=tc2 */ {
  383.             /* Differing type categories: only two cases. */
  384.  
  385.                 /* Case 1:  I + R|D|C|Z
  386.                    Result: size of dominant type */
  387.       if(tc1 == I) {
  388.         result_size = s2;
  389.       }
  390.                 /* Case 2:  R|D + C|Z
  391.                    Result: larger of C|Z and 2*size of R|D */
  392.       else {
  393.         if(ls2 >= 2*ls1)
  394.           result_size = s2;
  395.         else
  396.           result_size = 2*s1; /* 2*size_DEFAULT = 0 is still DEFAULT */
  397.       }
  398.     }/* end tc1 != tc2 */
  399.                 /* change D or Z to default size or else
  400.                    to explicitly sized R or C
  401.                  */
  402.     if(result_type == D || result_type == Z) {
  403.       if(result_size != size_DEFAULT
  404.          && result_size != type_size[result_type])
  405.            result_type = (result_type==D)?R:C;
  406.          else
  407.            result_size = size_DEFAULT;
  408.     }
  409.  
  410.                 /* Give -trunc warning if a real or
  411.                    complex type is promoted to double. */
  412.     if(trunc_check && !INTRINS_ARGS && is_float_type(t1) ) {
  413.           /* First clause checks R+R size agreement */
  414.       if( (type_category[result_type] == R && ls1 != ls2)
  415.              /* Second clause checks R+C and C+C */
  416.          || (type_category[result_type] == C &&
  417.          (type_category[t1] == R? ls2 != 2*ls1 : ls2 != ls1)) ){
  418.         warning(operator->line_num,operator->col_num,
  419.             "promotion may not give desired precision:");
  420.         report_mismatch(term1,operator,term2);
  421.       }
  422.     }
  423.  
  424.       }/*end if(non-DEFAULT sizes)*/
  425.  
  426.     }/*end if(result_size != E)*/
  427.  
  428. #ifdef DEBUG_EXPRTYPE
  429. if(debug_latest) {
  430. (void)fprintf(list_fd,"\nsize of %s %c",sized_typename(type1,size1),
  431.     ispunct(op)?op:'~');
  432. (void)fprintf(list_fd," %s = ",sized_typename(type2,size2));
  433. (void)fprintf(list_fd,"%s",sized_typename(result_type,result_size));
  434. }
  435. #endif
  436.  
  437.     result->TOK_type = type_byte(class_VAR, result_type);
  438.     result->TOK_flags = 0;    /* clear all flags */
  439.     result->size = result_size;
  440.  
  441.  
  442.         /* Keep track of constant expressions */
  443.     if( is_true(CONST_EXPR,term1->TOK_flags)
  444.      && is_true(CONST_EXPR,term2->TOK_flags)
  445.          && !(op==tok_power && type2!=I) ) { /* exclude **REAL */
  446.         make_true(CONST_EXPR,result->TOK_flags);
  447.     }
  448.  
  449.         /* Parameter expressions are like constant exprs
  450.            except we bend the rules to allow intrinsic functions
  451.            and **REAL */
  452.     if( is_true(PARAMETER_EXPR,term1->TOK_flags)
  453.      && is_true(PARAMETER_EXPR,term2->TOK_flags) ) {
  454.         make_true(PARAMETER_EXPR,result->TOK_flags);
  455.     }
  456.  
  457.     if( is_true(EVALUATED_EXPR,term1->TOK_flags)
  458.      && is_true(EVALUATED_EXPR,term2->TOK_flags) ) {
  459.         make_true(EVALUATED_EXPR,result->TOK_flags);
  460.     }
  461. #ifdef DEBUG_EXPRTYPE
  462. if(debug_latest)
  463. (void)fprintf(list_fd,"\nconst param eval: (%d %d %d) %s (%d %d %d) = (%d %d %d)",
  464. is_true(CONST_EXPR,term1->TOK_flags),
  465. is_true(PARAMETER_EXPR,term1->TOK_flags),
  466. is_true(EVALUATED_EXPR,term1->TOK_flags),
  467.  
  468. op->src_text,
  469.  
  470. is_true(CONST_EXPR,term2->TOK_flags),
  471. is_true(PARAMETER_EXPR,term2->TOK_flags),
  472. is_true(EVALUATED_EXPR,term2->TOK_flags),
  473.  
  474. is_true(CONST_EXPR,result->TOK_flags),
  475. is_true(PARAMETER_EXPR,result->TOK_flags),
  476. is_true(EVALUATED_EXPR,result->TOK_flags));
  477. #endif
  478.  
  479.   if(! INTRINS_ARGS) {        /* Remaining steps only applicable to exprs */
  480.  
  481.         /* Remember if integer division was used */
  482.     if(result_type == type_INTEGER &&
  483.        (op == '/' ||
  484.         (is_true(INT_QUOTIENT_EXPR,term1->TOK_flags) ||
  485.          is_true(INT_QUOTIENT_EXPR,term2->TOK_flags))) ) {
  486.         make_true(INT_QUOTIENT_EXPR,result->TOK_flags);
  487.     }
  488.         /* Issue warning if integer expr involving division is
  489.            later converted to any real type, or if it is used
  490.            as an exponent. */
  491.     if( is_true(INT_QUOTIENT_EXPR,term1->TOK_flags)
  492.     || is_true(INT_QUOTIENT_EXPR,term2->TOK_flags) ) {
  493.  
  494.     int r=result_type;
  495.     if(r == type_LOGICAL)        /* relational tests are equivalent */
  496.         r = arith_expr_type[type1][type2];        /* to subtraction */
  497.  
  498.     if(op == tok_power && is_true(INT_QUOTIENT_EXPR,term2->TOK_flags) ) {
  499.       if(trunc_check)
  500.         warning(operator->line_num,operator->col_num,
  501.             "integer quotient expr used in exponent");
  502.       if( ! is_true(INT_QUOTIENT_EXPR,term1->TOK_flags) )
  503.         make_false(INT_QUOTIENT_EXPR,result->TOK_flags);
  504.     }
  505.     else if( r == type_REAL || r == type_DP || r == type_COMPLEX) {
  506.       if(trunc_check)
  507.         warning(operator->line_num,operator->col_num,
  508.                 "integer quotient expr converted to real");
  509.     }
  510.     }
  511.  
  512.             /* If either term is an identifier, set use flag */
  513.     if(is_true(ID_EXPR,term1->TOK_flags))
  514.     use_variable(term1);
  515.     if(is_true(ID_EXPR,term2->TOK_flags))
  516.     use_variable(term2);
  517.  
  518.         /* Propagate the value of integer constant expressions */
  519.     if(is_true(EVALUATED_EXPR,result->TOK_flags)) {
  520.     if(result_type == type_INTEGER) {    /* Only ints propagated */
  521.       int a = int_expr_value(term1),
  522.           b = int_expr_value(term2),
  523.           c;
  524.       switch(op) {
  525.         case '+': c = a+b; break;
  526.         case '-': c = a-b; break;
  527.         case '*': c = a*b; break;
  528.         case '/': if(b == 0) {
  529.             syntax_error(term2->line_num,term2->col_num,
  530.                 "division by zero attempted");
  531.             c = 0;
  532.               }
  533.               else {
  534.             c = a/b;
  535.               }
  536.               break;
  537.         case tok_power: c = int_power(a,b); break;
  538.         case tok_AND: c = a&b; break;
  539.         case tok_OR: c = a|b; break;
  540.         case tok_EQV: c = ~(a^b); break;
  541.         case tok_NEQV: c = a^b; break;
  542.         default:
  543.           oops_message(OOPS_NONFATAL,
  544.                operator->line_num,operator->col_num,
  545.                "invalid int expr operator");
  546.             c = 0; break;
  547.       }
  548.  
  549.       make_true(EVALUATED_EXPR,result->TOK_flags);
  550.       result->value.integer = c;    /* Result goes into token value */
  551.  
  552.                 /* Integer division (including i**neg)
  553.                    that yields 0 is suspicious.  */
  554.       if(trunc_check)
  555.         if(c==0 && (op=='/' || op==tok_power)) {
  556.           warning(operator->line_num,operator->col_num,
  557.                 "integer const expr yields result of 0");
  558.         }
  559.     }
  560.       }
  561.                 /* Also nonconstant**neg is 0 unless
  562.                    nonconstant=1 */
  563.       else if(trunc_check)
  564.     if(result_type == type_INTEGER && op == tok_power
  565.           && is_true(EVALUATED_EXPR,term2->TOK_flags)
  566.           && int_expr_value(term2) < 0) {
  567.       warning(operator->line_num,operator->col_num,
  568.           "integer to negative power usually yields 0");
  569.     }
  570.   }/* end if !INTRINS_ARGS */
  571. }/*binexpr_type*/
  572.  
  573.  
  574.     /* this routine propagates type in unary expressions */
  575.  
  576. void
  577. unexpr_type(operator,term1,result)
  578.     Token *term1, *operator, *result;
  579. {
  580.    int    op = operator->class,
  581.     type1 = datatype_of(term1->TOK_type),
  582.     result_type;
  583.  
  584.     if( ! is_computational_type(type1) ) {
  585.         syntax_error(term1->line_num,term1->col_num,
  586.             "noncomputational primary in expression:");
  587.         report_type(term1);
  588.         result_type = E;
  589.     }
  590.     else {
  591.     switch(op) {
  592.             /* arith operators: use diagonal of lookup table */
  593.         case '+':
  594.         case '-':
  595.         result_type = arith_expr_type[type1][type1];
  596.         break;
  597.  
  598.                 /*  NOT: operand should be
  599.                     logical, but allow integers with a
  600.                     warning. */
  601.         case tok_NOT:
  602.         if(type1 == L)
  603.             result_type = L;
  604.         else if(type1 == I)
  605.             result_type = W+I;
  606.         else
  607.             result_type = E;
  608.         break;
  609.  
  610.         default:
  611.         oops_message(OOPS_NONFATAL,
  612.                  operator->line_num,operator->col_num,
  613.                  "unary operator type not propagated");
  614.         result_type = type1;
  615.         break;
  616.     }
  617.  
  618.     if( type1 != E )
  619.         if( result_type == E) {
  620.         syntax_error(operator->line_num,operator->col_num,
  621.             "expression incompatible with operator:");
  622.         msg_tail(operator->src_text);
  623.         msg_tail("used with");
  624.         report_type(term1);
  625.         }
  626.         else if(result_type >= W) {
  627.           if(f77_standard) {
  628.         warning(operator->line_num,operator->col_num,
  629.             "nonstandard type used with operator:");
  630.         msg_tail(operator->src_text);
  631.         msg_tail("used with");
  632.         report_type(term1);
  633.           }
  634.           result_type -= W;
  635.         }
  636.     }
  637.  
  638.     result->TOK_type = type_byte(class_VAR, result_type);
  639.     result->TOK_flags = 0;    /* clear all flags */
  640.     result->size = term1->size;    /* result is same size as operand */
  641.  
  642.         /* Keep track of constant expressions */
  643.     copy_flag(CONST_EXPR,result->TOK_flags,term1->TOK_flags);
  644.     copy_flag(PARAMETER_EXPR,result->TOK_flags,term1->TOK_flags);
  645.  
  646.         /* Remember if integer division was used */
  647.     if(result_type == type_INTEGER)
  648.         copy_flag(INT_QUOTIENT_EXPR,result->TOK_flags,term1->TOK_flags);
  649.  
  650.     if(is_true(ID_EXPR,term1->TOK_flags))
  651.     use_variable(term1);
  652.  
  653.         /* Propagate the value of integer constant expressions */
  654.     if(is_true(EVALUATED_EXPR,term1->TOK_flags)) {
  655.     if(result_type == type_INTEGER) {    /* Only ints propagated */
  656.       int a = int_expr_value(term1),
  657.           c;
  658.       switch(op) {
  659.         case '+': c = a; break;
  660.         case '-': c = -a; break;
  661.         case tok_NOT: c = ~a; break;
  662.         default: oops_message(OOPS_NONFATAL,
  663.                  operator->line_num,operator->col_num,
  664.                  "invalid int expr operator");
  665.             c = 0; break;
  666.       }
  667.       make_true(EVALUATED_EXPR,result->TOK_flags);
  668.       result->value.integer = c;    /* Result goes into token value */
  669.     }
  670.     }
  671. }
  672.  
  673.     /* this routine checks type and size match in assignment statements
  674.        and in parameter assignments */
  675.  
  676. void
  677. assignment_stmt_type(term1,equals,term2)
  678.     Token *term1, *equals, *term2;
  679. {
  680.     int type1 = datatype_of(term1->TOK_type),
  681.     type2 = datatype_of(term2->TOK_type),
  682.     result_type;
  683.  
  684.     if( ! is_computational_type(type1) ) {
  685.         syntax_error(term1->line_num,term1->col_num,
  686.             "noncomputational primary in expression:");
  687.         report_type(term1);
  688.         result_type = E;
  689.     }
  690.     else if( ! is_computational_type(type2) ) {
  691.         syntax_error(term2->line_num,term2->col_num,
  692.             "noncomputational primary in expression:");
  693.         report_type(term2);
  694.         result_type = E;
  695.     }
  696.     else {
  697.     result_type = (unsigned)assignment_type[type1][type2];
  698.  
  699.  
  700.     if( (type1 != E && type2 != E) ) {
  701.         if( result_type == E) {
  702.         syntax_error(equals->line_num,equals->col_num,
  703.             "type mismatch:");
  704.         report_type(term2);
  705.         msg_tail("assigned to");
  706.         report_type(term1);
  707.         }
  708.         else {
  709.           if(result_type >= W) {        /* W result */
  710.         if(f77_standard) {
  711.           warning(equals->line_num,equals->col_num,
  712.              "nonstandard type combination:");
  713.           report_type(term2);
  714.           msg_tail("assigned to");
  715.           report_type(term1);
  716.         }
  717.         result_type -= W;
  718.           }
  719.  
  720.             /* Watch for truncation to lower precision type */
  721.           if(trunc_check || port_check || local_wordsize==0) {
  722.         long size1 = term1->size;
  723.         long size2 = term2->size;
  724.         int type_trunc=FALSE, /* flags for kind of truncation */
  725.             size_trunc=FALSE,
  726.             mixed_size=FALSE,
  727.             promotion=FALSE,
  728.             trunc_warn,mixed_warn;
  729.  
  730.         if(size1 == size_DEFAULT && size2 == size_DEFAULT) {
  731.           type_trunc = ( is_numeric_type(type1) &&
  732.                  is_numeric_type(type2) &&
  733.                 (type1 < type2 ||
  734.                     /* C = D truncates precision of D */
  735.                 (type1 == C && type2 == D)) );
  736.  
  737.                 /* Watch for promotions also */
  738.           if(type_category[type2] == R) {
  739.             if(type_category[type1] == R) /* R|D = R|D */
  740.               promotion = (type1 > type2);
  741.             else if(type_category[type1] == C) /* C|Z = R|D */
  742.               promotion =
  743.             ((int)type_size[type1] > 2*(int)type_size[type2]);
  744.           }
  745.           else if(type_category[type2] == C) /* any = C|Z */
  746.             promotion = (type1 > type2);
  747.         }
  748.         else if(type1 == S) { /* character strings */
  749.           if(size1>0 && size2>0) /* ignore ADJUSTABLE and UNKNOWN */
  750.             size_trunc = size1 < size2;
  751.         } else {
  752.           int tc1,tc2;/* type categories: D->R, Z->C, H->I */
  753.           int ls1,ls2;/* local sizes */
  754.  
  755.                 /* Assign type categories and local sizes */
  756.           tc1 = type_category[type1];
  757.           tc2 = type_category[type2];
  758.           ls1 = size1; if(ls1 == size_DEFAULT)  ls1 = type_size[type1];
  759.           ls2 = size2; if(ls2 == size_DEFAULT)  ls2 = type_size[type2];
  760.  
  761.                 /* type truncation: any numeric type category
  762.                    to a lower category. */
  763.           type_trunc = ( /***is_numeric_type(type1) &&
  764.                  is_numeric_type(type2) &&***/
  765.                  tc1 < tc2 );
  766.  
  767.                 /* size truncation: assigned to smaller
  768.                    local size.  For C = R correct test is
  769.                    Csize < 2*Rsize */
  770.           if(tc1 == C && tc2 == R) {
  771.             size_trunc = (ls1 < ls2*2);
  772.             promotion = (ls1 > ls2*2);
  773.           }
  774.           else {
  775.             size_trunc = (ls1 < ls2);
  776.             promotion = ((tc2 == R || tc2 == C) && (ls1 > ls2));
  777.           }
  778.                 /* mixed size: default size assigned to
  779.                    declared size of like type category
  780.                    or vice-versa. -port only, and superseded
  781.                    by truncation warning if any. */
  782.           mixed_size = (tc1 == tc2) &&
  783.                (size1==size_DEFAULT ||
  784.                (size2==size_DEFAULT &&
  785.                 !is_true(CONST_EXPR,term2->TOK_flags)));
  786.  
  787.         }
  788.  
  789.             /* Under -trunc, report type truncation or size
  790.                truncation.  Say "possibly" if -nowordsize.
  791.                Also report promotions under -trunc.
  792.                If no truncation warning given and under -port,
  793.                report mixed assignment */
  794. #ifdef DEBUG_EXPRTYPE
  795. #define TorF(x) ((x)?"":"no")
  796. if(debug_latest) {
  797. (void)fprintf(list_fd,"\nassign %s =",sized_typename(type1,size1));
  798. (void)fprintf(list_fd," %s : ",sized_typename(type2,size2));
  799. (void)fprintf(list_fd,"%s type %s size %s mixed",
  800.     TorF(type_trunc),
  801.     TorF(size_trunc),
  802.     TorF(mixed_size));
  803. }
  804. #endif
  805.         trunc_warn = (trunc_check &&
  806.                   (type_trunc || size_trunc || promotion));
  807.         mixed_warn = ((port_check || local_wordsize==0) && mixed_size);
  808.         if( trunc_warn ) {
  809.           warning(equals->line_num,equals->col_num,"");
  810.           report_type(term2);
  811.           if(trunc_warn && !type_trunc && mixed_size
  812.                && local_wordsize == 0)
  813.             msg_tail("possibly");
  814.           if(promotion)
  815.             msg_tail("promoted to");
  816.           else
  817.             msg_tail("truncated to");
  818.           report_type(term1);
  819.           if(promotion)
  820.             msg_tail(": may not give desired precision");
  821.         }
  822.         else if(mixed_warn) {
  823.           nonportable(equals->line_num,equals->col_num,
  824.             "mixed default and explicit");
  825.           msg_tail((is_numeric_type(type1)&&is_numeric_type(type2))?
  826.              "precision":"size");
  827.           msg_tail("items:");
  828.           report_type(term2);
  829.           msg_tail("assigned to");
  830.           report_type(term1);
  831.         }
  832.           }
  833.         }/*end else (result_type != E)*/
  834.     }/*end if (type1,type2 != E)*/
  835.     }/*end else (is_computational_type(type2))*/
  836.  
  837.  
  838.         /* Issue warning if integer expr involving division is
  839.            later converted to any real type. */
  840.     if(trunc_check)
  841.       if( is_true(INT_QUOTIENT_EXPR,term2->TOK_flags) ) {
  842.  
  843.     int r=result_type;
  844.  
  845.     if( r == type_REAL || r == type_DP || r == type_COMPLEX)
  846.         warning(equals->line_num,equals->col_num,
  847.             "integer quotient expr converted to real");
  848.       }
  849.  
  850.  
  851.     if(is_true(ID_EXPR,term2->TOK_flags))
  852.     use_variable(term2);
  853.  
  854.     use_lvalue(term1);
  855. }
  856.  
  857.     /* Make an expression-token for a function invocation */
  858.  
  859. void
  860. func_ref_expr(id,args,result)
  861.     Token *id,*args,*result;
  862. {
  863.     Lsymtab *symt;
  864.     IntrinsInfo *defn;
  865.     int rettype, retsize;
  866.  
  867.     symt = hashtab[id->value.integer].loc_symtab;
  868.  
  869.     if( symt->intrinsic ) {
  870.         defn = symt->info.intrins_info;
  871.             /* Intrinsic functions: type stored in info field */
  872.         rettype = defn->result_type;
  873.         retsize = size_DEFAULT;
  874.  
  875.         /* Generic Intrinsic functions: use propagated arg type */
  876.         if(rettype == type_GENERIC) {
  877.         if(args->next_token == NULL) {
  878.           rettype = type_UNDECL;
  879.           retsize = size_DEFAULT;
  880.         }
  881.         else {
  882. #ifdef OLDSTUFF
  883.           rettype = args->next_token->TOK_type;
  884.           retsize = args->next_token->size;
  885. #else
  886.           rettype = args->TOK_type;
  887.           retsize = args->size;
  888. #endif
  889.         }
  890.             /* special case: REAL(integer|[d]real) ->  real */
  891.         if((defn->intrins_flags&I_SP_R) &&
  892.            (rettype != type_COMPLEX) && (rettype != type_DCOMPLEX)) {
  893.             rettype = type_REAL;
  894.             retsize = size_DEFAULT;
  895.         }
  896.  
  897.             /* special cases: */
  898.             /*       ABS([d]complex) -> [d]real */
  899.             /*      IMAG([d]complex) -> [d]real */
  900.             /*      REAL([d]complex) -> [d]real */
  901.         if(rettype == type_COMPLEX && (defn->intrins_flags&I_C_TO_R)) {
  902.             rettype = type_REAL;
  903.             retsize = retsize/2;
  904.         }
  905.         if(rettype == type_DCOMPLEX &&(defn->intrins_flags&I_C_TO_R)) {
  906.             rettype = type_DP;
  907.             retsize = size_DEFAULT;
  908.         }
  909.           }
  910.           else {        /* non-generic */
  911.  
  912.                 /* special case: CHAR(code): size=1 */
  913.         if(defn->intrins_flags&I_CHAR) {
  914.           retsize = 1;
  915.         }
  916.           }
  917.     }
  918.     else {            /* non-intrinsic */
  919.         rettype = get_type(symt);
  920.         retsize = get_size(symt,rettype);
  921.     }
  922.         /* referencing function makes it no longer a class_SUBPROGRAM
  923.            but an expression. */
  924. #ifndef TOK_type
  925.     result->class = id->class;
  926. #endif
  927.     result->subclass = 0;
  928.     result->TOK_type = type_byte(class_VAR,rettype);
  929. #ifndef TOK_flags
  930.     result->TOK_flags = 0;    /* clear all flags */
  931. #endif
  932.     result->size = retsize;
  933.     result->next_token = (Token *)NULL;
  934.  
  935. #ifdef DEBUG_EXPRTYPE
  936. if(debug_latest) {
  937. (void)fprintf(list_fd,"\n%sFunction %s() = %s",
  938. symt->intrinsic?"Intrinsic ":"",
  939. symt->name,sized_typename(rettype,retsize));
  940. }
  941. #endif
  942.  
  943.         /* If intrinsic and all arguments are PARAMETER_EXPRs,
  944.            then result is one too. */
  945.     if( symt->intrinsic ) {
  946.                 /* Evaluate intrinsic if result is
  947.                    integer, the args are const (except for
  948.                    LEN), and a handler is defined.
  949.                  */
  950.         if(rettype == type_INTEGER &&
  951.                (defn->intrins_flags&I_EVALUATED) )
  952.         {
  953.              result->value.integer = eval_intrins(defn,args);
  954.                 /* Evaluation routines can affect the flags */
  955.              copy_flag(EVALUATED_EXPR,result->TOK_flags,args->TOK_flags);
  956.         }
  957.         copy_flag(PARAMETER_EXPR,result->TOK_flags,args->TOK_flags);
  958. #ifdef DEBUG_EXPRTYPE
  959. if(debug_latest) {
  960. (void)fprintf(list_fd,"\n%s(...) ",defn->name);
  961. if(is_true(EVALUATED_EXPR,args->TOK_flags))
  962.   (void)fprintf(list_fd,"=%d",result->value.integer);
  963. else
  964.   (void)fprintf(list_fd,"not evaluated");
  965. (void)fprintf(list_fd,": const param eval=(%d %d %d)",
  966. is_true(CONST_EXPR,result->TOK_flags),
  967. is_true(PARAMETER_EXPR,result->TOK_flags),
  968. is_true(EVALUATED_EXPR,result->TOK_flags));
  969. }
  970. #endif
  971.     }
  972. }/*func_ref_expr*/
  973.  
  974.  
  975.  
  976.         /* Make an expression-token for primary consisting of
  977.            a symbolic name */
  978.  
  979. void
  980. primary_id_expr(id,primary)
  981.     Token *id,*primary;
  982. {
  983.     Lsymtab *symt;
  984.     int id_type;
  985.     symt = hashtab[id->value.integer].loc_symtab;
  986.     id_type=get_type(symt);
  987. #ifndef TOK_type
  988.     primary->class = id->class;
  989. #endif
  990.     primary->subclass = 0;
  991.     primary->TOK_type = type_byte(storage_class_of(symt->type),id_type);
  992. #ifndef TOK_flags
  993.     primary->TOK_flags = 0;
  994. #endif
  995.     primary->size =get_size(symt,id_type);
  996.     primary->left_token = (Token *) NULL;
  997.  
  998.     make_true(ID_EXPR,primary->TOK_flags);
  999.  
  1000.     if( storage_class_of(symt->type) == class_VAR) {
  1001.         if(symt->parameter) {
  1002.             make_true(CONST_EXPR,primary->TOK_flags);
  1003.             make_true(PARAMETER_EXPR,primary->TOK_flags);
  1004.             make_true(EVALUATED_EXPR,primary->TOK_flags);
  1005.         }
  1006.         else {
  1007.             make_true(LVALUE_EXPR,primary->TOK_flags);
  1008.         }
  1009.         if(symt->array_var)
  1010.             make_true(ARRAY_ID_EXPR,primary->TOK_flags);
  1011.         if(symt->set_flag || symt->common_var || symt->parameter
  1012.                   || symt->argument)
  1013.             make_true(SET_FLAG,primary->TOK_flags);
  1014.         if(symt->assigned_flag)
  1015.             make_true(ASSIGNED_FLAG,primary->TOK_flags);
  1016.         if(symt->used_before_set)
  1017.             make_true(USED_BEFORE_SET,primary->TOK_flags);
  1018.     }
  1019.     else if(storage_class_of(symt->type) == class_STMT_FUNCTION) {
  1020.         make_true(STMT_FUNCTION_EXPR,primary->TOK_flags);
  1021.     }
  1022.  
  1023. #ifdef DEBUG_PARSER
  1024. if(debug_parser){
  1025.     (void)fprintf(list_fd,"\nprimary %s: TOK_type=0x%x TOK_flags=0x%x",
  1026.         symt->name,primary->TOK_type,primary->TOK_flags);
  1027.       }
  1028. #endif
  1029. }/*primary_id_expr*/
  1030.  
  1031. int
  1032. intrins_arg_cmp(defn,t)
  1033.      IntrinsInfo *defn;        /* Definition */
  1034.      Token *t;            /* Argument */
  1035. {
  1036.   int defn_types=defn->arg_type;
  1037.   int a_type = datatype_of(t->TOK_type);
  1038.   int type_OK;
  1039.                 /* Check for argument type mismatch.
  1040.                  */
  1041.         type_OK = ( (1<<a_type) & defn_types );
  1042.         if(! type_OK) {
  1043.           int ct;/* compatible type */
  1044.                 /* Accept compatible types if
  1045.                    sizes agree, e.g. DSQRT(REAL*8).
  1046.                    The macros check the two cases and
  1047.                    set ct to the compatible type.
  1048.                  */
  1049. #define EXCEPTION1 (a_type==type_REAL && ((1<<(ct=type_DP))&defn_types))
  1050. #define EXCEPTION2 (a_type==type_COMPLEX&&((1<<(ct=type_DCOMPLEX))&defn_types))
  1051.  
  1052.           if(!( (EXCEPTION1||EXCEPTION2) && t->size==type_size[ct] )){
  1053.         syntax_error(t->line_num,t->col_num,
  1054.             "illegal argument data type for intrinsic function");
  1055.         msg_tail(defn->name);
  1056.         msg_tail(":");
  1057.         report_type(t);
  1058.           }
  1059.           else {
  1060.         if(port_check || local_wordsize==0) {
  1061.           nonportable(t->line_num,t->col_num,
  1062.           "argument precision may not be correct for intrinsic function");
  1063.           msg_tail(defn->name);
  1064.           msg_tail(":");
  1065.           report_type(t);
  1066.         }
  1067.         type_OK = TRUE; /* Acceptable after all */
  1068.           }
  1069.         }/* end if type mismatch */
  1070.  
  1071.   return type_OK;
  1072. }/*intrins_arg_cmp*/
  1073.  
  1074.  
  1075.                 /* Check agreement between statement function
  1076.                    dummy (t1) and actual (t2) args.  At this
  1077.                    time, checks only class, type and size,
  1078.                    not arrayness.  */
  1079. void
  1080. stmt_fun_arg_cmp(symt,d_arg,a_arg)
  1081.      Lsymtab *symt;
  1082.      Token *d_arg,*a_arg;
  1083. {
  1084.   int d_class = class_VAR,
  1085.       a_class = storage_class_of(a_arg->TOK_type),
  1086.       d_type = datatype_of(d_arg->TOK_type),
  1087.       a_type = datatype_of(a_arg->TOK_type),
  1088.       d_size = d_arg->size,
  1089.       a_size = a_arg->size,
  1090.       d_defsize = (d_size == size_DEFAULT),
  1091.       a_defsize = (a_size == size_DEFAULT);
  1092.   int d_cmptype= (d_type==type_HOLLERITH && a_type!=type_STRING)?
  1093.                 a_type:type_category[d_type];
  1094.   int a_cmptype= (a_type==type_HOLLERITH && d_type!=type_STRING)?
  1095.                 d_type:type_category[a_type];
  1096.  
  1097.   if(!(port_check || local_wordsize==0)) {
  1098.     if(d_defsize)
  1099.       d_size = type_size[d_type];
  1100.     if(a_defsize)
  1101.       a_size = type_size[a_type];
  1102.   }
  1103.  
  1104.   if(d_size < 0 || a_size < 0) { /* char size_ADJUSTABLE or UNKNOWN */
  1105.     d_size = a_size = size_DEFAULT;    /* suppress warnings on size */
  1106.     d_defsize = a_defsize = TRUE;
  1107.   }
  1108.  
  1109.   if(d_class != a_class || d_cmptype != a_cmptype ||
  1110.      (d_type == type_STRING? d_size > a_size: d_size != a_size) ) {
  1111.         syntax_error(a_arg->line_num,a_arg->col_num,
  1112.           "argument mismatch in stmt function");
  1113.         msg_tail(symt->name); /* Give the stmt func name */
  1114.         msg_tail(": dummy");
  1115.         report_type(d_arg); /* Dummy arg type */
  1116.         msg_tail("vs actual");
  1117.         report_type(a_arg);
  1118.   }
  1119. }/*stmt_fun_arg_cmp*/
  1120.  
  1121.  
  1122.                 /* Routine to document the types of
  1123.                    two terms and their operator */
  1124. PRIVATE void
  1125. report_mismatch(term1,operator,term2)
  1126.      Token *term1,*operator,*term2;
  1127. {
  1128.   report_type(term1);
  1129.   msg_tail(operator->src_text);
  1130.   report_type(term2);
  1131. }
  1132.                 /* Routine to document the type
  1133.                    of a token, with its name if it
  1134.                    has one. */
  1135. PRIVATE void
  1136. report_type(t)
  1137.      Token *t;
  1138. {
  1139.   msg_tail(sized_typename((int)datatype_of(t->TOK_type),t->size));
  1140.   if(is_true(ID_EXPR,t->TOK_flags))
  1141.     msg_tail(hashtab[t->value.integer].name);
  1142.   else if(is_true(LIT_CONST,t->TOK_flags))
  1143.     msg_tail("const");
  1144.   else
  1145.     msg_tail("expr");
  1146. }
  1147.  
  1148.  
  1149. int
  1150. substring_size(id,limits)
  1151.      Token *id,*limits;
  1152. {
  1153.     int id_type,id_len;
  1154.     int startindex,endindex,substr_len;
  1155. #ifdef DEBUG_EXPRTREES
  1156.     Lsymtab *symt = hashtab[id->value.integer].loc_symtab;
  1157. #endif
  1158. /***    id_type=get_type(symt); **/
  1159.     id_type = datatype_of(id->TOK_type);
  1160.  
  1161.     substr_len=size_UNKNOWN;
  1162.  
  1163.     if(id_type != type_STRING) {
  1164.       syntax_error(id->line_num,id->col_num,
  1165.                "string variable expected");
  1166.     }
  1167.     else {
  1168.       id_len = id->size;
  1169. #ifdef DEBUG_EXPRTREES
  1170.       if(debug_latest) {
  1171.         fprintf(list_fd,"\nSubstring %s :: ",symt->name);
  1172.         print_expr_list(limits);
  1173.       }
  1174. #endif
  1175.         /* fortran.y stores (startindex:endindex) in
  1176.            TOK_start, Tok_end */
  1177.       startindex = limits->TOK_start;
  1178.       endindex = limits->TOK_end;
  1179.       if(startindex != size_UNKNOWN && endindex != size_UNKNOWN) {
  1180.         /* Check limits unless endindex=0 */
  1181.         if( startindex > endindex && endindex > 0 ) {
  1182.           syntax_error(limits->line_num,limits->col_num,
  1183.               "invalid substring limits");
  1184.         }
  1185.         else {
  1186.           if(endindex == 0)    /* 0 means it was (startindex: ) */
  1187.         endindex=id_len;
  1188.           substr_len = endindex-startindex+1;
  1189.           if(id_len > 0 && substr_len > id_len)
  1190.         syntax_error(limits->line_num,limits->col_num,
  1191.               "substring size exceeds string size");
  1192.         }
  1193.       }
  1194.     }
  1195.     return substr_len;
  1196. }
  1197.  
  1198.     /* Integer power: uses recursion x**n = (x**(n/2))**2 */
  1199. PRIVATE int
  1200. int_power(x,n)
  1201.     int x,n;
  1202. {
  1203.     int temp;
  1204.             /* Order of tests puts commonest cases first */
  1205.     if(n > 1) {
  1206.         temp = int_power(x,n>>1);
  1207.         temp *= temp;
  1208.         if(n&1) return temp*x;    /* Odd n */
  1209.         else    return temp;    /* Even n */
  1210.     }
  1211.     else if(n == 1) return x;
  1212.     else if(n < 0) return 1/int_power(x,-n);    /* Usually 0 */
  1213.     else return 1;
  1214. }
  1215.  
  1216.                 /* Intrinsic function handlers */
  1217.  
  1218. PRIVATE int
  1219.     ii_abs(), ii_sign(),  ii_dim(),   ii_mod(),
  1220.     ii_max(), ii_min(),   ii_ichar(), ii_len(),  ii_index();
  1221.  
  1222. /* Array of pointers to functions for evaluating integer-valued intrinsic
  1223.    functions.  The order matches definitions of I_ABS thru I_INDEX in
  1224.    symtab.h */
  1225.  
  1226. PRIVATE int (*ii_fun[])()={
  1227.   NULL,
  1228.   ii_abs,
  1229.   ii_sign,
  1230.   ii_dim,
  1231.   ii_mod,
  1232.   ii_max,
  1233.   ii_min,
  1234.   ii_ichar,
  1235.   ii_len,
  1236.   ii_index,
  1237. };
  1238.  
  1239. PRIVATE int
  1240. eval_intrins(defn,args)
  1241.      IntrinsInfo *defn;
  1242.      Token *args;
  1243. {
  1244.     int index;
  1245.     index = (defn->intrins_flags & I_EVALUATED);
  1246.  
  1247.                 /* Args must be evaluated, except for LEN */
  1248.     if( (is_true(EVALUATED_EXPR,args->TOK_flags) || index==I_LEN) &&
  1249.        index > 0 && index < (sizeof(ii_fun)/sizeof(ii_fun[0])) ) {
  1250.       return (*ii_fun[index])(args);
  1251.     }
  1252.     else {
  1253. #ifdef DEBUG_EXPRTYPE
  1254.       if(debug_latest)
  1255.     (void)fprintf(list_fd,"\nIntrinsic %s not handled",defn->name);
  1256.       make_false(EVALUATED_EXPR,args->TOK_flags);
  1257. #endif
  1258.       return 0;
  1259.     }
  1260. }
  1261.  
  1262.  
  1263. PRIVATE int
  1264. ii_abs(args)
  1265.      Token *args;
  1266. {
  1267.   Token *t;
  1268.   int val, result=0;
  1269.   t = args->next_token;
  1270.   if(t->TOK_type != type_INTEGER) {/* wrong arg type: message given elsewhere */
  1271.     make_false(EVALUATED_EXPR,args->TOK_flags);
  1272.   }
  1273.   else {
  1274.     val = int_expr_value(t);
  1275.     result = (val >= 0? val: -val);
  1276.   }
  1277.   return result;
  1278. }
  1279.  
  1280. PRIVATE int
  1281. ii_sign(args)            /* SIGN(value,sign) */
  1282.      Token *args;
  1283. {
  1284.   Token *t1,*t2;
  1285.   int val1,val2, result=0;
  1286.   t1 = args->next_token;
  1287.   t2 = t1->next_token;
  1288.   if(t2 == NULL || t1->TOK_type != type_INTEGER
  1289.      || t2->TOK_type != type_INTEGER) {/* wrong arg type: message given elswr */
  1290.     make_false(EVALUATED_EXPR,args->TOK_flags);
  1291.   }
  1292.   else {
  1293.     val1 = int_expr_value(t1);
  1294.     if(val1 < 0) val1 = -val1;
  1295.     val2 = int_expr_value(t2);
  1296.     result = (val2 >= 0? val1: -val1);
  1297.   }
  1298.   return result;
  1299. }
  1300.  
  1301. PRIVATE int
  1302. ii_dim(args)            /* DIM(int,int) */
  1303.      Token *args;
  1304. {
  1305.   Token *t1,*t2;
  1306.   int val, result=0;
  1307.   t1 = args->next_token;
  1308.   t2 = t1->next_token;
  1309.   if(t2 == NULL || t1->TOK_type != type_INTEGER
  1310.      || t2->TOK_type != type_INTEGER) {/* wrong arg type: message given elswr */
  1311.     make_false(EVALUATED_EXPR,args->TOK_flags);
  1312.   }
  1313.   else {
  1314.     val = int_expr_value(t1)-int_expr_value(t2);
  1315.     result = (val >= 0? val: 0);
  1316.   }
  1317.   return result;
  1318. }
  1319.  
  1320. PRIVATE int
  1321. ii_mod(args)            /* MOD(int,int) */
  1322.      Token *args;
  1323. {
  1324.   Token *t1,*t2;
  1325.   int val1,val2,quotient, result=0;
  1326.   t1 = args->next_token;
  1327.   t2 = t1->next_token;
  1328.   if(t2 == NULL || t1->TOK_type != type_INTEGER
  1329.      || t2->TOK_type != type_INTEGER) {/* wrong arg type: message given elswr */
  1330.     make_false(EVALUATED_EXPR,args->TOK_flags);
  1331.   }
  1332.   else {
  1333.     val1 = int_expr_value(t1);
  1334.     val2 = int_expr_value(t2);
  1335.     if((val1 < 0) == (val2 < 0)) {
  1336.       quotient = val1/val2;    /* Both positive or both negative*/
  1337.     }
  1338.     else {
  1339.       quotient = -(-val1/val2);    /* Unlike signs */
  1340.     }
  1341.     result = val1 - quotient*val2;
  1342.   }
  1343.   return result;
  1344. }
  1345.  
  1346.  
  1347. PRIVATE int
  1348. ii_max(args)            /* MAX(int,int,...) */
  1349.      Token *args;
  1350. {
  1351.   Token *t=args;
  1352.   int val,result=0,n=0;
  1353. #ifdef DEBUG_EXPRTYPE
  1354. if(debug_latest)
  1355. (void)fprintf(list_fd,"\nEvaluating MAX(");
  1356. #endif
  1357.   while( (t=t->next_token) != NULL) {
  1358.  
  1359.       if(t->TOK_type != type_INTEGER) {/* wrong arg type: message given elswr */
  1360.     make_false(EVALUATED_EXPR,args->TOK_flags);
  1361.     break;
  1362.       }
  1363.       else {
  1364.     val = int_expr_value(t);
  1365.     if(n++ == 0 || val > result)
  1366.       result = val;
  1367. #ifdef DEBUG_EXPRTYPE
  1368. if(debug_latest)
  1369. (void)fprintf(list_fd,"%d ",val);
  1370. #endif
  1371.       }
  1372.   }
  1373. #ifdef DEBUG_EXPRTYPE
  1374. if(debug_latest)
  1375. (void)fprintf(list_fd,") = %d",result);
  1376. #endif
  1377.   return result;
  1378. }
  1379.  
  1380. PRIVATE int
  1381. ii_min(args)            /* MIN(int,int,...) */
  1382.      Token *args;
  1383. {
  1384.   Token *t=args;
  1385.   int val,result=0,n=0;
  1386.   while( (t=t->next_token) != NULL) {
  1387.       if(t->TOK_type != type_INTEGER) {/* wrong arg type: message given elswr */
  1388.     make_false(EVALUATED_EXPR,args->TOK_flags);
  1389.     break;
  1390.       }
  1391.       else {
  1392.     val = int_expr_value(t);
  1393.     if(n++ == 0 || val < result)
  1394.       result = val;
  1395.       }
  1396.   }
  1397.   return result;
  1398. }
  1399.  
  1400. PRIVATE int
  1401. ii_ichar(args)        /* ICHAR(string) */
  1402.      Token *args;
  1403. {
  1404.   Token *t=args->next_token;
  1405.  
  1406.   if(t->TOK_type != type_STRING || !is_true(LIT_CONST,t->TOK_flags)) {
  1407.     make_false(EVALUATED_EXPR,args->TOK_flags);
  1408.   }
  1409.   else {
  1410.     return t->value.string[0];    /* Processor collating sequence is used */
  1411.   }
  1412.   return 0;
  1413. }
  1414.  
  1415. PRIVATE int
  1416. ii_len(args)        /* LEN(string) */
  1417.      Token *args;
  1418. {
  1419.   Token *t=args->next_token;
  1420.   int val,result=0;
  1421.  
  1422.         /* Set the PARAMETER_EXPR flag since LEN of string does
  1423.            not require contents to be known */
  1424.   if( t->TOK_type == type_STRING && (val = t->size) > 0 ) {
  1425.     make_true(PARAMETER_EXPR,args->TOK_flags);
  1426.     make_true(EVALUATED_EXPR,args->TOK_flags);
  1427.     result = val;
  1428.   }
  1429.   else {            /* nonstring or adjustable or unknown */
  1430.     make_false(PARAMETER_EXPR,args->TOK_flags);
  1431.     make_false(EVALUATED_EXPR,args->TOK_flags);
  1432.   }
  1433.  
  1434.   return result;
  1435. }
  1436.  
  1437. PRIVATE int
  1438. ii_index(args)        /* INDEX(str1,str2) */
  1439.      Token *args;
  1440. {
  1441.   Token *t1,*t2;
  1442.   t1=args->next_token;
  1443.   t2=t1->next_token;
  1444.  
  1445.   if(t2 == NULL || t1->TOK_type != type_STRING
  1446.      || t2->TOK_type != type_STRING
  1447.      || !is_true(LIT_CONST,t1->TOK_flags) || !is_true(LIT_CONST,t2->TOK_flags)) {
  1448.     make_false(EVALUATED_EXPR,args->TOK_flags);
  1449.   }
  1450.   else {
  1451.     int i;
  1452.     char *s1=t1->value.string;
  1453.     char *s2=t2->value.string;
  1454.     int n1=strlen(s1), n2=strlen(s2);
  1455.  
  1456.     for(i=1; n1 > 0 && n1 >= n2; i++,s1++,n1--) {
  1457.       if(strncmp(s1,s2,n2) == 0)
  1458.     return i;
  1459.     }
  1460.   }
  1461.   return 0;
  1462. }
  1463.  
  1464.  
  1465.  
  1466.  
  1467.                 /* Undefine special macros */
  1468. #undef E
  1469. #undef I
  1470. #undef R
  1471. #undef D
  1472. #undef C
  1473. #undef L
  1474. #undef S
  1475. #undef H
  1476. #undef W
  1477.