source: src/Validate/InitializerLength.cpp @ 478dade

Last change on this file since 478dade was 91a72ef, checked in by Andrew Beach <ajbeach@…>, 3 years ago

Some assorted bits of clean-up. InitializerLength? was missing some pieces. Other than that it is white space fixes.

  • Property mode set to 100644
File size: 1.9 KB
Line 
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
16#include "InitializerLength.hpp"
17
18#include "AST/Expr.hpp"
19#include "AST/Decl.hpp"
20#include "AST/Pass.hpp"
21#include "AST/TranslationUnit.hpp"
22
23namespace Validate {
24
25namespace {
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 } };
31/// here x and y are known at compile-time to have length 3, so change this into
32///   int x[3] = { 1, 2, 3 };
33///   int y[3][2] = { { 1, 2, 3 }, { 1, 2, 3 } };
34struct InitializerLength {
35        const ast::ObjectDecl * previsit( const ast::ObjectDecl * decl );
36};
37
38const ast::ObjectDecl * InitializerLength::previsit( const ast::ObjectDecl * decl ) {
39        if ( auto type = decl->type.as<ast::ArrayType>() ) {
40                if ( type->dimension ) return decl;
41                if ( auto init = decl->init.as<ast::ListInit>() ) {
42                        ast::ObjectDecl * mutDecl = ast::mutate( decl );
43                        ast::ArrayType * mutType = ast::mutate( type );
44                        mutType->dimension = ast::ConstantExpr::from_ulong(
45                                mutDecl->location, init->size() );
46                        mutDecl->type = mutType;
47                        return mutDecl;
48                }
49        }
50        return decl;
51}
52
53} // namespace
54
55void setLengthFromInitializer( ast::TranslationUnit & translationUnit ) {
56        ast::Pass<InitializerLength>::run( translationUnit );
57}
58
59} // namespace Validate
60
61// Local Variables: //
62// tab-width: 4 //
63// mode: c++ //
64// compile-command: "make install" //
65// End: //
Note: See TracBrowser for help on using the repository browser.