source: src/SynTree/Declaration.h @ f5edcb4

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since f5edcb4 was aaeacf4, checked in by Thierry Delisle <tdelisle@…>, 5 years ago

Removed global look-up table from UniqueId? to Decl

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