| 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 | 
 | 
|---|
| 20 | struct TypeData;
 | 
|---|
| 21 | class InitializerNode;
 | 
|---|
| 22 | 
 | 
|---|
| 23 | struct 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 |         DeclarationNode();
 | 
|---|
| 79 |         ~DeclarationNode();
 | 
|---|
| 80 |         DeclarationNode * clone() const override;
 | 
|---|
| 81 | 
 | 
|---|
| 82 |         DeclarationNode * addQualifiers( DeclarationNode * );
 | 
|---|
| 83 |         void checkQualifiers( const TypeData *, const TypeData * );
 | 
|---|
| 84 |         void checkSpecifiers( DeclarationNode * );
 | 
|---|
| 85 |         DeclarationNode * copySpecifiers( DeclarationNode * );
 | 
|---|
| 86 |         DeclarationNode * addType( DeclarationNode * );
 | 
|---|
| 87 |         DeclarationNode * addTypedef();
 | 
|---|
| 88 |         DeclarationNode * addEnumBase( DeclarationNode * );
 | 
|---|
| 89 |         DeclarationNode * addAssertions( DeclarationNode * );
 | 
|---|
| 90 |         DeclarationNode * addName( std::string * );
 | 
|---|
| 91 |         DeclarationNode * addAsmName( DeclarationNode * );
 | 
|---|
| 92 |         DeclarationNode * addBitfield( ExpressionNode * size );
 | 
|---|
| 93 |         DeclarationNode * addVarArgs();
 | 
|---|
| 94 |         DeclarationNode * addFunctionBody( StatementNode * body, ExpressionNode * with = nullptr );
 | 
|---|
| 95 |         DeclarationNode * addOldDeclList( DeclarationNode * list );
 | 
|---|
| 96 |         DeclarationNode * setBase( TypeData * newType );
 | 
|---|
| 97 |         DeclarationNode * copyAttribute( DeclarationNode * attr );
 | 
|---|
| 98 |         DeclarationNode * addPointer( DeclarationNode * qualifiers );
 | 
|---|
| 99 |         DeclarationNode * addArray( DeclarationNode * array );
 | 
|---|
| 100 |         DeclarationNode * addNewPointer( DeclarationNode * pointer );
 | 
|---|
| 101 |         DeclarationNode * addNewArray( DeclarationNode * array );
 | 
|---|
| 102 |         DeclarationNode * addParamList( DeclarationNode * list );
 | 
|---|
| 103 |         DeclarationNode * addIdList( DeclarationNode * list ); // old-style functions
 | 
|---|
| 104 |         DeclarationNode * addInitializer( InitializerNode * init );
 | 
|---|
| 105 |         DeclarationNode * addTypeInitializer( DeclarationNode * init );
 | 
|---|
| 106 | 
 | 
|---|
| 107 |         DeclarationNode * cloneType( std::string * newName );
 | 
|---|
| 108 |         DeclarationNode * cloneBaseType( DeclarationNode * newdecl );
 | 
|---|
| 109 | 
 | 
|---|
| 110 |         DeclarationNode * appendList( DeclarationNode * node ) {
 | 
|---|
| 111 |                 return (DeclarationNode *)set_last( node );
 | 
|---|
| 112 |         }
 | 
|---|
| 113 | 
 | 
|---|
| 114 |         virtual void print( __attribute__((unused)) std::ostream & os, __attribute__((unused)) int indent = 0 ) const override;
 | 
|---|
| 115 |         virtual void printList( __attribute__((unused)) std::ostream & os, __attribute__((unused)) int indent = 0 ) const override;
 | 
|---|
| 116 | 
 | 
|---|
| 117 |         ast::Decl * build() const;
 | 
|---|
| 118 |         ast::Type * buildType() const;
 | 
|---|
| 119 | 
 | 
|---|
| 120 |         ast::Linkage::Spec get_linkage() const { return linkage; }
 | 
|---|
| 121 |         DeclarationNode * extractAggregate() const;
 | 
|---|
| 122 |         bool has_enumeratorValue() const { return (bool)enumeratorValue; }
 | 
|---|
| 123 |         ExpressionNode * consume_enumeratorValue() const { return const_cast<DeclarationNode *>(this)->enumeratorValue.release(); }
 | 
|---|
| 124 | 
 | 
|---|
| 125 |         bool get_extension() const { return extension; }
 | 
|---|
| 126 |         DeclarationNode * set_extension( bool exten ) { extension = exten; return this; }
 | 
|---|
| 127 | 
 | 
|---|
| 128 |         bool get_inLine() const { return inLine; }
 | 
|---|
| 129 |         DeclarationNode * set_inLine( bool inL ) { inLine = inL; return this; }
 | 
|---|
| 130 | 
 | 
|---|
| 131 |         DeclarationNode * get_last() { return (DeclarationNode *)ParseNode::get_last(); }
 | 
|---|
| 132 | 
 | 
|---|
| 133 |         struct Variable_t {
 | 
|---|
| 134 | //              const std::string * name;
 | 
|---|
| 135 |                 ast::TypeDecl::Kind tyClass;
 | 
|---|
| 136 |                 DeclarationNode * assertions;
 | 
|---|
| 137 |                 DeclarationNode * initializer;
 | 
|---|
| 138 |         };
 | 
|---|
| 139 |         Variable_t variable;
 | 
|---|
| 140 | 
 | 
|---|
| 141 |         struct StaticAssert_t {
 | 
|---|
| 142 |                 ExpressionNode * condition;
 | 
|---|
| 143 |                 ast::Expr * message;
 | 
|---|
| 144 |         };
 | 
|---|
| 145 |         StaticAssert_t assert;
 | 
|---|
| 146 | 
 | 
|---|
| 147 |         BuiltinType builtin = NoBuiltinType;
 | 
|---|
| 148 | 
 | 
|---|
| 149 |         TypeData * type = nullptr;
 | 
|---|
| 150 | 
 | 
|---|
| 151 |         bool inLine = false;
 | 
|---|
| 152 |         bool enumInLine = false;
 | 
|---|
| 153 |         ast::Function::Specs funcSpecs;
 | 
|---|
| 154 |         ast::Storage::Classes storageClasses;
 | 
|---|
| 155 | 
 | 
|---|
| 156 |         ExpressionNode * bitfieldWidth = nullptr;
 | 
|---|
| 157 |         std::unique_ptr<ExpressionNode> enumeratorValue;
 | 
|---|
| 158 |         bool hasEllipsis = false;
 | 
|---|
| 159 |         ast::Linkage::Spec linkage;
 | 
|---|
| 160 |         ast::Expr * asmName = nullptr;
 | 
|---|
| 161 |         std::vector<ast::ptr<ast::Attribute>> attributes;
 | 
|---|
| 162 |         InitializerNode * initializer = nullptr;
 | 
|---|
| 163 |         bool extension = false;
 | 
|---|
| 164 |         std::string error;
 | 
|---|
| 165 |         StatementNode * asmStmt = nullptr;
 | 
|---|
| 166 |         StatementNode * directiveStmt = nullptr;
 | 
|---|
| 167 | 
 | 
|---|
| 168 |         static UniqueName anonymous;
 | 
|---|
| 169 | }; // DeclarationNode
 | 
|---|
| 170 | 
 | 
|---|
| 171 | ast::Type * buildType( TypeData * type );
 | 
|---|
| 172 | 
 | 
|---|
| 173 | static inline ast::Type * maybeMoveBuildType( const DeclarationNode * orig ) {
 | 
|---|
| 174 |         ast::Type * ret = orig ? orig->buildType() : nullptr;
 | 
|---|
| 175 |         delete orig;
 | 
|---|
| 176 |         return ret;
 | 
|---|
| 177 | }
 | 
|---|
| 178 | 
 | 
|---|
| 179 | // This generic buildList is here along side its overloads.
 | 
|---|
| 180 | template<typename AstType, typename NodeType,
 | 
|---|
| 181 |     template<typename, typename...> class Container, typename... Args>
 | 
|---|
| 182 | void buildList( NodeType * firstNode,
 | 
|---|
| 183 |         Container<ast::ptr<AstType>, Args...> & output ) {
 | 
|---|
| 184 |     SemanticErrorException errors;
 | 
|---|
| 185 |     std::back_insert_iterator<Container<ast::ptr<AstType>, Args...>> out( output );
 | 
|---|
| 186 |     NodeType * cur = firstNode;
 | 
|---|
| 187 | 
 | 
|---|
| 188 |     while ( cur ) {
 | 
|---|
| 189 |         try {
 | 
|---|
| 190 |             if ( auto result = dynamic_cast<AstType *>( maybeBuild( cur ) ) ) {
 | 
|---|
| 191 |                 *out++ = result;
 | 
|---|
| 192 |             } else {
 | 
|---|
| 193 |                 assertf(false, __PRETTY_FUNCTION__ );
 | 
|---|
| 194 |                 SemanticError( cur->location, "type specifier declaration in forall clause is currently unimplemented." );
 | 
|---|
| 195 |             } // if
 | 
|---|
| 196 |         } catch( SemanticErrorException & e ) {
 | 
|---|
| 197 |             errors.append( e );
 | 
|---|
| 198 |         } // try
 | 
|---|
| 199 |         ParseNode * temp = cur->get_next();
 | 
|---|
| 200 |         // Should not return nullptr, then it is non-homogeneous:
 | 
|---|
| 201 |         cur = dynamic_cast<NodeType *>( temp );
 | 
|---|
| 202 |         if ( !cur && temp ) {
 | 
|---|
| 203 |             SemanticError( temp->location, "internal error, non-homogeneous nodes founds in buildList processing." );
 | 
|---|
| 204 |         } // if
 | 
|---|
| 205 |     } // while
 | 
|---|
| 206 |     if ( ! errors.isEmpty() ) {
 | 
|---|
| 207 |         throw errors;
 | 
|---|
| 208 |     } // if
 | 
|---|
| 209 | }
 | 
|---|
| 210 | 
 | 
|---|
| 211 | void buildList( DeclarationNode * firstNode, std::vector<ast::ptr<ast::Decl>> & outputList );
 | 
|---|
| 212 | void buildList( DeclarationNode * firstNode, std::vector<ast::ptr<ast::DeclWithType>> & outputList );
 | 
|---|
| 213 | void buildTypeList( const DeclarationNode * firstNode, std::vector<ast::ptr<ast::Type>> & outputList );
 | 
|---|
| 214 | 
 | 
|---|
| 215 | template<typename AstType, typename NodeType,
 | 
|---|
| 216 | template<typename, typename...> class Container, typename... Args>
 | 
|---|
| 217 | void buildMoveList( NodeType * firstNode,
 | 
|---|
| 218 |                 Container<ast::ptr<AstType>, Args...> & output ) {
 | 
|---|
| 219 |         buildList<AstType, NodeType, Container, Args...>( firstNode, output );
 | 
|---|
| 220 |         delete firstNode;
 | 
|---|
| 221 | }
 | 
|---|