[ce36b55] | 1 | // |
---|
| 2 | // Cforall Version 1.0.0 Copyright (C) 2018 University of Waterloo |
---|
| 3 | // |
---|
| 4 | // The contents of this file are covered under the licence agreement in the |
---|
| 5 | // file "LICENCE" distributed with Cforall. |
---|
| 6 | // |
---|
| 7 | // InitializerLength.cpp -- Calculate the length of arrays from initializers. |
---|
| 8 | // |
---|
| 9 | // Author : Andrew Beach |
---|
| 10 | // Created On : Fri Nov 12 11:46:00 2021 |
---|
| 11 | // Last Modified By : Andrew Beach |
---|
| 12 | // Last Modified On : Fri Nov 12 13:35:00 2021 |
---|
| 13 | // Update Count : 0 |
---|
| 14 | // |
---|
| 15 | |
---|
[91a72ef] | 16 | #include "InitializerLength.hpp" |
---|
[ce36b55] | 17 | |
---|
| 18 | #include "AST/Expr.hpp" |
---|
| 19 | #include "AST/Decl.hpp" |
---|
| 20 | #include "AST/Pass.hpp" |
---|
| 21 | #include "AST/TranslationUnit.hpp" |
---|
| 22 | |
---|
| 23 | namespace Validate { |
---|
| 24 | |
---|
| 25 | namespace { |
---|
| 26 | |
---|
| 27 | /// for array types without an explicit length, compute the length and store it so that it |
---|
| 28 | /// is known to the rest of the phases. For example, |
---|
| 29 | /// int x[] = { 1, 2, 3 }; |
---|
| 30 | /// int y[][2] = { { 1, 2, 3 }, { 1, 2, 3 } }; |
---|
[b6f2e7ab] | 31 | /// char z[] = "hello"; |
---|
[ce36b55] | 32 | /// here x and y are known at compile-time to have length 3, so change this into |
---|
| 33 | /// int x[3] = { 1, 2, 3 }; |
---|
| 34 | /// int y[3][2] = { { 1, 2, 3 }, { 1, 2, 3 } }; |
---|
[b6f2e7ab] | 35 | /// char z[6] = "hello"; |
---|
[ce36b55] | 36 | struct InitializerLength { |
---|
| 37 | const ast::ObjectDecl * previsit( const ast::ObjectDecl * decl ); |
---|
| 38 | }; |
---|
| 39 | |
---|
[b6f2e7ab] | 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 | |
---|
[ce36b55] | 57 | const ast::ObjectDecl * InitializerLength::previsit( const ast::ObjectDecl * decl ) { |
---|
| 58 | if ( auto type = decl->type.as<ast::ArrayType>() ) { |
---|
| 59 | if ( type->dimension ) return decl; |
---|
[b6f2e7ab] | 60 | if ( auto dimension = makeDimension( decl ) ) { |
---|
[ce36b55] | 61 | ast::ObjectDecl * mutDecl = ast::mutate( decl ); |
---|
| 62 | ast::ArrayType * mutType = ast::mutate( type ); |
---|
[b6f2e7ab] | 63 | mutType->dimension = dimension; |
---|
[ce36b55] | 64 | mutDecl->type = mutType; |
---|
| 65 | return mutDecl; |
---|
| 66 | } |
---|
| 67 | } |
---|
| 68 | return decl; |
---|
| 69 | } |
---|
| 70 | |
---|
| 71 | } // namespace |
---|
| 72 | |
---|
| 73 | void setLengthFromInitializer( ast::TranslationUnit & translationUnit ) { |
---|
| 74 | ast::Pass<InitializerLength>::run( translationUnit ); |
---|
| 75 | } |
---|
| 76 | |
---|
| 77 | } // namespace Validate |
---|
| 78 | |
---|
| 79 | // Local Variables: // |
---|
| 80 | // tab-width: 4 // |
---|
| 81 | // mode: c++ // |
---|
| 82 | // compile-command: "make install" // |
---|
| 83 | // End: // |
---|