Changeset 11ab8ea8 for src


Ignore:
Timestamp:
May 19, 2017, 3:44:49 PM (7 years ago)
Author:
Rob Schluntz <rschlunt@…>
Branches:
ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, deferred_resn, demangler, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, with_gc
Children:
88e0080
Parents:
d298e03
Message:

add checks for generic type parameter length

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/SymTab/Validate.cc

    rd298e03 r11ab8ea8  
    208208        };
    209209
     210        /// ensure that generic types have the correct number of type arguments
     211        class ValidateGenericParameters : public Visitor {
     212        public:
     213                typedef Visitor Parent;
     214                virtual void visit( StructInstType * inst ) final override;
     215                virtual void visit( UnionInstType * inst ) final override;
     216        };
     217
    210218        class ArrayLength : public Visitor {
    211219        public:
     
    235243                Pass3 pass3( 0 );
    236244                CompoundLiteral compoundliteral;
    237 
    238                 HoistStruct::hoistStruct( translationUnit );
     245                ValidateGenericParameters genericParams;
     246
    239247                EliminateTypedef::eliminateTypedef( translationUnit );
     248                HoistStruct::hoistStruct( translationUnit ); // must happen after EliminateTypedef, so that aggregate typedefs occur in the correct order
    240249                ReturnTypeFixer::fix( translationUnit ); // must happen before autogen
    241250                acceptAll( translationUnit, lrt ); // must happen before autogen, because sized flag needs to propagate to generated functions
     251                acceptAll( translationUnit, genericParams );  // check as early as possible - can't happen before LinkReferenceToTypes
    242252                acceptAll( translationUnit, epc ); // must happen before VerifyCtorDtorAssign, because void return objects should not exist
    243253                VerifyCtorDtorAssign::verify( translationUnit );  // must happen before autogen, because autogen examines existing ctor/dtors
     
    829839        }
    830840
     841        template< typename Aggr >
     842        void validateGeneric( Aggr * inst ) {
     843                std::list< TypeDecl * > * params = inst->get_baseParameters();
     844                if ( params != NULL ) {
     845                        std::list< Expression * > & args = inst->get_parameters();
     846                        if ( args.size() < params->size() ) throw SemanticError( "Too few type arguments in generic type ", inst );
     847                        if ( args.size() > params->size() ) throw SemanticError( "Too many type arguments in generic type ", inst );
     848                }
     849        }
     850
     851        void ValidateGenericParameters::visit( StructInstType * inst ) {
     852                validateGeneric( inst );
     853                Parent::visit( inst );
     854        }
     855
     856        void ValidateGenericParameters::visit( UnionInstType * inst ) {
     857                validateGeneric( inst );
     858                Parent::visit( inst );
     859        }
     860
    831861        DeclarationWithType * CompoundLiteral::mutate( ObjectDecl *objectDecl ) {
    832862                storageClasses = objectDecl->get_storageClasses();
Note: See TracChangeset for help on using the changeset viewer.