- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/Validate/InitializerLength.cpp
rb6f2e7ab r91a72ef 29 29 /// int x[] = { 1, 2, 3 }; 30 30 /// int y[][2] = { { 1, 2, 3 }, { 1, 2, 3 } }; 31 /// char z[] = "hello";32 31 /// here x and y are known at compile-time to have length 3, so change this into 33 32 /// int x[3] = { 1, 2, 3 }; 34 33 /// int y[3][2] = { { 1, 2, 3 }, { 1, 2, 3 } }; 35 /// char z[6] = "hello";36 34 struct InitializerLength { 37 35 const ast::ObjectDecl * previsit( const ast::ObjectDecl * decl ); 38 36 }; 39 37 40 ast::ConstantExpr * makeDimension( const ast::ObjectDecl * decl ) {41 if ( auto init = decl->init.as<ast::ListInit>() ) {42 return ast::ConstantExpr::from_ulong( decl->location, init->size() );43 } else if ( auto init = decl->init.as<ast::SingleInit>() ) {44 if ( auto constant = init->value.as<ast::ConstantExpr>() ) {45 if ( auto type = constant->result.as<ast::ArrayType>() ) {46 if ( auto dim = type->dimension.as<ast::ConstantExpr>() ) {47 ast::ConstantExpr * dimension = ast::deepCopy( dim );48 dimension->location = decl->location;49 return dimension;50 }51 }52 }53 }54 return nullptr;55 }56 57 38 const ast::ObjectDecl * InitializerLength::previsit( const ast::ObjectDecl * decl ) { 58 39 if ( auto type = decl->type.as<ast::ArrayType>() ) { 59 40 if ( type->dimension ) return decl; 60 if ( auto dimension = makeDimension( decl) ) {41 if ( auto init = decl->init.as<ast::ListInit>() ) { 61 42 ast::ObjectDecl * mutDecl = ast::mutate( decl ); 62 43 ast::ArrayType * mutType = ast::mutate( type ); 63 mutType->dimension = dimension; 44 mutType->dimension = ast::ConstantExpr::from_ulong( 45 mutDecl->location, init->size() ); 64 46 mutDecl->type = mutType; 65 47 return mutDecl;
Note: See TracChangeset
for help on using the changeset viewer.