Changeset fbd7ad6


Ignore:
Timestamp:
Apr 4, 2017, 1:28:15 PM (4 years ago)
Author:
Rob Schluntz <rschlunt@…>
Branches:
aaron-thesis, arm-eh, cleanup-dtors, deferred_resn, demangler, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, resolv-new, with_gc
Children:
af68f0a
Parents:
7493339
Message:

compute and store array length when missing and known at compile-time

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/SymTab/Validate.cc

    r7493339 rfbd7ad6  
    208208        };
    209209
     210        class ArrayLength : public Visitor {
     211        public:
     212                /// for array types without an explicit length, compute the length and store it so that it
     213                /// is known to the rest of the phases. For example,
     214                ///   int x[] = { 1, 2, 3 };
     215                ///   int y[][2] = { { 1, 2, 3 }, { 1, 2, 3 } };
     216                /// here x and y are known at compile-time to have length 3, so change this into
     217                ///   int x[3] = { 1, 2, 3 };
     218                ///   int y[3][2] = { { 1, 2, 3 }, { 1, 2, 3 } };
     219                static void computeLength( std::list< Declaration * > & translationUnit );
     220
     221                virtual void visit( ObjectDecl * objDecl );
     222        };
     223
    210224        class CompoundLiteral final : public GenPoly::DeclMutator {
    211225                Type::StorageClasses storageClasses;
     
    235249                acceptAll( translationUnit, pass3 );
    236250                VerifyCtorDtorAssign::verify( translationUnit );
     251                ArrayLength::computeLength( translationUnit );
    237252        }
    238253
     
    869884                }
    870885        }
     886
     887        void ArrayLength::computeLength( std::list< Declaration * > & translationUnit ) {
     888                ArrayLength len;
     889                acceptAll( translationUnit, len );
     890        }
     891
     892        void ArrayLength::visit( ObjectDecl * objDecl ) {
     893                if ( ArrayType * at = dynamic_cast< ArrayType * >( objDecl->get_type() ) ) {
     894                        if ( at->get_dimension() != nullptr ) return;
     895                        if ( ListInit * init = dynamic_cast< ListInit * >( objDecl->get_init() ) ) {
     896                                at->set_dimension( new ConstantExpr( Constant::from_ulong( init->get_initializers().size() ) ) );
     897                        }
     898                }
     899        }
    871900} // namespace SymTab
    872901
Note: See TracChangeset for help on using the changeset viewer.