dotgnu-pnet-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Dotgnu-pnet-commits] CVS: pnet/cscc/c c_defs.tc,1.18,1.19 c_grammar.y,


From: Rhys Weatherley <address@hidden>
Subject: [Dotgnu-pnet-commits] CVS: pnet/cscc/c c_defs.tc,1.18,1.19 c_grammar.y,1.41,1.42 c_oper.tc,1.23,1.24 c_stmt.tc,1.7,1.8
Date: Sun, 08 Dec 2002 19:28:21 -0500

Update of /cvsroot/dotgnu-pnet/pnet/cscc/c
In directory subversions:/tmp/cvs-serv22882/cscc/c

Modified Files:
        c_defs.tc c_grammar.y c_oper.tc c_stmt.tc 
Log Message:


Infer the size of an array from its initializer.


Index: c_defs.tc
===================================================================
RCS file: /cvsroot/dotgnu-pnet/pnet/cscc/c/c_defs.tc,v
retrieving revision 1.18
retrieving revision 1.19
diff -C2 -r1.18 -r1.19
*** c_defs.tc   5 Nov 2002 23:58:27 -0000       1.18
--- c_defs.tc   9 Dec 2002 00:28:19 -0000       1.19
***************
*** 404,407 ****
--- 404,412 ----
        ILEvalValue value;
  }
+ %node ILNode_CAssignArray ILNode_Statement =
+ {
+       ILNode *expr1;
+       ILNode *expr2;
+ }
  
  %output "c_semantics.c"

Index: c_grammar.y
===================================================================
RCS file: /cvsroot/dotgnu-pnet/pnet/cscc/c/c_grammar.y,v
retrieving revision 1.41
retrieving revision 1.42
diff -C2 -r1.41 -r1.42
*** c_grammar.y 19 Oct 2002 21:49:17 -0000      1.41
--- c_grammar.y 9 Dec 2002 00:28:19 -0000       1.42
***************
*** 511,514 ****
--- 511,537 ----
  
  /*
+  * Get the size of an array initializer for a particular array type.
+  */
+ static ILUInt32 ArrayInitializerSize(ILType *type, ILNode *init)
+ {
+       /* Handle the string case first */
+       if(yyisa(init, ILNode_CString))
+       {
+               return (ILUInt32)(((ILNode_CString *)init)->len) + 1;
+       }
+ 
+       /* If this isn't an array initializer, then bail out */
+       if(!yyisa(init, ILNode_ArrayInit))
+       {
+               CCErrorOnLine(yygetfilename(init), yygetlinenum(init),
+                                         _("invalid initializer"));
+               return 1;
+       }
+ 
+       /* TODO: handle arrays of structures that don't have nested bracketing 
*/
+       return ILNode_List_Length(((ILNode_ArrayInit *)init)->expr);
+ }
+ 
+ /*
   * Process a local or global declaration.
   */
***************
*** 521,524 ****
--- 544,548 ----
        ILNode *assign;
        ILType *prevType;
+       ILUInt32 size;
  
        /* If there is a parameter list associated with the declarator, then
***************
*** 617,621 ****
                }
  
!               /* TODO: extend open arrays to the same size as initializers */
  
                /* Make sure that the type size is fixed or dynamic */
--- 641,663 ----
                }
  
!               /* Extend open arrays to the same size as initializers */
!               if(CTypeIsOpenArray(type))
!               {
!                       if(init != 0)
!                       {
!                               /* Infer the initializer size from the 
expression */
!                               size = ArrayInitializerSize(type, init);
!                       }
!                       else
!                       {
!                               /* If there is no initializer, then change to 
one element */
!                               CCWarningOnLine(yygetfilename(decl.node),
!                                                               
yygetlinenum(decl.node),
!                                                               _("array `%s' 
assumed to have one element"),
!                                                               decl.name);
!                               size = 1;
!                       }
!                       type = CTypeCreateArray(&CCCodeGen, 
CTypeGetElemType(type), size);
!               }
  
                /* Make sure that the type size is fixed or dynamic */
***************
*** 669,672 ****
--- 711,736 ----
                else
                {
+                       /* Extend open arrays to the same size as initializers 
*/
+                       if(CTypeIsOpenArray(type))
+                       {
+                               if(init != 0)
+                               {
+                                       /* Infer the initializer size from the 
expression */
+                                       size = ArrayInitializerSize(type, init);
+                               }
+                               else
+                               {
+                                       /* If there is no initializer, then 
change to one element */
+                                       
CCWarningOnLine(yygetfilename(decl.node),
+                                                                       
yygetlinenum(decl.node),
+                                                                       
_("array `%s' assumed to have one element"),
+                                                                       
decl.name);
+                                       size = 1;
+                               }
+                               type = CTypeCreateArray
+                                       (&CCCodeGen, CTypeGetElemType(type), 
size);
+                       }
+ 
+                       /* Allocate the global variable */
                        index = CGenAllocLocal(&CCCodeGen, type);
                        CScopeAddLocal(decl.name, decl.node, index, type);
***************
*** 681,685 ****
                                         CTypeDecay(&CCCodeGen, type));
                                CGenCloneLine(assign, decl.node);
!                               assign = ILNode_Assign_create(assign, init);
                                CGenCloneLine(assign, init);
                                ILNode_List_Add(*list, assign);
--- 745,756 ----
                                         CTypeDecay(&CCCodeGen, type));
                                CGenCloneLine(assign, decl.node);
!                               if(CTypeIsArray(type))
!                               {
!                                       assign = 
ILNode_CAssignArray_create(assign, init);
!                               }
!                               else
!                               {
!                                       assign = ILNode_Assign_create(assign, 
init);
!                               }
                                CGenCloneLine(assign, init);
                                ILNode_List_Add(*list, assign);
***************
*** 2343,2353 ****
  Initializer
        : AssignmentExpression                          { $$ = $1; }
!       | '{' InitializerList '}'                       { $$ = $2; }
!       | '{' InitializerList ',' '}'           { $$ = $2; }
        ;
  
  InitializerList
!       : Initializer                                           { $$ = $1; }
!       | InitializerList ',' Initializer       { $$ = 0; /* TODO */ }
        ;
  
--- 2414,2430 ----
  Initializer
        : AssignmentExpression                          { $$ = $1; }
!       | '{' InitializerList '}'                       { $$ = 
ILNode_ArrayInit_create($2); }
!       | '{' InitializerList ',' '}'           { $$ = 
ILNode_ArrayInit_create($2); }
        ;
  
  InitializerList
!       : Initializer                                           {
!                               $$ = ILNode_List_create();
!                               ILNode_List_Add($$, $1);
!                       }
!       | InitializerList ',' Initializer       {
!                               $$ = $1;
!                               ILNode_List_Add($$, $3);
!                       }
        ;
  

Index: c_oper.tc
===================================================================
RCS file: /cvsroot/dotgnu-pnet/pnet/cscc/c/c_oper.tc,v
retrieving revision 1.23
retrieving revision 1.24
diff -C2 -r1.23 -r1.24
*** c_oper.tc   5 Nov 2002 23:58:27 -0000       1.23
--- c_oper.tc   9 Dec 2002 00:28:19 -0000       1.24
***************
*** 1795,1798 ****
--- 1795,1873 ----
  
  /*
+  * Perform semantic analysis for the array assignment statement.
+  */
+ ILNode_CSemAnalysis(ILNode_CAssignArray)
+ {
+       CSemValue value1;
+       ILType *elemType;
+       ILUInt32 arraySize;
+       ILUInt32 initSize;
+ 
+       /* Perform semantic analysis on the destination expression.
+          We expect it to be either an l-value or a decayed r-value */
+       value1 = ILNode_CSemAnalysis(node->expr1, info, &(node->expr1), 0);
+       if(CSemIsDecayed(value1))
+       {
+               /* Un-decay the array type */
+               CSemSetLValue(value1, CSemGetDecayedType(value1));
+       }
+       else if(!CSemIsLValue(value1))
+       {
+               if(!CSemIsError(value1))
+               {
+                       CCErrorOnLine(yygetfilename(node), yygetlinenum(node),
+                                                 _("invalid lvalue in 
assignment"));
+               }
+               return CSemValueError;
+       }
+ 
+       /* Determine the type of assignment based on the r-value */
+       if(yyisa(node->expr2, ILNode_CString))
+       {
+               /* Assign the contents of a string */
+               elemType = 
CTypeWithoutQuals(CTypeGetElemType(CSemGetType(value1)));
+               if(elemType != ILType_Int8 && elemType != ILType_UInt8)
+               {
+                       CCErrorOnLine(yygetfilename(node), yygetlinenum(node),
+                                                 _("invalid initializer"));
+                       return CSemValueError;
+               }
+               arraySize = CTypeGetNumElems(CSemGetType(value1));
+               initSize = (ILUInt32)(((ILNode_CString *)(node->expr2))->len);
+               if(initSize > arraySize)
+               {
+                       CCWarningOnLine(yygetfilename(node), yygetlinenum(node),
+                                   _("initializer-string for array of chars is 
too long"));
+               }
+       }
+       else if(yyisa(node->expr2, ILNode_ArrayInit))
+       {
+               /* TODO */
+               CCErrorOnLine(yygetfilename(node), yygetlinenum(node),
+                                         _("array/structure assignment is not 
implemented yet"));
+       }
+       else
+       {
+               /* The grammar already reported an error for this case */
+               return CSemValueError;
+       }
+ 
+       /* Array assignment is used at the statement level of a function */
+       return CSemValueDefault;
+ }
+ 
+ /*
+  * Generate discard code for an array assignment statement.
+  */
+ ILNode_GenDiscard(ILNode_CAssignArray)
+ {
+       /* TODO */
+ }
+ JavaGenDiscard(ILNode_CAssignArray)
+ {
+       /* Nothing to do here */
+ }
+ 
+ /*
   * Perform semantic analysis for the pre-increment expression.
   */

Index: c_stmt.tc
===================================================================
RCS file: /cvsroot/dotgnu-pnet/pnet/cscc/c/c_stmt.tc,v
retrieving revision 1.7
retrieving revision 1.8
diff -C2 -r1.7 -r1.8
*** c_stmt.tc   23 Aug 2002 09:44:27 -0000      1.7
--- c_stmt.tc   9 Dec 2002 00:28:19 -0000       1.8
***************
*** 149,153 ****
                        *(iter.last) = replace;
                }
!               if(!CSemIsVoid(value) && !CSemIsRValue(value))
                {
                        CCErrorOnLine(yygetfilename(node), yygetlinenum(node),
--- 149,153 ----
                        *(iter.last) = replace;
                }
!               if(!CSemIsVoid(value) && !CSemIsRValue(value) && 
!CSemIsError(value))
                {
                        CCErrorOnLine(yygetfilename(node), yygetlinenum(node),




reply via email to

[Prev in Thread] Current Thread [Next in Thread]