source: src/Parser/DeclarationNode.h @ 2d82999

Last change on this file since 2d82999 was e048ece, checked in by Andrew Beach <ajbeach@…>, 3 months ago

Moved the DeclarationNode? enums over to TypeData? where they are actually used.

  • Property mode set to 100644
File size: 7.6 KB
Line 
1//
2// Cforall Version 1.0.0 Copyright (C) 2015 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// DeclarationNode.h --
8//
9// Author           : Andrew Beach
10// Created On       : Wed Apr  5 11:38:00 2023
11// Last Modified By : Peter A. Buhr
12// Last Modified On : Sat Feb 17 09:24:12 2024
13// Update Count     : 4
14//
15
16#pragma once
17
18#include "ParseNode.h"
19
20struct TypeData;
21struct InitializerNode;
22
23struct DeclarationNode final : public ParseList<DeclarationNode> {
24        static DeclarationNode * newFromTypeData( TypeData * );
25        static DeclarationNode * newStorageClass( ast::Storage::Classes );
26        static DeclarationNode * newFuncSpecifier( ast::Function::Specs );
27        static DeclarationNode * newFunction( const std::string * name, DeclarationNode * ret, DeclarationNode * param, StatementNode * body );
28        static DeclarationNode * newAggregate( ast::AggregateDecl::Aggregate kind, const std::string * name, ExpressionNode * actuals, DeclarationNode * fields, bool body );
29        static DeclarationNode * newEnum( const std::string * name, DeclarationNode * constants, bool body, bool typed, DeclarationNode * base = nullptr, EnumHiding hiding = EnumHiding::Visible );
30        static DeclarationNode * newEnumConstant( const std::string * name, ExpressionNode * constant );
31        static DeclarationNode * newEnumValueGeneric( const std::string * name, InitializerNode * init );
32        static DeclarationNode * newEnumInLine( const std::string name );
33        static DeclarationNode * newName( const std::string * );
34        static DeclarationNode * newTypeParam( ast::TypeDecl::Kind, const std::string * );
35        static DeclarationNode * newTrait( const std::string * name, DeclarationNode * params, DeclarationNode * asserts );
36        static DeclarationNode * newTraitUse( const std::string * name, ExpressionNode * params );
37        static DeclarationNode * newTypeDecl( const std::string * name, DeclarationNode * typeParams );
38        static DeclarationNode * newPointer( DeclarationNode * qualifiers, OperKinds kind );
39        static DeclarationNode * newArray( ExpressionNode * size, DeclarationNode * qualifiers, bool isStatic );
40        static DeclarationNode * newVarArray( DeclarationNode * qualifiers );
41        static DeclarationNode * newBitfield( ExpressionNode * size );
42        static DeclarationNode * newTuple( DeclarationNode * members );
43        static DeclarationNode * newTypeof( ExpressionNode * expr, bool basetypeof = false );
44        static DeclarationNode * newAttribute( const std::string *, ExpressionNode * expr = nullptr ); // gcc attributes
45        static DeclarationNode * newDirectiveStmt( StatementNode * stmt ); // gcc external directive statement
46        static DeclarationNode * newAsmStmt( StatementNode * stmt ); // gcc external asm statement
47        static DeclarationNode * newStaticAssert( ExpressionNode * condition, ast::Expr * message );
48
49        DeclarationNode();
50        ~DeclarationNode();
51        DeclarationNode * clone() const override;
52
53        DeclarationNode * addQualifiers( DeclarationNode * );
54        void checkQualifiers( const TypeData *, const TypeData * );
55        void checkSpecifiers( DeclarationNode * );
56        DeclarationNode * copySpecifiers( DeclarationNode *, bool = true );
57        DeclarationNode * addType( DeclarationNode *, bool = true );
58        DeclarationNode * addTypedef();
59        DeclarationNode * addEnumBase( DeclarationNode * );
60        DeclarationNode * addAssertions( DeclarationNode * );
61        DeclarationNode * addName( std::string * );
62        DeclarationNode * addAsmName( DeclarationNode * );
63        DeclarationNode * addBitfield( ExpressionNode * size );
64        DeclarationNode * addVarArgs();
65        DeclarationNode * addFunctionBody( StatementNode * body, ExpressionNode * with = nullptr );
66        DeclarationNode * addOldDeclList( DeclarationNode * list );
67        DeclarationNode * setBase( TypeData * newType );
68        DeclarationNode * copyAttribute( DeclarationNode * attr );
69        DeclarationNode * addPointer( DeclarationNode * qualifiers );
70        DeclarationNode * addArray( DeclarationNode * array );
71        DeclarationNode * addNewPointer( DeclarationNode * pointer );
72        DeclarationNode * addNewArray( DeclarationNode * array );
73        DeclarationNode * addParamList( DeclarationNode * list );
74        DeclarationNode * addIdList( DeclarationNode * list ); // old-style functions
75        DeclarationNode * addInitializer( InitializerNode * init );
76        DeclarationNode * addTypeInitializer( DeclarationNode * init );
77
78        DeclarationNode * cloneType( std::string * newName );
79        DeclarationNode * cloneBaseType( DeclarationNode * newdecl, bool = true );
80
81        virtual void print( __attribute__((unused)) std::ostream & os, __attribute__((unused)) int indent = 0 ) const override;
82        virtual void printList( __attribute__((unused)) std::ostream & os, __attribute__((unused)) int indent = 0 ) const override;
83
84        ast::Decl * build() const;
85        ast::Type * buildType() const;
86
87        ast::Linkage::Spec get_linkage() const { return linkage; }
88        DeclarationNode * extractAggregate() const;
89        bool has_enumeratorValue() const { return (bool)enumeratorValue; }
90        ExpressionNode * consume_enumeratorValue() const { return const_cast<DeclarationNode *>(this)->enumeratorValue.release(); }
91
92        bool get_extension() const { return extension; }
93        DeclarationNode * set_extension( bool exten ) { extension = exten; return this; }
94
95        bool get_inLine() const { return inLine; }
96        DeclarationNode * set_inLine( bool inL ) { inLine = inL; return this; }
97
98        const std::string * name = nullptr;
99
100        struct Variable_t {
101//              const std::string * name;
102                ast::TypeDecl::Kind tyClass;
103                DeclarationNode * assertions;
104                DeclarationNode * initializer;
105        };
106        Variable_t variable;
107
108        struct StaticAssert_t {
109                ExpressionNode * condition;
110                ast::Expr * message;
111        };
112        StaticAssert_t assert;
113
114        TypeData * type = nullptr;
115
116        bool inLine = false;
117        bool enumInLine = false;
118        ast::Function::Specs funcSpecs;
119        ast::Storage::Classes storageClasses;
120
121        ExpressionNode * bitfieldWidth = nullptr;
122        std::unique_ptr<ExpressionNode> enumeratorValue;
123        bool hasEllipsis = false;
124        ast::Linkage::Spec linkage;
125        ast::Expr * asmName = nullptr;
126        std::vector<ast::ptr<ast::Attribute>> attributes;
127        InitializerNode * initializer = nullptr;
128        bool extension = false;
129        std::string error;
130        StatementNode * asmStmt = nullptr;
131        StatementNode * directiveStmt = nullptr;
132
133        static UniqueName anonymous;
134}; // DeclarationNode
135
136static inline ast::Type * maybeMoveBuildType( const DeclarationNode * orig ) {
137        ast::Type * ret = orig ? orig->buildType() : nullptr;
138        delete orig;
139        return ret;
140}
141
142// This generic buildList is here along side its overloads.
143template<typename AstType, typename NodeType,
144                template<typename, typename...> class Container, typename... Args>
145void buildList( NodeType * firstNode,
146                Container<ast::ptr<AstType>, Args...> & output ) {
147        SemanticErrorException errors;
148        std::back_insert_iterator<Container<ast::ptr<AstType>, Args...>> out( output );
149
150        for ( NodeType * cur = firstNode ; cur ; cur = cur->next ) {
151                try {
152                        AstType * node = dynamic_cast<AstType *>( maybeBuild( cur ) );
153                        assertf( node, "buildList: Did not build node of correct type." );
154                        *out++ = node;
155                } catch ( SemanticErrorException & e ) {
156                        errors.append( e );
157                } // try
158        } // for
159        if ( ! errors.isEmpty() ) {
160                throw errors;
161        } // if
162}
163
164void buildList( DeclarationNode * firstNode, std::vector<ast::ptr<ast::Decl>> & outputList );
165void buildList( DeclarationNode * firstNode, std::vector<ast::ptr<ast::DeclWithType>> & outputList );
166void buildTypeList( const DeclarationNode * firstNode, std::vector<ast::ptr<ast::Type>> & outputList );
167
168template<typename AstType, typename NodeType,
169                template<typename, typename...> class Container, typename... Args>
170void buildMoveList( NodeType * firstNode,
171                Container<ast::ptr<AstType>, Args...> & output ) {
172        buildList<AstType, NodeType, Container, Args...>( firstNode, output );
173        delete firstNode;
174}
Note: See TracBrowser for help on using the repository browser.