source: src/Validate/HoistTypeDecls.cpp @ 5c473c9

ADTast-experimental
Last change on this file since 5c473c9 was 11df881, checked in by Andrew Beach <ajbeach@…>, 2 years ago

Updated documentation on pre-resolver passes, moving code to headers instead of uses. Note that some comments were just copied over, I don't know if they are accurate.

  • Property mode set to 100644
File size: 2.1 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// HoistTypeDecls.cpp -- Hoists declarations of implicitly declared types.
8//
9// Author           : Andrew Beach
10// Created On       : Mon Jul  4  9:52:00 2022
11// Last Modified By : Andrew Beach
12// Last Modified On : Mon Jul 12 14:07:00 2022
13// Update Count     : 0
14//
15
16#include "HoistTypeDecls.hpp"
17
18#include "AST/Pass.hpp"
19
20namespace Validate {
21
22namespace {
23
24struct HoistTypeDecls final : public ast::WithDeclsToAdd<> {
25        void previsit( ast::SizeofExpr const * );
26        void previsit( ast::AlignofExpr const * );
27        void previsit( ast::UntypedOffsetofExpr const * );
28        void previsit( ast::CompoundLiteralExpr const * );
29        void handleType( ast::Type const * );
30};
31
32void HoistTypeDecls::previsit( ast::SizeofExpr const * expr ) {
33        handleType( expr->type );
34}
35
36void HoistTypeDecls::previsit( ast::AlignofExpr const * expr ) {
37        handleType( expr->type );
38}
39
40void HoistTypeDecls::previsit( ast::UntypedOffsetofExpr const * expr ) {
41        handleType( expr->type );
42}
43
44void HoistTypeDecls::previsit( ast::CompoundLiteralExpr const * expr ) {
45        handleType( expr->result );
46}
47
48void HoistTypeDecls::handleType( ast::Type const * type ) {
49        // Some type declarations are buried in expressions and not easy to
50        // hoist during parsing; so they are hoisted here instead.
51        // This also where some missing code locations come in.
52        ast::AggregateDecl const * aggr = nullptr;
53        if ( auto inst = dynamic_cast<ast::StructInstType const *>( type ) ) {
54                aggr = inst->base;
55        } else if ( auto inst = dynamic_cast<ast::UnionInstType const *>( type ) ) {
56                aggr = inst->base;
57        } else if ( auto inst = dynamic_cast<ast::EnumInstType const *>( type ) ) {
58                aggr = inst->base;
59        }
60        if ( aggr && aggr->body ) {
61                declsToAddBefore.push_front( aggr );
62        }
63}
64
65} // namespace
66
67void hoistTypeDecls( ast::TranslationUnit & translationUnit ) {
68        ast::Pass<HoistTypeDecls>::run( translationUnit );
69}
70
71} // namespace Validate
72
73// Local Variables: //
74// tab-width: 4 //
75// mode: c++ //
76// compile-command: "make install" //
77// End: //
Note: See TracBrowser for help on using the repository browser.