source: src/SynTree/Declaration.h @ e612146c

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since e612146c was e612146c, checked in by Peter A. Buhr <pabuhr@…>, 7 years ago

third attempt at user-defined literals

  • Property mode set to 100644
File size: 13.8 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        virtual ~Declaration();
48
[5f2f2d7]49        const std::string &get_name() const { return name; }
[0dd3a2f]50        void set_name( std::string newValue ) { name = newValue; }
[a7c90d4]51
[68fe077a]52        Type::StorageClasses get_storageClasses() const { return storageClasses; }
[a7c90d4]53
[8b7ee09]54        LinkageSpec::Spec get_linkage() const { return linkage; }
55        void set_linkage( LinkageSpec::Spec newValue ) { linkage = newValue; }
[a7c90d4]56
[0dd3a2f]57        UniqueId get_uniqueId() const { return uniqueId; }
[a7c90d4]58
[8e9cbb2]59        bool get_extension() const { return extension; }
60        Declaration *set_extension( bool exten ) { extension = exten; return this; }
[0dd3a2f]61
62        void fixUniqueId( void );
63        virtual Declaration *clone() const = 0;
64        virtual void accept( Visitor &v ) = 0;
65        virtual Declaration *acceptMutator( Mutator &m ) = 0;
66        virtual void print( std::ostream &os, int indent = 0 ) const = 0;
67        virtual void printShort( std::ostream &os, int indent = 0 ) const = 0;
68
69        static void dumpIds( std::ostream &os );
70        static Declaration *declFromId( UniqueId id );
[65cdc1e]71
[17cd4eb]72  private:
[68fe077a]73        Type::StorageClasses storageClasses;
[0dd3a2f]74        UniqueId uniqueId;
[51b7345]75};
76
[17cd4eb]77class DeclarationWithType : public Declaration {
78  public:
[65cdc1e]79        // this represents the type with all types and typedefs expanded it is generated by SymTab::Validate::Pass2
80        std::string mangleName;
81        // need to remember the scope level at which the variable was declared, so that shadowed identifiers can be accessed
82        int scopeLevel = 0;
83
[e612146c]84        Expression *asmName;
[65cdc1e]85        std::list< Attribute * > attributes;
86
[ddfd945]87        DeclarationWithType( const std::string &name, Type::StorageClasses scs, LinkageSpec::Spec linkage, const std::list< Attribute * > & attributes, Type::FuncSpecifiers fs );
[0dd3a2f]88        DeclarationWithType( const DeclarationWithType &other );
89        virtual ~DeclarationWithType();
[51b7345]90
[0dd3a2f]91        std::string get_mangleName() const { return mangleName; }
[58dd019]92        DeclarationWithType * set_mangleName( std::string newValue ) { mangleName = newValue; return this; }
[17cd4eb]93
[f326f99]94        std::string get_scopedMangleName() const { return mangleName + "_" + std::to_string(scopeLevel); }
95
96        int get_scopeLevel() const { return scopeLevel; }
[58dd019]97        DeclarationWithType * set_scopeLevel( int newValue ) { scopeLevel = newValue; return this; }
98
[e612146c]99        Expression *get_asmName() const { return asmName; }
100        DeclarationWithType * set_asmName( Expression *newValue ) { asmName = newValue; return this; }
[f326f99]101
[f9cebb5]102        std::list< Attribute * >& get_attributes() { return attributes; }
103        const std::list< Attribute * >& get_attributes() const { return attributes; }
104
[ddfd945]105        Type::FuncSpecifiers get_funcSpec() const { return fs; }
106        //void set_functionSpecifiers( Type::FuncSpecifiers newValue ) { fs = newValue; }
[dd020c0]107
[0dd3a2f]108        virtual DeclarationWithType *clone() const = 0;
109        virtual DeclarationWithType *acceptMutator( Mutator &m ) = 0;
[17cd4eb]110
[9aaac6e9]111        virtual Type * get_type() const = 0;
[0dd3a2f]112        virtual void set_type(Type *) = 0;
[f9cebb5]113
[65cdc1e]114  private:
[ddfd945]115        Type::FuncSpecifiers fs;
[51b7345]116};
117
[17cd4eb]118class ObjectDecl : public DeclarationWithType {
[0dd3a2f]119        typedef DeclarationWithType Parent;
[17cd4eb]120  public:
[65cdc1e]121        Type *type;
122        Initializer *init;
123        Expression *bitfieldWidth;
124
[68fe077a]125        ObjectDecl( const std::string &name, Type::StorageClasses scs, LinkageSpec::Spec linkage, Expression *bitfieldWidth, Type *type, Initializer *init,
[ddfd945]126                                const std::list< Attribute * > attributes = std::list< Attribute * >(), Type::FuncSpecifiers fs = Type::FuncSpecifiers() );
[0dd3a2f]127        ObjectDecl( const ObjectDecl &other );
128        virtual ~ObjectDecl();
129
[9aaac6e9]130        virtual Type * get_type() const { return type; }
[0dd3a2f]131        virtual void set_type(Type *newType) { type = newType; }
132
133        Initializer *get_init() const { return init; }
134        void set_init( Initializer *newValue ) { init = newValue; }
[58dd019]135
[0dd3a2f]136        Expression *get_bitfieldWidth() const { return bitfieldWidth; }
137        void set_bitfieldWidth( Expression *newValue ) { bitfieldWidth = newValue; }
138
139        virtual ObjectDecl *clone() const { return new ObjectDecl( *this ); }
140        virtual void accept( Visitor &v ) { v.visit( this ); }
[1db21619]141        virtual DeclarationWithType *acceptMutator( Mutator &m ) { return m.mutate( this ); }
[0dd3a2f]142        virtual void print( std::ostream &os, int indent = 0 ) const;
143        virtual void printShort( std::ostream &os, int indent = 0 ) const;
[51b7345]144};
145
[17cd4eb]146class FunctionDecl : public DeclarationWithType {
[0dd3a2f]147        typedef DeclarationWithType Parent;
[17cd4eb]148  public:
[65cdc1e]149        FunctionType *type;
150        CompoundStmt *statements;
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 );
155        virtual ~FunctionDecl();
[51b7345]156
[9aaac6e9]157        Type * get_type() const;
[0dd3a2f]158        virtual void set_type(Type *);
[51b7345]159
[9aaac6e9]160        FunctionType * get_functionType() const { return type; }
[0dd3a2f]161        void set_functionType( FunctionType *newValue ) { type = newValue; }
162        CompoundStmt *get_statements() const { return statements; }
163        void set_statements( CompoundStmt *newValue ) { statements = newValue; }
164
165        virtual FunctionDecl *clone() const { return new FunctionDecl( *this ); }
166        virtual void accept( Visitor &v ) { v.visit( this ); }
167        virtual DeclarationWithType *acceptMutator( Mutator &m ) { return m.mutate( this ); }
168        virtual void print( std::ostream &os, int indent = 0 ) const;
169        virtual void printShort( std::ostream &os, int indent = 0 ) const;
[51b7345]170};
171
[17cd4eb]172class NamedTypeDecl : public Declaration {
[0dd3a2f]173        typedef Declaration Parent;
[17cd4eb]174  public:
[65cdc1e]175        Type *base;
176        std::list< TypeDecl* > parameters;
177        std::list< DeclarationWithType* > assertions;
178
[68fe077a]179        NamedTypeDecl( const std::string &name, Type::StorageClasses scs, Type *type );
[46f6134]180        NamedTypeDecl( const NamedTypeDecl &other );
[0dd3a2f]181        virtual ~NamedTypeDecl();
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
[0dd3a2f]190        virtual NamedTypeDecl *clone() const = 0;
191        virtual void print( std::ostream &os, int indent = 0 ) const;
192        virtual void printShort( std::ostream &os, int indent = 0 ) const;
[51b7345]193};
194
[17cd4eb]195class TypeDecl : public NamedTypeDecl {
[0dd3a2f]196        typedef NamedTypeDecl Parent;
[17cd4eb]197  public:
[8f60f0b]198        enum Kind { Any, 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
[67cf18c]214        TypeDecl( const std::string &name, Type::StorageClasses scs, Type *type, Kind kind, Type * init = nullptr );
[0dd3a2f]215        TypeDecl( const TypeDecl &other );
[67cf18c]216        virtual ~TypeDecl();
[17cd4eb]217
[0dd3a2f]218        Kind get_kind() const { return kind; }
[51b7345]219
[67cf18c]220        Type * get_init() const { return init; }
221        TypeDecl * set_init( Type * newValue ) { init = newValue; return this; }
222
[2c57025]223        bool isComplete() const { return kind == Any || sized; }
224        bool get_sized() const { return sized; }
225        TypeDecl * set_sized( bool newValue ) { sized = newValue; return this; }
226
[e39241b]227        virtual std::string typeString() const;
[bdd0755]228        virtual std::string genTypeString() const;
[e39241b]229
[0dd3a2f]230        virtual TypeDecl *clone() const { return new TypeDecl( *this ); }
231        virtual void accept( Visitor &v ) { v.visit( this ); }
232        virtual TypeDecl *acceptMutator( Mutator &m ) { return m.mutate( this ); }
[67cf18c]233        virtual void print( std::ostream &os, int indent = 0 ) const;
234
[17cd4eb]235  private:
[0dd3a2f]236        Kind kind;
[51b7345]237};
238
[17cd4eb]239class TypedefDecl : public NamedTypeDecl {
[0dd3a2f]240        typedef NamedTypeDecl Parent;
[17cd4eb]241  public:
[cbce272]242        TypedefDecl( const std::string &name, Type::StorageClasses scs, Type *type, LinkageSpec::Spec spec = LinkageSpec::Cforall ) : Parent( name, scs, type ) { set_linkage( spec ); }
[0dd3a2f]243        TypedefDecl( const TypedefDecl &other ) : Parent( other ) {}
[17cd4eb]244
[e39241b]245        virtual std::string typeString() const;
246
[0dd3a2f]247        virtual TypedefDecl *clone() const { return new TypedefDecl( *this ); }
248        virtual void accept( Visitor &v ) { v.visit( this ); }
249        virtual Declaration *acceptMutator( Mutator &m ) { return m.mutate( this ); }
[17cd4eb]250  private:
[51b7345]251};
252
[17cd4eb]253class AggregateDecl : public Declaration {
[0dd3a2f]254        typedef Declaration Parent;
[17cd4eb]255  public:
[65cdc1e]256        std::list<Declaration*> members;
257        std::list<TypeDecl*> parameters;
258        bool body;
259        std::list< Attribute * > attributes;
260
[fa4805f]261        AggregateDecl( const std::string &name, const std::list< Attribute * > & attributes = std::list< class Attribute * >(), LinkageSpec::Spec linkage = LinkageSpec::Cforall );
[0dd3a2f]262        AggregateDecl( const AggregateDecl &other );
263        virtual ~AggregateDecl();
[51b7345]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
[0dd3a2f]274        virtual void print( std::ostream &os, int indent = 0 ) const;
275        virtual void printShort( std::ostream &os, int indent = 0 ) const;
[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
[0dd3a2f]290        virtual StructDecl *clone() const { return new StructDecl( *this ); }
291        virtual void accept( Visitor &v ) { v.visit( this ); }
292        virtual Declaration *acceptMutator( Mutator &m ) { return m.mutate( this ); }
[17cd4eb]293  private:
[409433da]294        DeclarationNode::Aggregate kind;
[0dd3a2f]295        virtual std::string typeString() const;
[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
[0dd3a2f]304        virtual UnionDecl *clone() const { return new UnionDecl( *this ); }
305        virtual void accept( Visitor &v ) { v.visit( this ); }
306        virtual Declaration *acceptMutator( Mutator &m ) { return m.mutate( this ); }
[17cd4eb]307  private:
[0dd3a2f]308        virtual std::string typeString() const;
[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
[0dd3a2f]317        virtual EnumDecl *clone() const { return new EnumDecl( *this ); }
318        virtual void accept( Visitor &v ) { v.visit( this ); }
319        virtual Declaration *acceptMutator( Mutator &m ) { return m.mutate( this ); }
[17cd4eb]320  private:
[0dd3a2f]321        virtual std::string typeString() const;
[51b7345]322};
323
[4040425]324class TraitDecl : public AggregateDecl {
[0dd3a2f]325        typedef AggregateDecl Parent;
[17cd4eb]326  public:
[bd46af4]327        TraitDecl( const std::string &name, const std::list< Attribute * > & attributes, LinkageSpec::Spec linkage ) : Parent( name, attributes, linkage ) {
[c0aa336]328                assertf( attributes.empty(), "attribute unsupported for traits" );
329        }
[4040425]330        TraitDecl( const TraitDecl &other ) : Parent( other ) {}
[17cd4eb]331
[4040425]332        virtual TraitDecl *clone() const { return new TraitDecl( *this ); }
[0dd3a2f]333        virtual void accept( Visitor &v ) { v.visit( this ); }
334        virtual Declaration *acceptMutator( Mutator &m ) { return m.mutate( this ); }
[17cd4eb]335  private:
[0dd3a2f]336        virtual std::string typeString() const;
[51b7345]337};
338
[e994912]339class AsmDecl : public Declaration {
340  public:
[65cdc1e]341        AsmStmt *stmt;
342
[e994912]343        AsmDecl( AsmStmt *stmt );
344        AsmDecl( const AsmDecl &other );
345        virtual ~AsmDecl();
346
347        AsmStmt *get_stmt() { return stmt; }
348        void set_stmt( AsmStmt *newValue ) { stmt = newValue; }
349
350        virtual AsmDecl *clone() const { return new AsmDecl( *this ); }
351        virtual void accept( Visitor &v ) { v.visit( this ); }
352        virtual AsmDecl *acceptMutator( Mutator &m ) { return m.mutate( this ); }
353        virtual void print( std::ostream &os, int indent = 0 ) const;
354        virtual void printShort( std::ostream &os, int indent = 0 ) const;
355};
356
[3906301]357std::ostream & operator<<( std::ostream & out, const Declaration * decl );
[2c57025]358std::ostream & operator<<( std::ostream & os, const TypeDecl::Data & data );
[baf7fee]359
[0dd3a2f]360// Local Variables: //
361// tab-width: 4 //
362// mode: c++ //
363// compile-command: "make install" //
364// End: //
Note: See TracBrowser for help on using the repository browser.