source: src/Validate/EnumAndPointerDecay.cpp

Last change on this file was 19a8c40, checked in by Andrew Beach <ajbeach@…>, 17 months ago

Some clean-up I had stashed from before I knew the InlineMemberDecl? clean-up was underway.

  • Property mode set to 100644
File size: 3.8 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// EnumAndPointerDecay.cpp -- Normalizes enumerations and types in functions.
8//
9// Author           : Andrew Beach
10// Created On       : Tue Jun 28 15:50:00 2022
11// Last Modified By : Andrew Beach
12// Last Modified On : Tue Sep 20 16:14:00 2022
13// Update Count     : 1
14//
15
16#include "EnumAndPointerDecay.hpp"
17
18#include "AST/CVQualifiers.hpp"
19#include "AST/Decl.hpp"
20#include "AST/Pass.hpp"
21#include "AST/Type.hpp"
22#include "SymTab/FixFunction.h"
23#include "Validate/NoIdSymbolTable.hpp"
24
25namespace Validate {
26
27namespace {
28
29struct EnumAndPointerDecayCore final : public WithNoIdSymbolTable, public ast::WithCodeLocation {
30        ast::EnumDecl const * previsit( ast::EnumDecl const * decl );
31        ast::FunctionDecl const * previsit( ast::FunctionDecl const * decl );
32        ast::FunctionType const * previsit( ast::FunctionType const * type );
33};
34
35ast::EnumDecl const * EnumAndPointerDecayCore::previsit(
36                ast::EnumDecl const * decl ) {
37        if ( decl->members.empty() ) {
38                return decl;
39        }
40        // Set the type of each member of the enumeration to be EnumContant.
41        auto mut = ast::mutate( decl );
42        std::vector<ast::ptr<ast::Decl>> buffer;
43        for ( auto member : decl->members ) {
44                if ( ast::ObjectDecl const * object = member.as<ast::ObjectDecl>() ) {
45                        buffer.push_back( ast::mutate_field( object,
46                                &ast::ObjectDecl::type,
47                                new ast::EnumInstType( decl, ast::CV::Const ) ) );
48                } else if ( auto value = member.as<ast::InlineMemberDecl>() ) {
49                        if ( auto targetEnum = symtab.lookupEnum( value->name ) ) {
50                                for ( auto enumMember : targetEnum->members ) {
51                                        auto enumObject = enumMember.strict_as<ast::ObjectDecl>();
52                                        buffer.push_back( new ast::ObjectDecl(
53                                                // Get the location from the "inline" declaration.
54                                                value->location,
55                                                enumObject->name,
56                                                // Construct a new EnumInstType as the type.
57                                                new ast::EnumInstType( decl, ast::CV::Const ),
58                                                enumObject->init,
59                                                enumObject->storage,
60                                                enumObject->linkage,
61                                                enumObject->bitfieldWidth,
62                                                {},
63                                                enumObject->funcSpec
64                                        ) );
65                                }
66                        }
67                }
68        }
69        mut->members = buffer;
70        return mut;
71}
72
73template<typename Member>
74void fixFunctionList( CodeLocation const & location, bool isVarArgs,
75                std::vector<ast::ptr<Member>> & list ) {
76        bool hasVoid = false;
77        for ( ast::ptr<Member> & member : list ) {
78                member = SymTab::fixFunction( member, hasVoid );
79        }
80
81        // The remaining code only applies if void is present.
82        if ( !hasVoid ) {
83                return;
84        }
85
86        // So there is a void, which is only valid if it is the only argument.
87        if ( 1 < list.size() || isVarArgs ) {
88                SemanticError( location, "invalid type void in function type " );
89        }
90
91        // If a single "void" thing in the list to remove it.
92        list.clear();
93}
94
95ast::FunctionDecl const * EnumAndPointerDecayCore::previsit(
96                ast::FunctionDecl const * decl ) {
97        auto mut = ast::mutate( decl );
98        ast::ArgumentFlag isVarArgs = mut->type->isVarArgs;
99        // It seems fixFunction (via fixFunctionList) does the pointer decay part.
100        fixFunctionList( mut->location, isVarArgs, mut->params );
101        fixFunctionList( mut->location, false, mut->returns );
102        return mut;
103}
104
105ast::FunctionType const * EnumAndPointerDecayCore::previsit(
106                ast::FunctionType const * type ) {
107        assert( location );
108        auto mut = ast::mutate( type );
109        ast::ArgumentFlag isVarArgs = mut->isVarArgs;
110        // It seems fixFunction (via fixFunctionList) does the pointer decay part.
111        fixFunctionList( *location, isVarArgs, mut->params );
112        fixFunctionList( *location, false, mut->returns );
113        return mut;
114}
115
116} // namespace
117
118void decayEnumsAndPointers( ast::TranslationUnit & translationUnit ) {
119        ast::Pass<EnumAndPointerDecayCore>::run( translationUnit );
120}
121
122} // namespace Validate
123
124// Local Variables: //
125// tab-width: 4 //
126// mode: c++ //
127// compile-command: "make install" //
128// End: //
Note: See TracBrowser for help on using the repository browser.