* sim/ucsim/configure.in,
[fw/sdcc] / src / SDCCast.c
index 29ac0a73fca8523130e9c9b51b3c399a900da27e..8e20910cf1c505dae56081cc9e485c210bbe0a52 100644 (file)
@@ -679,6 +679,7 @@ processParms (ast *func,
   if (func->type != EX_VALUE && !IFFUNC_ISREENT (functype) && !options.stackAuto)
     {
       werror (W_NONRENT_ARGS);
+      fatalError++;
       return 1;
     }
 
@@ -870,7 +871,7 @@ createIvalStruct (ast * sym, sym_link * type, initList * ilist)
     }
 
   if (iloop) {
-    werrorfl (filename, sym->opval.val->sym->lineDef,
+    werrorfl (sym->opval.val->sym->fileDef, sym->opval.val->sym->lineDef,
              W_EXCESS_INITIALIZERS, "struct", 
              sym->opval.val->sym->name);
   }
@@ -931,6 +932,7 @@ createIvalArray (ast * sym, sym_link * type, initList * ilist)
            // Array size was specified, and we have more initializers than needed.
            char *name=sym->opval.val->sym->name;
            int lineno=sym->opval.val->sym->lineDef;
+           char *filename=sym->opval.val->sym->fileDef;
            
            werrorfl (filename, lineno, W_EXCESS_INITIALIZERS, "array", name);
        }
@@ -957,6 +959,7 @@ createIvalArray (ast * sym, sym_link * type, initList * ilist)
                // there has to be a better way
                char *name=sym->opval.val->sym->name;
                int lineno=sym->opval.val->sym->lineDef;
+               char *filename=sym->opval.val->sym->fileDef;
                werrorfl (filename, lineno, W_EXCESS_INITIALIZERS, "array", name);
                
                break;
@@ -1131,7 +1134,7 @@ gatherAutoInit (symbol * autoChain)
            work = initAggregates (sym, sym->ival, NULL);
          } else {
            if (getNelements(sym->type, sym->ival)>1) {
-             werrorfl (filename, sym->lineDef,
+             werrorfl (sym->fileDef, sym->lineDef,
                        W_EXCESS_INITIALIZERS, "scalar", 
                        sym->name);
            }
@@ -1167,7 +1170,7 @@ gatherAutoInit (symbol * autoChain)
            work = initAggregates (sym, sym->ival, NULL);
          } else {
            if (getNelements(sym->type, sym->ival)>1) {
-             werrorfl (filename, sym->lineDef,
+             werrorfl (sym->fileDef, sym->lineDef,
                        W_EXCESS_INITIALIZERS, "scalar", 
                        sym->name);
            }
@@ -2016,10 +2019,18 @@ RESULT_TYPE
 getResultTypeFromType (sym_link *type)
 {
   /* type = getSpec (type); */
-  if (IS_BITVAR (type))
+  if (IS_BIT (type))
     return RESULT_TYPE_BIT;
   if (IS_BITFIELD (type))
-    return RESULT_TYPE_CHAR;
+    {
+      int blen = SPEC_BLEN (type);
+      
+      if (blen <= 1)
+        return RESULT_TYPE_BIT;
+      if (blen <= 8)
+        return RESULT_TYPE_CHAR;
+      return RESULT_TYPE_INT;
+    }
   if (IS_CHAR (type))
     return RESULT_TYPE_CHAR;
   if (   IS_INT (type)
@@ -2081,6 +2092,7 @@ addCast (ast *tree, RESULT_TYPE resultType, bool upcast)
     }
   tree->decorated = 0;
   tree = newNode (CAST, newAst_LINK (newLink), tree);
+  tree->lineno = tree->right->lineno;
   /* keep unsigned type during cast to smaller type,
      but not when promoting from char to int */
   if (!upCasted)
@@ -2101,6 +2113,10 @@ resultTypePropagate (ast *tree, RESULT_TYPE resultType)
       case ':':
       case '|':
       case '^':
+      case '*':
+      case '+':
+      case '-':
+      case LABEL:
        return resultType;
       case '&':
        if (!tree->right)
@@ -2108,6 +2124,8 @@ resultTypePropagate (ast *tree, RESULT_TYPE resultType)
          return RESULT_TYPE_NONE;
        else
          return resultType;
+      case IFX:
+       return RESULT_TYPE_IFX;
       default:
        return RESULT_TYPE_NONE;
     }
@@ -2277,7 +2295,10 @@ decorateType (ast * tree, RESULT_TYPE resultType)
        upon tree->opval.op, if resultType can be propagated */
     resultTypeProp = resultTypePropagate (tree, resultType);
 
-    dtl = decorateType (tree->left, resultTypeProp);
+    if (tree->opval.op == '?')
+      dtl = decorateType (tree->left, RESULT_TYPE_IFX);
+    else
+      dtl = decorateType (tree->left, resultTypeProp);
 
     /* if an array node, we may need to swap branches */
     if (tree->opval.op == '[')
@@ -2543,7 +2564,10 @@ decorateType (ast * tree, RESULT_TYPE resultType)
 
          tree->left  = addCast (tree->left,  resultType, FALSE);
          tree->right = addCast (tree->right, resultType, FALSE);
-         TTYPE (tree) = computeType (LTYPE (tree), RTYPE (tree), FALSE);
+         TTYPE (tree) = computeType (LTYPE (tree),
+                                     RTYPE (tree),
+                                     resultType,
+                                     tree->opval.op);
          TETYPE (tree) = getSpec (TTYPE (tree));
 
           /* if left is a literal exchange left & right */
@@ -2761,7 +2785,8 @@ decorateType (ast * tree, RESULT_TYPE resultType)
       TETYPE (tree) = getSpec (TTYPE (tree) =
                               computeType (LTYPE (tree),
                                            RTYPE (tree),
-                                           FALSE));
+                                           resultType,
+                                           tree->opval.op));
 
       return tree;
 
@@ -2789,10 +2814,12 @@ decorateType (ast * tree, RESULT_TYPE resultType)
        }
 
       LRVAL (tree) = RRVAL (tree) = 1;
+
       TETYPE (tree) = getSpec (TTYPE (tree) =
                               computeType (LTYPE (tree),
                                            RTYPE (tree),
-               ! (IS_UNSIGNED (LTYPE (tree)) && IS_UNSIGNED (RTYPE (tree)))));
+                                           resultType,
+                                           tree->opval.op));
 
       /* if right is a literal and */
       /* left is also a division by a literal then */
@@ -2858,7 +2885,8 @@ decorateType (ast * tree, RESULT_TYPE resultType)
       TETYPE (tree) = getSpec (TTYPE (tree) =
                               computeType (LTYPE (tree),
                                            RTYPE (tree),
-               ! (IS_UNSIGNED (LTYPE (tree)) && IS_UNSIGNED (RTYPE (tree)))));
+                                           resultType,
+                                           tree->opval.op));
       return tree;
 
       /*------------------------------------------------------------------*/
@@ -2973,8 +3001,9 @@ decorateType (ast * tree, RESULT_TYPE resultType)
       TETYPE (tree) = getSpec (TTYPE (tree) =
                                   computeType (LTYPE (tree),
                                                RTYPE (tree),
-                              resultType == RESULT_TYPE_CHAR ? FALSE : TRUE));
-      
+                                               resultType,
+                                               tree->opval.op));
+
       return tree;
 
       /*------------------------------------------------------------------*/
@@ -3107,8 +3136,9 @@ decorateType (ast * tree, RESULT_TYPE resultType)
           tree->right = addCast (tree->right, resultType, TRUE);
           TETYPE (tree) = getSpec (TTYPE (tree) =
                                     computeType (LTYPE (tree),
-                                                 RTYPE (tree),
-                              resultType == RESULT_TYPE_CHAR ? FALSE : TRUE));
+                                                 RTYPE (tree),
+                                                 resultType,
+                                                 tree->opval.op));
        }
        
       return tree;
@@ -3212,8 +3242,9 @@ decorateType (ast * tree, RESULT_TYPE resultType)
          tree->right = addCast (tree->right, resultType, TRUE);
          TETYPE (tree) = getSpec (TTYPE (tree) =
                                     computeType (LTYPE (tree),
-                                                 RTYPE (tree),
-                              resultType == RESULT_TYPE_CHAR ? FALSE : TRUE));
+                                                 RTYPE (tree),
+                                                 resultType,
+                                                 tree->opval.op));
        }
 
       LRVAL (tree) = RRVAL (tree) = 1;
@@ -3360,8 +3391,9 @@ decorateType (ast * tree, RESULT_TYPE resultType)
          tree->left = addCast (tree->left, resultType, TRUE);
          TETYPE (tree) = getSpec (TTYPE (tree) =
                                       computeType (LTYPE (tree),
-                                                   RTYPE (tree),
-                              resultType == RESULT_TYPE_CHAR ? FALSE : TRUE));
+                                                   NULL,
+                                                   resultType,
+                                                   tree->opval.op));
        }
       else /* RIGHT_OP */
        {
@@ -3682,8 +3714,14 @@ decorateType (ast * tree, RESULT_TYPE resultType)
            }
          if (tree->opval.op == '>')
            {
-             /* if the parent is an ifx, then we could do */
-             /* return tree->left; */
+             if (resultType == RESULT_TYPE_IFX)
+               {
+                 /* the parent is an ifx: */
+                 /* if (unsigned value) */
+                 return tree->left;
+               }
+             
+             /* (unsigned value) ? 1 : 0 */
              tree->opval.op = '?';
              tree->right = newNode (':',
                                     newAst_VALUE (constVal ("1")),
@@ -3717,8 +3755,13 @@ decorateType (ast * tree, RESULT_TYPE resultType)
       /*----------------------------*/
     case SIZEOF:               /* evaluate wihout code generation */
       /* change the type to a integer */
+      {
+       int size = getSize (tree->right->ftype);
+       SNPRINTF(buffer, sizeof(buffer), "%d", size);
+       if (!size && !IS_VOID(tree->right->ftype))
+         werrorfl (tree->filename, tree->lineno, E_SIZEOF_INCOMPLETE_TYPE);
+      }
       tree->type = EX_VALUE;
-      SNPRINTF(buffer, sizeof(buffer), "%d", (getSize (tree->right->ftype)));
       tree->opval.val = constVal (buffer);
       tree->right = tree->left = NULL;
       TETYPE (tree) = getSpec (TTYPE (tree) =
@@ -3833,7 +3876,8 @@ decorateType (ast * tree, RESULT_TYPE resultType)
          goto errorTreeReturn;
        }
 
-      TTYPE (tree) = computeType (LTYPE (tree), RTYPE (tree), FALSE);
+      TTYPE (tree) = computeType (LTYPE (tree), RTYPE (tree),
+                                  resultType, tree->opval.op);
       TETYPE (tree) = getSpec (TTYPE (tree));
       return tree;
 
@@ -3916,7 +3960,8 @@ decorateType (ast * tree, RESULT_TYPE resultType)
       TETYPE (tree) = getSpec (TTYPE (tree) =
                               computeType (LTYPE (tree),
                                            RTYPE (tree),
-                                           FALSE));
+                                           RESULT_TYPE_NOPROM,
+                                           tree->opval.op));
 
       if (!tree->initMode && IS_CONSTANT (LETYPE (tree)))
        werror (E_CODE_WRITE, "-=");
@@ -3958,7 +4003,8 @@ decorateType (ast * tree, RESULT_TYPE resultType)
       TETYPE (tree) = getSpec (TTYPE (tree) =
                               computeType (LTYPE (tree),
                                            RTYPE (tree),
-                                           FALSE));
+                                           RESULT_TYPE_NOPROM,
+                                           tree->opval.op));
 
       if (!tree->initMode && IS_CONSTANT (LETYPE (tree)))
        werror (E_CODE_WRITE, "+=");
@@ -4200,12 +4246,15 @@ value *
 sizeofOp (sym_link * type)
 {
   char buff[10];
+  int size;
 
   /* make sure the type is complete and sane */
   checkTypeSanity(type, "(sizeof)");
 
   /* get the size and convert it to character  */
-  SNPRINTF (buff, sizeof(buff), "%d", getSize (type));
+  SNPRINTF (buff, sizeof(buff), "%d", size = getSize (type));
+  if (!size && !IS_VOID(type))
+    werror (E_SIZEOF_INCOMPLETE_TYPE);
 
   /* now convert into value  */
   return constVal (buff);