source: src/Parser/DeclarationNode.h @ fa2c005

ADT
Last change on this file since fa2c005 was fa2c005, checked in by JiadaL <j82liang@…>, 12 months ago

Finish Adt POC

  • Property mode set to 100644
File size: 10.3 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 : Andrew Beach
12// Last Modified On : Wed Apr  5 11:55:00 2023
13// Update Count     : 0
14//
15
16#pragma once
17
18#include "ParseNode.h"
19
20struct TypeData;
21class InitializerNode;
22
23struct DeclarationNode : public ParseNode {
24        // These enumerations must harmonize with their names in DeclarationNode.cc.
25        enum BasicType {
26                Void, Bool, Char, Int, Int128,
27                Float, Double, LongDouble, uuFloat80, uuFloat128,
28                uFloat16, uFloat32, uFloat32x, uFloat64, uFloat64x, uFloat128, uFloat128x,
29                NoBasicType
30        };
31        static const char * basicTypeNames[];
32        enum ComplexType { Complex, NoComplexType, Imaginary };
33        // Imaginary unsupported => parse, but make invisible and print error message
34        static const char * complexTypeNames[];
35        enum Signedness { Signed, Unsigned, NoSignedness };
36        static const char * signednessNames[];
37        enum Length { Short, Long, LongLong, NoLength };
38        static const char * lengthNames[];
39        enum BuiltinType { Valist, AutoType, Zero, One, NoBuiltinType };
40        static const char * builtinTypeNames[];
41
42        static DeclarationNode * newStorageClass( ast::Storage::Classes );
43        static DeclarationNode * newFuncSpecifier( ast::Function::Specs );
44        static DeclarationNode * newTypeQualifier( ast::CV::Qualifiers );
45        static DeclarationNode * newBasicType( BasicType );
46        static DeclarationNode * newComplexType( ComplexType );
47        static DeclarationNode * newSignedNess( Signedness );
48        static DeclarationNode * newLength( Length );
49        static DeclarationNode * newBuiltinType( BuiltinType );
50        static DeclarationNode * newForall( DeclarationNode * );
51        static DeclarationNode * newFromTypedef( const std::string * );
52        static DeclarationNode * newFromGlobalScope();
53        static DeclarationNode * newQualifiedType( DeclarationNode *, DeclarationNode * );
54        static DeclarationNode * newFunction( const std::string * name, DeclarationNode * ret, DeclarationNode * param, StatementNode * body );
55        static DeclarationNode * newAggregate( ast::AggregateDecl::Aggregate kind, const std::string * name, ExpressionNode * actuals, DeclarationNode * fields, bool body );
56        static DeclarationNode * newEnum( const std::string * name, DeclarationNode * constants, bool body, bool typed, DeclarationNode * base = nullptr, EnumHiding hiding = EnumHiding::Visible );
57        static DeclarationNode * newEnumConstant( const std::string * name, ExpressionNode * constant );
58        static DeclarationNode * newEnumValueGeneric( const std::string * name, InitializerNode * init );
59        static DeclarationNode * newEnumInLine( const std::string name );
60        static DeclarationNode * newName( const std::string * );
61        static DeclarationNode * newFromTypeGen( const std::string *, ExpressionNode * params );
62        static DeclarationNode * newTypeParam( ast::TypeDecl::Kind, const std::string * );
63        static DeclarationNode * newTrait( const std::string * name, DeclarationNode * params, DeclarationNode * asserts );
64        static DeclarationNode * newTraitUse( const std::string * name, ExpressionNode * params );
65        static DeclarationNode * newTypeDecl( const std::string * name, DeclarationNode * typeParams );
66        static DeclarationNode * newPointer( DeclarationNode * qualifiers, OperKinds kind );
67        static DeclarationNode * newArray( ExpressionNode * size, DeclarationNode * qualifiers, bool isStatic );
68        static DeclarationNode * newVarArray( DeclarationNode * qualifiers );
69        static DeclarationNode * newBitfield( ExpressionNode * size );
70        static DeclarationNode * newTuple( DeclarationNode * members );
71        static DeclarationNode * newTypeof( ExpressionNode * expr, bool basetypeof = false );
72        static DeclarationNode * newVtableType( DeclarationNode * expr );
73        static DeclarationNode * newAttribute( const std::string *, ExpressionNode * expr = nullptr ); // gcc attributes
74        static DeclarationNode * newDirectiveStmt( StatementNode * stmt ); // gcc external directive statement
75        static DeclarationNode * newAsmStmt( StatementNode * stmt ); // gcc external asm statement
76        static DeclarationNode * newStaticAssert( ExpressionNode * condition, ast::Expr * message );
77
78        // Experimental algebric data type
79        static DeclarationNode * newAdt( const std::string * name, DeclarationNode * constructors );
80        static DeclarationNode * newDataConstructor( const std::string * name );
81        // static DeclarationNode * newDataConstructor( const std::string * name, DeclarationNode * typeSpecifiers );
82
83        DeclarationNode();
84        ~DeclarationNode();
85        DeclarationNode * clone() const override;
86
87        DeclarationNode * addQualifiers( DeclarationNode * );
88        void checkQualifiers( const TypeData *, const TypeData * );
89        void checkSpecifiers( DeclarationNode * );
90        DeclarationNode * copySpecifiers( DeclarationNode * );
91        DeclarationNode * addType( DeclarationNode * );
92        DeclarationNode * addTypedef();
93        DeclarationNode * addEnumBase( DeclarationNode * );
94        DeclarationNode * addAssertions( DeclarationNode * );
95        DeclarationNode * addName( std::string * );
96        DeclarationNode * addAsmName( DeclarationNode * );
97        DeclarationNode * addBitfield( ExpressionNode * size );
98        DeclarationNode * addVarArgs();
99        DeclarationNode * addFunctionBody( StatementNode * body, ExpressionNode * with = nullptr );
100        DeclarationNode * addOldDeclList( DeclarationNode * list );
101        DeclarationNode * setBase( TypeData * newType );
102        DeclarationNode * copyAttribute( DeclarationNode * attr );
103        DeclarationNode * addPointer( DeclarationNode * qualifiers );
104        DeclarationNode * addArray( DeclarationNode * array );
105        DeclarationNode * addNewPointer( DeclarationNode * pointer );
106        DeclarationNode * addNewArray( DeclarationNode * array );
107        DeclarationNode * addParamList( DeclarationNode * list );
108        DeclarationNode * addIdList( DeclarationNode * list ); // old-style functions
109        DeclarationNode * addInitializer( InitializerNode * init );
110        DeclarationNode * addTypeInitializer( DeclarationNode * init );
111
112        DeclarationNode * cloneType( std::string * newName );
113        DeclarationNode * cloneBaseType( DeclarationNode * newdecl );
114
115        DeclarationNode * appendList( DeclarationNode * node ) {
116                return (DeclarationNode *)set_last( node );
117        }
118
119        virtual void print( __attribute__((unused)) std::ostream & os, __attribute__((unused)) int indent = 0 ) const override;
120        virtual void printList( __attribute__((unused)) std::ostream & os, __attribute__((unused)) int indent = 0 ) const override;
121
122        ast::Decl * build() const;
123        ast::Type * buildType() const;
124
125        ast::Linkage::Spec get_linkage() const { return linkage; }
126        DeclarationNode * extractAggregate() const;
127        bool has_enumeratorValue() const { return (bool)enumeratorValue; }
128        ExpressionNode * consume_enumeratorValue() const { return const_cast<DeclarationNode *>(this)->enumeratorValue.release(); }
129
130        bool get_extension() const { return extension; }
131        DeclarationNode * set_extension( bool exten ) { extension = exten; return this; }
132
133        bool get_inLine() const { return inLine; }
134        DeclarationNode * set_inLine( bool inL ) { inLine = inL; return this; }
135
136        DeclarationNode * get_last() { return (DeclarationNode *)ParseNode::get_last(); }
137
138        struct Variable_t {
139//              const std::string * name;
140                ast::TypeDecl::Kind tyClass;
141                DeclarationNode * assertions;
142                DeclarationNode * initializer;
143        };
144        Variable_t variable;
145
146        struct StaticAssert_t {
147                ExpressionNode * condition;
148                ast::Expr * message;
149        };
150        StaticAssert_t assert;
151
152        BuiltinType builtin = NoBuiltinType;
153
154        TypeData * type = nullptr;
155
156        bool inLine = false;
157        bool enumInLine = false;
158        ast::Function::Specs funcSpecs;
159        ast::Storage::Classes storageClasses;
160
161        ExpressionNode * bitfieldWidth = nullptr;
162        std::unique_ptr<ExpressionNode> enumeratorValue;
163
164        bool hasEllipsis = false;
165        ast::Linkage::Spec linkage;
166        ast::Expr * asmName = nullptr;
167        std::vector<ast::ptr<ast::Attribute>> attributes;
168        InitializerNode * initializer = nullptr;
169        bool extension = false;
170        std::string error;
171        StatementNode * asmStmt = nullptr;
172        StatementNode * directiveStmt = nullptr;
173
174        static UniqueName anonymous;
175}; // DeclarationNode
176
177ast::Type * buildType( TypeData * type );
178
179static inline ast::Type * maybeMoveBuildType( const DeclarationNode * orig ) {
180        ast::Type * ret = orig ? orig->buildType() : nullptr;
181        delete orig;
182        return ret;
183}
184
185template<typename NodeType>
186NodeType * strict_next( NodeType * node ) {
187        ParseNode * next = node->get_next();
188        if ( nullptr == next ) return nullptr;
189        if ( NodeType * ret = dynamic_cast<NodeType *>( next ) ) return ret;
190        SemanticError( next->location, "internal error, non-homogeneous nodes founds in buildList processing." );
191}
192
193// This generic buildList is here along side its overloads.
194template<typename AstType, typename NodeType,
195                template<typename, typename...> class Container, typename... Args>
196void buildList( NodeType * firstNode,
197                Container<ast::ptr<AstType>, Args...> & output ) {
198        SemanticErrorException errors;
199        std::back_insert_iterator<Container<ast::ptr<AstType>, Args...>> out( output );
200
201        for ( NodeType * cur = firstNode ; cur ; cur = strict_next( cur ) ) {
202                try {
203                        AstType * node = dynamic_cast<AstType *>( maybeBuild( cur ) );
204                        assertf( node, "buildList: Did not build node of correct type." );
205                        *out++ = node;
206                } catch ( SemanticErrorException & e ) {
207                        errors.append( e );
208                } // try
209        } // for
210        if ( ! errors.isEmpty() ) {
211                throw errors;
212        } // if
213}
214
215void buildList( DeclarationNode * firstNode, std::vector<ast::ptr<ast::Decl>> & outputList );
216void buildList( DeclarationNode * firstNode, std::vector<ast::ptr<ast::DeclWithType>> & outputList );
217void buildTypeList( const DeclarationNode * firstNode, std::vector<ast::ptr<ast::Type>> & outputList );
218
219std::vector<ast::ptr<ast::StructDecl>> buildDataConstructors( DeclarationNode * firstNode );
220void buildDataConstructorsAsMember( DeclarationNode * firstNode, ast::AdtDecl * adtDecl );
221ast::UnionDecl * buildDataUnion( const CodeLocation & loc, const std::vector<ast::ptr<ast::Decl>> & typeList );
222ast::EnumDecl * buildTag( const CodeLocation & loc, std::vector<ast::ptr<ast::Decl>> & typeList );
223ast::StructDecl * buildTaggedUnions( const TypeData * data, const ast::EnumDecl * tag, const ast::UnionDecl * data_union );
224
225template<typename AstType, typename NodeType,
226                template<typename, typename...> class Container, typename... Args>
227void buildMoveList( NodeType * firstNode,
228                Container<ast::ptr<AstType>, Args...> & output ) {
229        buildList<AstType, NodeType, Container, Args...>( firstNode, output );
230        delete firstNode;
231}
Note: See TracBrowser for help on using the repository browser.