source: src/SynTree/Declaration.h @ 561354f

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

Save progress

  • Property mode set to 100644
File size: 21.0 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// Declaration.h --
8//
9// Author           : Richard C. Bilson
10// Created On       : Mon May 18 07:44:20 2015
11// Last Modified By : Henry Xue
12// Last Modified On : Tue Jul 20 04:10:50 2021
13// Update Count     : 160
14//
15
16#pragma once
17
18#include <cassert>               // for assertf
19#include <iosfwd>                // for ostream
20#include <list>                  // for list
21#include <unordered_map>         // for unordered_map
22#include <string>                // for string, operator+, allocator, to_string
23
24#include "BaseSyntaxNode.h"      // for BaseSyntaxNode
25#include "Mutator.h"             // for Mutator
26#include "LinkageSpec.h"         // for Spec, Cforall
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;
38
39class Declaration : public BaseSyntaxNode {
40  public:
41        std::string name;
42        LinkageSpec::Spec linkage;
43        bool extension = false;
44
45        Declaration( const std::string & name, Type::StorageClasses scs, LinkageSpec::Spec linkage );
46        Declaration( const Declaration & other );
47        virtual ~Declaration();
48
49        const std::string & get_name() const { return name; }
50        void set_name( std::string newValue ) { name = newValue; }
51
52        Type::StorageClasses get_storageClasses() const { return storageClasses; }
53
54        LinkageSpec::Spec get_linkage() const { return linkage; }
55        void set_linkage( LinkageSpec::Spec newValue ) { linkage = newValue; }
56
57        UniqueId get_uniqueId() const { return uniqueId; }
58
59        bool get_extension() const { return extension; }
60        Declaration * set_extension( bool exten ) { extension = exten; return this; }
61
62        void fixUniqueId( void );
63        virtual Declaration * clone() const override = 0;
64        virtual void accept( Visitor & v ) override = 0;
65        virtual void accept( Visitor & v ) const override = 0;
66        virtual Declaration * acceptMutator( Mutator & m ) override = 0;
67        virtual void print( std::ostream & os, Indenter indent = {} ) const override = 0;
68        virtual void printShort( std::ostream & os, Indenter indent = {} ) const = 0;
69
70        UniqueId uniqueId;
71        Type::StorageClasses storageClasses;
72  private:
73};
74
75class DeclarationWithType : public Declaration {
76  public:
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
82        Expression * asmName;
83        std::list< Attribute * > attributes;
84        bool isDeleted = false;
85
86        DeclarationWithType( const std::string & name, Type::StorageClasses scs, LinkageSpec::Spec linkage, const std::list< Attribute * > & attributes, Type::FuncSpecifiers fs );
87        DeclarationWithType( const DeclarationWithType & other );
88        virtual ~DeclarationWithType();
89
90        std::string get_mangleName() const { return mangleName; }
91        DeclarationWithType * set_mangleName( std::string newValue ) { mangleName = newValue; return this; }
92
93        std::string get_scopedMangleName() const { return mangleName + "_" + std::to_string(scopeLevel); }
94
95        int get_scopeLevel() const { return scopeLevel; }
96        DeclarationWithType * set_scopeLevel( int newValue ) { scopeLevel = newValue; return this; }
97
98        Expression * get_asmName() const { return asmName; }
99        DeclarationWithType * set_asmName( Expression * newValue ) { asmName = newValue; return this; }
100
101        std::list< Attribute * >& get_attributes() { return attributes; }
102        const std::list< Attribute * >& get_attributes() const { return attributes; }
103
104        Type::FuncSpecifiers get_funcSpec() const { return fs; }
105        //void set_functionSpecifiers( Type::FuncSpecifiers newValue ) { fs = newValue; }
106
107        virtual DeclarationWithType * clone() const override = 0;
108        virtual DeclarationWithType * acceptMutator( Mutator & m )  override = 0;
109
110        virtual Type * get_type() const = 0;
111        virtual void set_type(Type *) = 0;
112
113  private:
114        Type::FuncSpecifiers fs;
115};
116
117class ObjectDecl : public DeclarationWithType {
118        typedef DeclarationWithType Parent;
119  public:
120        Type * type;
121        Initializer * init;
122        Expression * bitfieldWidth;
123        bool enumInLine = false;
124
125        ObjectDecl( const std::string & name, Type::StorageClasses scs, LinkageSpec::Spec linkage, Expression * bitfieldWidth, Type * type, Initializer * init,
126                                const std::list< Attribute * > attributes = std::list< Attribute * >(), Type::FuncSpecifiers fs = Type::FuncSpecifiers() );
127        ObjectDecl( const ObjectDecl & other );
128        virtual ~ObjectDecl();
129
130        virtual Type * get_type() const override { return type; }
131        virtual void set_type(Type * newType) override { type = newType; }
132
133        Initializer * get_init() const { return init; }
134        void set_init( Initializer * newValue ) { init = newValue; }
135
136        Expression * get_bitfieldWidth() const { return bitfieldWidth; }
137        void set_bitfieldWidth( Expression * newValue ) { bitfieldWidth = newValue; }
138
139        static ObjectDecl * newObject( const std::string & name, Type * type, Initializer * init );
140
141        virtual ObjectDecl * clone() const override { return new ObjectDecl( *this ); }
142        virtual void accept( Visitor & v ) override { v.visit( this ); }
143        virtual void accept( Visitor & v ) const override { v.visit( this ); }
144        virtual DeclarationWithType * acceptMutator( Mutator & m )  override { return m.mutate( this ); }
145        virtual void print( std::ostream & os, Indenter indent = {} ) const override;
146        virtual void printShort( std::ostream & os, Indenter indent = {} ) const override;
147
148        void checkAssignedValue() const;
149};
150
151class FunctionDecl : public DeclarationWithType {
152        typedef DeclarationWithType Parent;
153  public:
154        FunctionType * type;
155        CompoundStmt * statements;
156        std::list< Expression * > withExprs;
157
158        FunctionDecl( const std::string & name, Type::StorageClasses scs, LinkageSpec::Spec linkage, FunctionType * type, CompoundStmt * statements,
159                                  const std::list< Attribute * > attributes = std::list< Attribute * >(), Type::FuncSpecifiers fs = Type::FuncSpecifiers() );
160        FunctionDecl( const FunctionDecl & other );
161        virtual ~FunctionDecl();
162
163        virtual Type * get_type() const override { return type; }
164        virtual void set_type(Type * t) override { type = strict_dynamic_cast< FunctionType* >( t ); }
165
166        FunctionType * get_functionType() const { return type; }
167        void set_functionType( FunctionType * newValue ) { type = newValue; }
168        CompoundStmt * get_statements() const { return statements; }
169        void set_statements( CompoundStmt * newValue ) { statements = newValue; }
170        bool has_body() const { return NULL != statements; }
171
172        static FunctionDecl * newFunction( const std::string & name, FunctionType * type, CompoundStmt * statements );
173
174        virtual FunctionDecl * clone() const override { return new FunctionDecl( *this ); }
175        virtual void accept( Visitor & v ) override { v.visit( this ); }
176        virtual void accept( Visitor & v ) const override { v.visit( this ); }
177        virtual DeclarationWithType * acceptMutator( Mutator & m )  override { return m.mutate( this ); }
178        virtual void print( std::ostream & os, Indenter indent = {} ) const override;
179        virtual void printShort( std::ostream & os, Indenter indent = {} ) const override;
180};
181
182class NamedTypeDecl : public Declaration {
183        typedef Declaration Parent;
184  public:
185        Type * base;
186        std::list< DeclarationWithType * > assertions;
187
188        NamedTypeDecl( const std::string & name, Type::StorageClasses scs, Type * type );
189        NamedTypeDecl( const NamedTypeDecl & other );
190        virtual ~NamedTypeDecl();
191
192        Type * get_base() const { return base; }
193        void set_base( Type * newValue ) { base = newValue; }
194        std::list< DeclarationWithType * >& get_assertions() { return assertions; }
195
196        virtual const char * typeString() const = 0;
197
198        virtual NamedTypeDecl * clone() const override = 0;
199        virtual void print( std::ostream & os, Indenter indent = {} ) const override;
200        virtual void printShort( std::ostream & os, Indenter indent = {} ) const override;
201};
202
203class TypeDecl : public NamedTypeDecl {
204        typedef NamedTypeDecl Parent;
205  public:
206        enum Kind { Dtype, DStype, Otype, Ftype, Ttype, Dimension, NUMBER_OF_KINDS };
207
208        Kind kind;
209        bool sized;
210        Type * init;
211
212        /// Data extracted from a type decl
213        struct Data {
214                Kind kind;
215                bool isComplete;
216
217                Data() : kind( NUMBER_OF_KINDS ), isComplete( false ) {}
218                Data( const TypeDecl * typeDecl ) : Data( typeDecl->get_kind(), typeDecl->isComplete() ) {}
219                Data( Kind kind, bool isComplete ) : kind( kind ), isComplete( isComplete ) {}
220                Data( const Data & d1, const Data & d2 )
221                        : kind( d1.kind ), isComplete( d1.isComplete || d2.isComplete ) {}
222
223                bool operator==( const Data & other ) const { return kind == other.kind && isComplete == other.isComplete; }
224                bool operator!=( const Data & other ) const { return !(*this == other);}
225        };
226
227        TypeDecl( const std::string & name, Type::StorageClasses scs, Type * type, Kind kind, bool sized, Type * init = nullptr );
228        TypeDecl( const TypeDecl & other );
229        virtual ~TypeDecl();
230
231        Kind get_kind() const { return kind; }
232
233        Type * get_init() const { return init; }
234        TypeDecl * set_init( Type * newValue ) { init = newValue; return this; }
235
236        bool isComplete() const { return sized; }
237        bool get_sized() const { return sized; }
238        TypeDecl * set_sized( bool newValue ) { sized = newValue; return this; }
239
240        virtual const char * typeString() const override;
241        virtual const char * genTypeString() const;
242
243        virtual TypeDecl * clone() const override { return new TypeDecl( *this ); }
244        virtual void accept( Visitor & v ) override { v.visit( this ); }
245        virtual void accept( Visitor & v ) const override { v.visit( this ); }
246        virtual Declaration * acceptMutator( Mutator & m )  override { return m.mutate( this ); }
247        virtual void print( std::ostream & os, Indenter indent = {} ) const override;
248};
249
250class TypedefDecl : public NamedTypeDecl {
251        typedef NamedTypeDecl Parent;
252  public:
253        TypedefDecl( const std::string & name, CodeLocation location, Type::StorageClasses scs, Type * type, LinkageSpec::Spec spec = LinkageSpec::Cforall )
254                : Parent( name, scs, type ) { set_linkage( spec ); this->location = location; }
255
256        TypedefDecl( const TypedefDecl & other ) : Parent( other ) {}
257
258        virtual const char * typeString() const override;
259
260        virtual TypedefDecl * clone() const override { return new TypedefDecl( *this ); }
261        virtual void accept( Visitor & v ) override { v.visit( this ); }
262        virtual void accept( Visitor & v ) const override { v.visit( this ); }
263        virtual Declaration * acceptMutator( Mutator & m )  override { return m.mutate( this ); }
264  private:
265};
266
267class AggregateDecl : public Declaration {
268        typedef Declaration Parent;
269  public:
270        enum Aggregate { Struct, Union, Enum, Exception, Trait, Generator, Coroutine, Monitor, Thread, NoAggregate, ADT };
271        static const char * aggrString( Aggregate aggr );
272
273        std::list<Declaration*> members;
274        std::list<TypeDecl*> parameters;
275        bool body;
276        std::list< Attribute * > attributes;
277        AggregateDecl * parent = nullptr;
278
279        AggregateDecl( const std::string & name, const std::list< Attribute * > & attributes = std::list< class Attribute * >(), LinkageSpec::Spec linkage = LinkageSpec::Cforall );
280        AggregateDecl( const AggregateDecl & other );
281        virtual ~AggregateDecl();
282
283        std::list<Declaration*>& get_members() { return members; }
284        std::list<TypeDecl*>& get_parameters() { return parameters; }
285
286        std::list< Attribute * >& get_attributes() { return attributes; }
287        const std::list< Attribute * >& get_attributes() const { return attributes; }
288
289        bool has_body() const { return body; }
290        AggregateDecl * set_body( bool body ) { AggregateDecl::body = body; return this; }
291
292        virtual void print( std::ostream & os, Indenter indent = {} ) const override;
293        virtual void printShort( std::ostream & os, Indenter indent = {} ) const override;
294  protected:
295        virtual const char * typeString() const = 0;
296};
297
298class StructDecl : public AggregateDecl {
299        typedef AggregateDecl Parent;
300  public:
301        StructDecl( const std::string & name, Aggregate kind = Struct, const std::list< Attribute * > & attributes = std::list< class Attribute * >(), LinkageSpec::Spec linkage = LinkageSpec::Cforall ) : Parent( name, attributes, linkage ), kind( kind ) {}
302        StructDecl( const StructDecl & other ) : Parent( other ), kind( other.kind ) {}
303
304        bool is_coroutine() { return kind == Coroutine; }
305        bool is_exception() { return kind == Exception; }
306        bool is_generator() { return kind == Generator; }
307        bool is_monitor  () { return kind == Monitor  ; }
308        bool is_thread   () { return kind == Thread   ; }
309
310        // Make a type instance of this declaration.
311        StructInstType * makeInst( std::list< Expression * > const & parameters );
312        StructInstType * makeInst( std::list< Expression * > && parameters );
313
314        virtual StructDecl * clone() const override { return new StructDecl( *this ); }
315        virtual void accept( Visitor & v ) override { v.visit( this ); }
316        virtual void accept( Visitor & v ) const override { v.visit( this ); }
317        virtual Declaration * acceptMutator( Mutator & m )  override { return m.mutate( this ); }
318        Aggregate kind;
319  private:
320        virtual const char * typeString() const override;
321};
322
323class UnionDecl : public AggregateDecl {
324        typedef AggregateDecl Parent;
325  public:
326        UnionDecl( const std::string & name, const std::list< Attribute * > & attributes = std::list< class Attribute * >(), LinkageSpec::Spec linkage = LinkageSpec::Cforall ) : Parent( name, attributes, linkage ) {}
327        UnionDecl( const UnionDecl & other ) : Parent( other ) {}
328
329        virtual UnionDecl * clone() const override { return new UnionDecl( *this ); }
330        virtual void accept( Visitor & v ) override { v.visit( this ); }
331        virtual void accept( Visitor & v ) const override { v.visit( this ); }
332        virtual Declaration * acceptMutator( Mutator & m )  override { return m.mutate( this ); }
333  private:
334        virtual const char * typeString() const override;
335};
336
337class EnumDecl : public AggregateDecl {
338        typedef AggregateDecl Parent;
339  public:
340        bool isTyped;
341        Type * base;
342        enum EnumHiding { Visible, Hide } hide;
343
344        EnumDecl( const std::string & name,
345         const std::list< Attribute * > & attributes = std::list< class Attribute * >(),
346          bool isTyped = false, LinkageSpec::Spec linkage = LinkageSpec::Cforall,
347          Type * baseType = nullptr ) 
348          : Parent( name, attributes, linkage ), isTyped(isTyped), base( baseType ) {}
349        EnumDecl( const EnumDecl & other ) 
350          : Parent( other ), isTyped( other.isTyped), base( other.base ) {}
351        bool valueOf( Declaration * enumerator, long long int & value );
352        virtual EnumDecl * clone() const override { return new EnumDecl( *this ); }
353        virtual void accept( Visitor & v ) override { v.visit( this ); }
354        virtual void accept( Visitor & v ) const override { v.visit( this ); }
355        virtual Declaration * acceptMutator( Mutator & m )  override { return m.mutate( this ); }
356
357        std::unordered_map< std::string, long long int > enumValues; // This attribute is unused
358        virtual void print( std::ostream & os, Indenter indent = {} ) const override final;
359  private:
360        // std::unordered_map< std::string, long long int > enumValues;
361        virtual const char * typeString() const override;
362};
363
364class AdtDecl : public AggregateDecl {
365        typedef AggregateDecl Parent;
366  public:
367        std::list<StructDecl*> data_constructors;
368        UnionDecl * data_union;
369        EnumDecl * tag;
370        StructDecl * tag_union;
371
372        AdtDecl( const std::string & name,
373         const std::list< Attribute * > & attributes = std::list< class Attribute * >(),
374         LinkageSpec::Spec linkage = LinkageSpec::Cforall, 
375         const std::list< StructDecl* > data_constructors = std::list< StructDecl * >(),
376         UnionDecl * data_union = nullptr, EnumDecl * tag = nullptr, StructDecl * tag_union = nullptr )
377         : Parent( name, attributes, linkage ), data_constructors(data_constructors), 
378         data_union( data_union ), tag( tag ), tag_union( tag_union ) {}
379
380        AdtDecl( const AdtDecl & other )
381         : Parent( other ) {}
382
383        virtual AdtDecl * clone() const override { return new AdtDecl( *this ); }
384        virtual void accept( Visitor & v ) override { v.visit( this ); }
385        virtual void accept( Visitor & v ) const override { v.visit( this ); }
386
387        virtual Declaration * acceptMutator( Mutator & m )  override { return m.mutate( this ); }
388        virtual void print( std::ostream & os, Indenter indent = {} ) const override final {
389                os << "AdtDecl ... " << indent;
390        }
391       
392private:
393        virtual const char * typeString() const override {
394                return "AdtDecl";
395        }
396};
397
398class TraitDecl : public AggregateDecl {
399        typedef AggregateDecl Parent;
400  public:
401        TraitDecl( const std::string & name, const std::list< Attribute * > & attributes, LinkageSpec::Spec linkage ) : Parent( name, attributes, linkage ) {
402                assertf( attributes.empty(), "attribute unsupported for traits" );
403        }
404        TraitDecl( const TraitDecl & other ) : Parent( other ) {}
405
406        virtual TraitDecl * clone() const override { return new TraitDecl( *this ); }
407        virtual void accept( Visitor & v ) override { v.visit( this ); }
408        virtual void accept( Visitor & v ) const override { v.visit( this ); }
409        virtual Declaration * acceptMutator( Mutator & m )  override { return m.mutate( this ); }
410  private:
411        virtual const char * typeString() const override;
412};
413
414class WithStmt : public Declaration {
415public:
416        std::list< Expression * > exprs;
417        Statement * stmt;
418
419        WithStmt( const std::list< Expression * > & exprs, Statement * stmt );
420        WithStmt( const WithStmt & other );
421        virtual ~WithStmt();
422
423        virtual WithStmt * clone() const override { return new WithStmt( *this ); }
424        virtual void accept( Visitor & v ) override { v.visit( this ); }
425        virtual void accept( Visitor & v ) const override { v.visit( this ); }
426        virtual Declaration * acceptMutator( Mutator & m )  override { return m.mutate( this ); }
427        virtual void print( std::ostream & os, Indenter indent = {} ) const override;
428        virtual void printShort( std::ostream & os, Indenter indent = {} ) const override { print(os, indent); }
429};
430
431class AsmDecl : public Declaration {
432  public:
433        AsmStmt * stmt;
434
435        AsmDecl( AsmStmt * stmt );
436        AsmDecl( const AsmDecl & other );
437        virtual ~AsmDecl();
438
439        AsmStmt * get_stmt() { return stmt; }
440        void set_stmt( AsmStmt * newValue ) { stmt = newValue; }
441
442        virtual AsmDecl * clone() const override { return new AsmDecl( *this ); }
443        virtual void accept( Visitor & v ) override { v.visit( this ); }
444        virtual void accept( Visitor & v ) const override { v.visit( this ); }
445        virtual AsmDecl * acceptMutator( Mutator & m )  override { return m.mutate( this ); }
446        virtual void print( std::ostream & os, Indenter indent = {} ) const override;
447        virtual void printShort( std::ostream & os, Indenter indent = {} ) const override;
448};
449
450class DirectiveDecl : public Declaration {
451  public:
452        DirectiveStmt * stmt;
453
454        DirectiveDecl( DirectiveStmt * stmt );
455        DirectiveDecl( const DirectiveDecl & other );
456        virtual ~DirectiveDecl();
457
458        DirectiveStmt * get_stmt() { return stmt; }
459        void set_stmt( DirectiveStmt * newValue ) { stmt = newValue; }
460
461        virtual DirectiveDecl * clone() const override { return new DirectiveDecl( *this ); }
462        virtual void accept( Visitor & v ) override { v.visit( this ); }
463        virtual void accept( Visitor & v ) const override { v.visit( this ); }
464        virtual DirectiveDecl * acceptMutator( Mutator & m )  override { return m.mutate( this ); }
465        virtual void print( std::ostream & os, Indenter indent = {} ) const override;
466        virtual void printShort( std::ostream & os, Indenter indent = {} ) const override;
467};
468
469class StaticAssertDecl : public Declaration {
470public:
471        Expression * condition;
472        ConstantExpr * message;   // string literal
473
474        StaticAssertDecl( Expression * condition, ConstantExpr * message );
475        StaticAssertDecl( const StaticAssertDecl & other );
476        virtual ~StaticAssertDecl();
477
478        virtual StaticAssertDecl * clone() const override { return new StaticAssertDecl( *this ); }
479        virtual void accept( Visitor & v ) override { v.visit( this ); }
480        virtual void accept( Visitor & v ) const override { v.visit( this ); }
481        virtual StaticAssertDecl * acceptMutator( Mutator & m )  override { return m.mutate( this ); }
482        virtual void print( std::ostream & os, Indenter indent = {} ) const override;
483        virtual void printShort( std::ostream & os, Indenter indent = {} ) const override;
484};
485
486
487class InlineMemberDecl : public DeclarationWithType {
488        typedef DeclarationWithType Parent;
489  public:
490        Type * type;
491
492        InlineMemberDecl( const std::string & name, Type::StorageClasses scs, LinkageSpec::Spec linkage, Type * type,
493                                const std::list< Attribute * > attributes = std::list< Attribute * >(), Type::FuncSpecifiers fs = Type::FuncSpecifiers() );
494        InlineMemberDecl( const InlineMemberDecl & other );
495        virtual ~InlineMemberDecl();
496
497        virtual Type * get_type() const override { return type; }
498        virtual void set_type(Type * newType) override { type = newType; }
499
500        static InlineMemberDecl * newInlineMemberDecl( const std::string & name, Type * type );
501
502        virtual InlineMemberDecl * clone() const override { return new InlineMemberDecl( *this ); }
503        virtual void accept( Visitor & v ) override { v.visit( this ); }
504        virtual void accept( Visitor & v ) const override { v.visit( this ); }
505        virtual DeclarationWithType * acceptMutator( Mutator & m )  override { return m.mutate( this ); }
506        virtual void print( std::ostream & os, Indenter indent = {} ) const override;
507        virtual void printShort( std::ostream & os, Indenter indent = {} ) const override;
508
509};
510
511std::ostream & operator<<( std::ostream & os, const TypeDecl::Data & data );
512
513// Local Variables: //
514// tab-width: 4 //
515// mode: c++ //
516// compile-command: "make install" //
517// End: //
Note: See TracBrowser for help on using the repository browser.