source: src/SynTree/Declaration.h@ 8d25360

no_list
Last change on this file since 8d25360 was 70a1c3ae, checked in by Thierry Delisle <tdelisle@…>, 7 years ago

Starting to remove std::list to see if it affects performance, started with List of attributes

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