source: src/SynTree/Declaration.h @ 34dcc474

new-envwith_gc
Last change on this file since 34dcc474 was 68f9c43, checked in by Aaron Moss <a3moss@…>, 6 years ago

First pass at delete removal

  • Property mode set to 100644
File size: 14.6 KB
RevLine 
[0dd3a2f]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//
[974906e2]7// Declaration.h --
[0dd3a2f]8//
9// Author           : Richard C. Bilson
10// Created On       : Mon May 18 07:44:20 2015
[e612146c]11// Last Modified By : Peter A. Buhr
12// Last Modified On : Sun Sep  3 19:24:06 2017
13// Update Count     : 131
[0dd3a2f]14//
15
[6b0b624]16#pragma once
[51b7345]17
[ea6332d]18#include <cassert>               // for assertf
19#include <iosfwd>                // for ostream
20#include <list>                  // for list
21#include <string>                // for string, operator+, allocator, to_string
22
23#include "BaseSyntaxNode.h"      // for BaseSyntaxNode
24#include "Mutator.h"             // for Mutator
25#include "Parser/LinkageSpec.h"  // for Spec, Cforall
26#include "Parser/ParseNode.h"    // for DeclarationNode, DeclarationNode::Ag...
27#include "SynTree.h"             // for UniqueId
28#include "SynTree/Type.h"        // for Type, Type::StorageClasses, Type::Fu...
29#include "Visitor.h"             // for Visitor
30
31class AsmStmt;
32class Attribute;
33class CompoundStmt;
34class ConstantExpr;
35class Expression;
36class Initializer;
37class TypeDecl;
[51b7345]38
[294647b]39class Declaration : public BaseSyntaxNode {
[17cd4eb]40  public:
[65cdc1e]41        std::string name;
42        LinkageSpec::Spec linkage;
43        bool extension = false;
44
[68fe077a]45        Declaration( const std::string &name, Type::StorageClasses scs, LinkageSpec::Spec linkage );
[0dd3a2f]46        Declaration( const Declaration &other );
47
[5f2f2d7]48        const std::string &get_name() const { return name; }
[0dd3a2f]49        void set_name( std::string newValue ) { name = newValue; }
[a7c90d4]50
[68fe077a]51        Type::StorageClasses get_storageClasses() const { return storageClasses; }
[a7c90d4]52
[8b7ee09]53        LinkageSpec::Spec get_linkage() const { return linkage; }
54        void set_linkage( LinkageSpec::Spec newValue ) { linkage = newValue; }
[a7c90d4]55
[0dd3a2f]56        UniqueId get_uniqueId() const { return uniqueId; }
[a7c90d4]57
[8e9cbb2]58        bool get_extension() const { return extension; }
59        Declaration *set_extension( bool exten ) { extension = exten; return this; }
[0dd3a2f]60
61        void fixUniqueId( void );
[fa16264]62        virtual Declaration *clone() const override = 0;
[e149f77]63        virtual void accept( Visitor &v ) override = 0;
[fa16264]64        virtual Declaration *acceptMutator( Mutator &m ) override = 0;
[50377a4]65        virtual void print( std::ostream &os, Indenter indent = {} ) const override = 0;
66        virtual void printShort( std::ostream &os, Indenter indent = {} ) const = 0;
[0dd3a2f]67
68        static void dumpIds( std::ostream &os );
69        static Declaration *declFromId( UniqueId id );
[65cdc1e]70
[17cd4eb]71  private:
[68fe077a]72        Type::StorageClasses storageClasses;
[0dd3a2f]73        UniqueId uniqueId;
[51b7345]74};
75
[17cd4eb]76class DeclarationWithType : public Declaration {
77  public:
[65cdc1e]78        // this represents the type with all types and typedefs expanded it is generated by SymTab::Validate::Pass2
79        std::string mangleName;
80        // need to remember the scope level at which the variable was declared, so that shadowed identifiers can be accessed
81        int scopeLevel = 0;
82
[e612146c]83        Expression *asmName;
[65cdc1e]84        std::list< Attribute * > attributes;
85
[ddfd945]86        DeclarationWithType( const std::string &name, Type::StorageClasses scs, LinkageSpec::Spec linkage, const std::list< Attribute * > & attributes, Type::FuncSpecifiers fs );
[0dd3a2f]87        DeclarationWithType( const DeclarationWithType &other );
[68f9c43]88       
[0dd3a2f]89        std::string get_mangleName() const { return mangleName; }
[58dd019]90        DeclarationWithType * set_mangleName( std::string newValue ) { mangleName = newValue; return this; }
[17cd4eb]91
[f326f99]92        std::string get_scopedMangleName() const { return mangleName + "_" + std::to_string(scopeLevel); }
93
94        int get_scopeLevel() const { return scopeLevel; }
[58dd019]95        DeclarationWithType * set_scopeLevel( int newValue ) { scopeLevel = newValue; return this; }
96
[e612146c]97        Expression *get_asmName() const { return asmName; }
98        DeclarationWithType * set_asmName( Expression *newValue ) { asmName = newValue; return this; }
[f326f99]99
[f9cebb5]100        std::list< Attribute * >& get_attributes() { return attributes; }
101        const std::list< Attribute * >& get_attributes() const { return attributes; }
102
[ddfd945]103        Type::FuncSpecifiers get_funcSpec() const { return fs; }
104        //void set_functionSpecifiers( Type::FuncSpecifiers newValue ) { fs = newValue; }
[dd020c0]105
[e149f77]106        virtual DeclarationWithType *clone() const override = 0;
107        virtual DeclarationWithType *acceptMutator( Mutator &m )  override = 0;
[17cd4eb]108
[9aaac6e9]109        virtual Type * get_type() const = 0;
[0dd3a2f]110        virtual void set_type(Type *) = 0;
[f9cebb5]111
[65cdc1e]112  private:
[ddfd945]113        Type::FuncSpecifiers fs;
[51b7345]114};
115
[17cd4eb]116class ObjectDecl : public DeclarationWithType {
[0dd3a2f]117        typedef DeclarationWithType Parent;
[17cd4eb]118  public:
[65cdc1e]119        Type *type;
120        Initializer *init;
121        Expression *bitfieldWidth;
122
[68fe077a]123        ObjectDecl( const std::string &name, Type::StorageClasses scs, LinkageSpec::Spec linkage, Expression *bitfieldWidth, Type *type, Initializer *init,
[ddfd945]124                                const std::list< Attribute * > attributes = std::list< Attribute * >(), Type::FuncSpecifiers fs = Type::FuncSpecifiers() );
[0dd3a2f]125        ObjectDecl( const ObjectDecl &other );
126
[e149f77]127        virtual Type * get_type() const override { return type; }
128        virtual void set_type(Type *newType) override { type = newType; }
[0dd3a2f]129
130        Initializer *get_init() const { return init; }
131        void set_init( Initializer *newValue ) { init = newValue; }
[58dd019]132
[0dd3a2f]133        Expression *get_bitfieldWidth() const { return bitfieldWidth; }
134        void set_bitfieldWidth( Expression *newValue ) { bitfieldWidth = newValue; }
135
[96f9ef5]136        static ObjectDecl * newObject( const std::string & name, Type * type, Initializer * init );
137
[e149f77]138        virtual ObjectDecl *clone() const override { return new ObjectDecl( *this ); }
139        virtual void accept( Visitor &v ) override { v.visit( this ); }
140        virtual DeclarationWithType *acceptMutator( Mutator &m )  override { return m.mutate( this ); }
[50377a4]141        virtual void print( std::ostream &os, Indenter indent = {} ) const override;
142        virtual void printShort( std::ostream &os, Indenter indent = {} ) const override;
[51b7345]143};
144
[17cd4eb]145class FunctionDecl : public DeclarationWithType {
[0dd3a2f]146        typedef DeclarationWithType Parent;
[17cd4eb]147  public:
[65cdc1e]148        FunctionType *type;
149        CompoundStmt *statements;
[574894d]150        std::list< Expression * > withExprs;
[65cdc1e]151
[68fe077a]152        FunctionDecl( const std::string &name, Type::StorageClasses scs, LinkageSpec::Spec linkage, FunctionType *type, CompoundStmt *statements,
[ddfd945]153                                  const std::list< Attribute * > attributes = std::list< Attribute * >(), Type::FuncSpecifiers fs = Type::FuncSpecifiers() );
[0dd3a2f]154        FunctionDecl( const FunctionDecl &other );
[51b7345]155
[e149f77]156        virtual Type * get_type() const override { return type; }
157        virtual void set_type(Type * t) override { type = strict_dynamic_cast< FunctionType* >( t ); }
[51b7345]158
[9aaac6e9]159        FunctionType * get_functionType() const { return type; }
[0dd3a2f]160        void set_functionType( FunctionType *newValue ) { type = newValue; }
161        CompoundStmt *get_statements() const { return statements; }
162        void set_statements( CompoundStmt *newValue ) { statements = newValue; }
163
[75626a1]164        static FunctionDecl * newFunction( const std::string & name, FunctionType * type, CompoundStmt * statements );
165
[e149f77]166        virtual FunctionDecl *clone() const override { return new FunctionDecl( *this ); }
167        virtual void accept( Visitor &v ) override { v.visit( this ); }
168        virtual DeclarationWithType *acceptMutator( Mutator &m )  override { return m.mutate( this ); }
[50377a4]169        virtual void print( std::ostream &os, Indenter indent = {} ) const override;
170        virtual void printShort( std::ostream &os, Indenter indent = {} ) const override;
[51b7345]171};
172
[17cd4eb]173class NamedTypeDecl : public Declaration {
[0dd3a2f]174        typedef Declaration Parent;
[17cd4eb]175  public:
[65cdc1e]176        Type *base;
177        std::list< TypeDecl* > parameters;
178        std::list< DeclarationWithType* > assertions;
179
[68fe077a]180        NamedTypeDecl( const std::string &name, Type::StorageClasses scs, Type *type );
[46f6134]181        NamedTypeDecl( const NamedTypeDecl &other );
[0dd3a2f]182
183        Type *get_base() const { return base; }
184        void set_base( Type *newValue ) { base = newValue; }
185        std::list< TypeDecl* >& get_parameters() { return parameters; }
186        std::list< DeclarationWithType* >& get_assertions() { return assertions; }
187
[e39241b]188        virtual std::string typeString() const = 0;
189
[e149f77]190        virtual NamedTypeDecl *clone() const override = 0;
[50377a4]191        virtual void print( std::ostream &os, Indenter indent = {} ) const override;
192        virtual void printShort( std::ostream &os, Indenter indent = {} ) const override;
[51b7345]193};
194
[17cd4eb]195class TypeDecl : public NamedTypeDecl {
[0dd3a2f]196        typedef NamedTypeDecl Parent;
[17cd4eb]197  public:
[f0ecf9b]198        enum Kind { Dtype, Ftype, Ttype };
[65cdc1e]199
200        Type * init;
201        bool sized;
202
[2c57025]203        /// Data extracted from a type decl
204        struct Data {
205                TypeDecl::Kind kind;
206                bool isComplete;
207                Data() : kind( (TypeDecl::Kind)-1 ), isComplete( false ) {}
208                Data( TypeDecl * typeDecl ) : Data( typeDecl->get_kind(), typeDecl->isComplete() ) {}
209                Data( Kind kind, bool isComplete ) : kind( kind ), isComplete( isComplete ) {}
210                bool operator==(const Data & other) const { return kind == other.kind && isComplete == other.isComplete; }
211                bool operator!=(const Data & other) const { return !(*this == other);}
212        };
[51b7345]213
[f0ecf9b]214        TypeDecl( const std::string &name, Type::StorageClasses scs, Type *type, Kind kind, bool sized, Type * init = nullptr );
[0dd3a2f]215        TypeDecl( const TypeDecl &other );
[17cd4eb]216
[0dd3a2f]217        Kind get_kind() const { return kind; }
[51b7345]218
[67cf18c]219        Type * get_init() const { return init; }
220        TypeDecl * set_init( Type * newValue ) { init = newValue; return this; }
221
[f0ecf9b]222        bool isComplete() const { return sized; }
[2c57025]223        bool get_sized() const { return sized; }
224        TypeDecl * set_sized( bool newValue ) { sized = newValue; return this; }
225
[e149f77]226        virtual std::string typeString() const override;
[bdd0755]227        virtual std::string genTypeString() const;
[e39241b]228
[e149f77]229        virtual TypeDecl *clone() const override { return new TypeDecl( *this ); }
230        virtual void accept( Visitor &v ) override { v.visit( this ); }
231        virtual Declaration *acceptMutator( Mutator &m )  override { return m.mutate( this ); }
[50377a4]232        virtual void print( std::ostream &os, Indenter indent = {} ) const override;
[67cf18c]233
[17cd4eb]234  private:
[0dd3a2f]235        Kind kind;
[51b7345]236};
237
[17cd4eb]238class TypedefDecl : public NamedTypeDecl {
[0dd3a2f]239        typedef NamedTypeDecl Parent;
[17cd4eb]240  public:
[0b0f1dd]241        TypedefDecl( const std::string &name, CodeLocation location, Type::StorageClasses scs, Type *type, LinkageSpec::Spec spec = LinkageSpec::Cforall )
242                : Parent( name, scs, type ) { set_linkage( spec ); this->location = location; }
243
[0dd3a2f]244        TypedefDecl( const TypedefDecl &other ) : Parent( other ) {}
[17cd4eb]245
[e149f77]246        virtual std::string typeString() const override;
[e39241b]247
[e149f77]248        virtual TypedefDecl *clone() const override { return new TypedefDecl( *this ); }
249        virtual void accept( Visitor &v ) override { v.visit( this ); }
250        virtual Declaration *acceptMutator( Mutator &m )  override { return m.mutate( this ); }
[17cd4eb]251  private:
[51b7345]252};
253
[17cd4eb]254class AggregateDecl : public Declaration {
[0dd3a2f]255        typedef Declaration Parent;
[17cd4eb]256  public:
[65cdc1e]257        std::list<Declaration*> members;
258        std::list<TypeDecl*> parameters;
259        bool body;
260        std::list< Attribute * > attributes;
261
[fa4805f]262        AggregateDecl( const std::string &name, const std::list< Attribute * > & attributes = std::list< class Attribute * >(), LinkageSpec::Spec linkage = LinkageSpec::Cforall );
[0dd3a2f]263        AggregateDecl( const AggregateDecl &other );
[68f9c43]264       
[0dd3a2f]265        std::list<Declaration*>& get_members() { return members; }
266        std::list<TypeDecl*>& get_parameters() { return parameters; }
[17cd4eb]267
[c0aa336]268        std::list< Attribute * >& get_attributes() { return attributes; }
269        const std::list< Attribute * >& get_attributes() const { return attributes; }
270
[5d125e4]271        bool has_body() const { return body; }
272        AggregateDecl * set_body( bool body ) { AggregateDecl::body = body; return this; }
273
[50377a4]274        virtual void print( std::ostream &os, Indenter indent = {} ) const override;
275        virtual void printShort( std::ostream &os, Indenter indent = {} ) const override;
[17cd4eb]276  protected:
[0dd3a2f]277        virtual std::string typeString() const = 0;
[51b7345]278};
279
[17cd4eb]280class StructDecl : public AggregateDecl {
[0dd3a2f]281        typedef AggregateDecl Parent;
[17cd4eb]282  public:
[f196351]283        StructDecl( const std::string &name, DeclarationNode::Aggregate kind = DeclarationNode::Struct, const std::list< Attribute * > & attributes = std::list< class Attribute * >(), LinkageSpec::Spec linkage = LinkageSpec::Cforall ) : Parent( name, attributes, linkage ), kind( kind ) {}
284        StructDecl( const StructDecl &other ) : Parent( other ), kind( other.kind ) {}
[17cd4eb]285
[409433da]286        bool is_coroutine() { return kind == DeclarationNode::Coroutine; }
287        bool is_monitor() { return kind == DeclarationNode::Monitor; }
288        bool is_thread() { return kind == DeclarationNode::Thread; }
289
[e149f77]290        virtual StructDecl *clone() const override { return new StructDecl( *this ); }
291        virtual void accept( Visitor &v ) override { v.visit( this ); }
292        virtual Declaration *acceptMutator( Mutator &m )  override { return m.mutate( this ); }
[17cd4eb]293  private:
[409433da]294        DeclarationNode::Aggregate kind;
[e149f77]295        virtual std::string typeString() const override;
[51b7345]296};
297
[17cd4eb]298class UnionDecl : public AggregateDecl {
[0dd3a2f]299        typedef AggregateDecl Parent;
[17cd4eb]300  public:
[fa4805f]301        UnionDecl( const std::string &name, const std::list< Attribute * > & attributes = std::list< class Attribute * >(), LinkageSpec::Spec linkage = LinkageSpec::Cforall ) : Parent( name, attributes, linkage ) {}
[0dd3a2f]302        UnionDecl( const UnionDecl &other ) : Parent( other ) {}
[17cd4eb]303
[e149f77]304        virtual UnionDecl *clone() const override { return new UnionDecl( *this ); }
305        virtual void accept( Visitor &v ) override { v.visit( this ); }
306        virtual Declaration *acceptMutator( Mutator &m )  override { return m.mutate( this ); }
[17cd4eb]307  private:
[e149f77]308        virtual std::string typeString() const override;
[51b7345]309};
310
[17cd4eb]311class EnumDecl : public AggregateDecl {
[0dd3a2f]312        typedef AggregateDecl Parent;
[17cd4eb]313  public:
[fa4805f]314        EnumDecl( const std::string &name, const std::list< Attribute * > & attributes = std::list< class Attribute * >(), LinkageSpec::Spec linkage = LinkageSpec::Cforall ) : Parent( name, attributes, linkage ) {}
[0dd3a2f]315        EnumDecl( const EnumDecl &other ) : Parent( other ) {}
[17cd4eb]316
[fc9153d]317        bool valueOf( Declaration * enumerator, long long int & value );
318
[e149f77]319        virtual EnumDecl *clone() const override { return new EnumDecl( *this ); }
320        virtual void accept( Visitor &v ) override { v.visit( this ); }
321        virtual Declaration *acceptMutator( Mutator &m )  override { return m.mutate( this ); }
[17cd4eb]322  private:
[fc9153d]323        std::map< std::string, long long int > enumValues;
[e149f77]324        virtual std::string typeString() const override;
[51b7345]325};
326
[4040425]327class TraitDecl : public AggregateDecl {
[0dd3a2f]328        typedef AggregateDecl Parent;
[17cd4eb]329  public:
[bd46af4]330        TraitDecl( const std::string &name, const std::list< Attribute * > & attributes, LinkageSpec::Spec linkage ) : Parent( name, attributes, linkage ) {
[c0aa336]331                assertf( attributes.empty(), "attribute unsupported for traits" );
332        }
[4040425]333        TraitDecl( const TraitDecl &other ) : Parent( other ) {}
[17cd4eb]334
[e149f77]335        virtual TraitDecl *clone() const override { return new TraitDecl( *this ); }
336        virtual void accept( Visitor &v ) override { v.visit( this ); }
337        virtual Declaration *acceptMutator( Mutator &m )  override { return m.mutate( this ); }
[17cd4eb]338  private:
[e149f77]339        virtual std::string typeString() const override;
[51b7345]340};
341
[e994912]342class AsmDecl : public Declaration {
343  public:
[65cdc1e]344        AsmStmt *stmt;
345
[e994912]346        AsmDecl( AsmStmt *stmt );
347        AsmDecl( const AsmDecl &other );
348
349        AsmStmt *get_stmt() { return stmt; }
350        void set_stmt( AsmStmt *newValue ) { stmt = newValue; }
351
[e149f77]352        virtual AsmDecl *clone() const override { return new AsmDecl( *this ); }
353        virtual void accept( Visitor &v ) override { v.visit( this ); }
354        virtual AsmDecl *acceptMutator( Mutator &m )  override { return m.mutate( this ); }
[50377a4]355        virtual void print( std::ostream &os, Indenter indent = {} ) const override;
356        virtual void printShort( std::ostream &os, Indenter indent = {} ) const override;
[e994912]357};
358
[2c57025]359std::ostream & operator<<( std::ostream & os, const TypeDecl::Data & data );
[baf7fee]360
[0dd3a2f]361// Local Variables: //
362// tab-width: 4 //
363// mode: c++ //
364// compile-command: "make install" //
365// End: //
Note: See TracBrowser for help on using the repository browser.