Changes in src/AST/Decl.hpp [427854b:99da267]
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/AST/Decl.hpp
r427854b r99da267 9 9 // Author : Aaron B. Moss 10 10 // Created On : Thu May 9 10:00:00 2019 11 // Last Modified By : Peter A. Buhr12 // Last Modified On : Fri Dec 13 17:38:33201913 // Update Count : 2911 // Last Modified By : Aaron B. Moss 12 // Last Modified On : Thu May 9 10:00:00 2019 13 // Update Count : 1 14 14 // 15 15 … … 20 20 #include <unordered_map> 21 21 #include <vector> 22 #include <algorithm>23 22 24 23 #include "FunctionSpec.hpp" … … 28 27 #include "ParseNode.hpp" 29 28 #include "StorageClasses.hpp" 29 #include "TypeVar.hpp" 30 30 #include "Visitor.hpp" 31 #include "Common/utility.h" 32 #include "Common/SemanticError.h" // error_str 31 #include "Parser/ParseNode.h" // for DeclarationNode::Aggregate 33 32 34 33 // Must be included in *all* AST classes; should be #undef'd at the end of the file 35 #define MUTATE_FRIEND template<typename node_t> friend node_t * mutate(const node_t * node); 34 #define MUTATE_FRIEND \ 35 template<typename node_t> friend node_t * mutate(const node_t * node); \ 36 template<typename node_t> friend node_t * shallowCopy(const node_t * node); 36 37 37 38 namespace ast { … … 88 89 virtual const Type * get_type() const = 0; 89 90 /// Set type of this declaration. May be verified by subclass 90 virtual void set_type( Type *) = 0;91 virtual void set_type( const Type * ) = 0; 91 92 92 93 const DeclWithType * accept( Visitor & v ) const override = 0; … … 103 104 ptr<Expr> bitfieldWidth; 104 105 105 ObjectDecl( const CodeLocation & loc, const std::string & name, const Type * type, 106 const Init * init = nullptr, Storage::Classes storage = {}, 107 Linkage::Spec linkage = Linkage::C, const Expr * bitWd = nullptr, 106 ObjectDecl( const CodeLocation & loc, const std::string & name, const Type * type, 107 const Init * init = nullptr, Storage::Classes storage = {}, 108 Linkage::Spec linkage = Linkage::C, const Expr * bitWd = nullptr, 108 109 std::vector< ptr<Attribute> > && attrs = {}, Function::Specs fs = {} ) 109 110 : DeclWithType( loc, name, storage, linkage, std::move(attrs), fs ), type( type ), … … 111 112 112 113 const Type* get_type() const override { return type; } 113 void set_type( Type * ty ) override { type = ty; }114 void set_type( const Type * ty ) override { type = ty; } 114 115 115 116 const DeclWithType * accept( Visitor& v ) const override { return v.visit( this ); } … … 126 127 std::vector< ptr<Expr> > withExprs; 127 128 128 FunctionDecl( const CodeLocation & loc, const std::string & 129 FunctionDecl( const CodeLocation & loc, const std::string &name, FunctionType * type, 129 130 CompoundStmt * stmts, Storage::Classes storage = {}, Linkage::Spec linkage = Linkage::C, 130 131 std::vector<ptr<Attribute>>&& attrs = {}, Function::Specs fs = {}) … … 133 134 134 135 const Type * get_type() const override; 135 void set_type( Type * t) override;136 void set_type( const Type * t ) override; 136 137 137 138 bool has_body() const { return stmts; } 138 139 139 const DeclWithType * accept( Visitor & 140 const DeclWithType * accept( Visitor &v ) const override { return v.visit( this ); } 140 141 private: 141 142 FunctionDecl * clone() const override { return new FunctionDecl( *this ); } … … 150 151 std::vector<ptr<DeclWithType>> assertions; 151 152 152 NamedTypeDecl( const CodeLocation& loc, const std::string& name, Storage::Classes storage, 153 Type* b, Linkage::Spec spec = Linkage::Cforall ) 153 NamedTypeDecl( 154 const CodeLocation & loc, const std::string & name, Storage::Classes storage, 155 const Type * b, Linkage::Spec spec = Linkage::Cforall ) 154 156 : Decl( loc, name, storage, spec ), base( b ), params(), assertions() {} 155 157 156 158 /// Produces a name for the kind of alias 157 virtual const char *typeString() const = 0;159 virtual std::string typeString() const = 0; 158 160 159 161 private: … … 164 166 /// Cforall type variable: `dtype T` 165 167 class TypeDecl final : public NamedTypeDecl { 166 public: 167 enum Kind { Dtype, Otype, Ftype, Ttype, NUMBER_OF_KINDS }; 168 169 Kind kind; 168 public: 169 TypeVar::Kind kind; 170 170 bool sized; 171 171 ptr<Type> init; … … 173 173 /// Data extracted from a type decl 174 174 struct Data { 175 Kind kind;175 TypeVar::Kind kind; 176 176 bool isComplete; 177 177 178 Data() : kind( NUMBER_OF_KINDS), isComplete( false ) {}178 Data() : kind( (TypeVar::Kind)-1 ), isComplete( false ) {} 179 179 Data( const TypeDecl * d ) : kind( d->kind ), isComplete( d->sized ) {} 180 Data( Kind k, bool c ) : kind( k ), isComplete( c ) {}180 Data( TypeVar::Kind k, bool c ) : kind( k ), isComplete( c ) {} 181 181 Data( const Data & d1, const Data & d2 ) 182 : kind( d1.kind ), isComplete( d1.isComplete || d2.isComplete ) {} 183 184 bool operator==( const Data & o ) const { return kind == o.kind && isComplete == o.isComplete; } 185 bool operator!=( const Data & o ) const { return !(*this == o); } 182 : kind( d1.kind ), isComplete( d1.isComplete || d2.isComplete ) {} 183 184 bool operator== ( const Data & o ) const { 185 return kind == o.kind && isComplete == o.isComplete; 186 } 187 bool operator!= ( const Data & o ) const { return !(*this == o); } 186 188 }; 187 189 188 TypeDecl( const CodeLocation & loc, const std::string & name, Storage::Classes storage, Type * b, 189 Kind k, bool s, Type * i = nullptr ) 190 : NamedTypeDecl( loc, name, storage, b ), kind( k ), sized( k == Ttype || s ), 191 init( i ) {} 192 193 const char * typeString() const override; 190 TypeDecl( 191 const CodeLocation & loc, const std::string & name, Storage::Classes storage, 192 const Type * b, TypeVar::Kind k, bool s, const Type * i = nullptr ) 193 : NamedTypeDecl( loc, name, storage, b ), kind( k ), sized( k == TypeVar::Ttype || s ), 194 init( i ) {} 195 196 std::string typeString() const override; 194 197 /// Produces a name for generated code 195 const char *genTypeString() const;198 std::string genTypeString() const; 196 199 197 200 /// convenience accessor to match Type::isComplete() … … 199 202 200 203 const Decl * accept( Visitor & v ) const override { return v.visit( this ); } 201 204 private: 202 205 TypeDecl * clone() const override { return new TypeDecl{ *this }; } 203 206 MUTATE_FRIEND … … 213 216 : NamedTypeDecl( loc, name, storage, b, spec ) {} 214 217 215 const char *typeString() const override { return "typedef"; }218 std::string typeString() const override { return "typedef"; } 216 219 217 220 const Decl * accept( Visitor & v ) const override { return v.visit( this ); } … … 224 227 class AggregateDecl : public Decl { 225 228 public: 226 enum Aggregate { Struct, Union, Enum, Exception, Trait, Generator, Coroutine, Monitor, Thread, NoAggregate };227 static const char * aggrString( Aggregate aggr );228 229 229 std::vector<ptr<Decl>> members; 230 230 std::vector<ptr<TypeDecl>> params; … … 241 241 242 242 /// Produces a name for the kind of aggregate 243 virtual const char *typeString() const = 0;243 virtual std::string typeString() const = 0; 244 244 245 245 private: … … 251 251 class StructDecl final : public AggregateDecl { 252 252 public: 253 Aggregate kind;253 DeclarationNode::Aggregate kind; 254 254 255 255 StructDecl( const CodeLocation& loc, const std::string& name, 256 Aggregate kind =Struct,256 DeclarationNode::Aggregate kind = DeclarationNode::Struct, 257 257 std::vector<ptr<Attribute>>&& attrs = {}, Linkage::Spec linkage = Linkage::Cforall ) 258 258 : AggregateDecl( loc, name, std::move(attrs), linkage ), kind( kind ) {} 259 259 260 bool is_coroutine() { return kind == Coroutine; } 261 bool is_generator() { return kind == Generator; } 262 bool is_monitor () { return kind == Monitor ; } 263 bool is_thread () { return kind == Thread ; } 264 265 const Decl * accept( Visitor & v ) const override { return v.visit( this ); } 266 267 const char * typeString() const override { return aggrString( kind ); } 260 bool is_coroutine() { return kind == DeclarationNode::Coroutine; } 261 bool is_monitor() { return kind == DeclarationNode::Monitor; } 262 bool is_thread() { return kind == DeclarationNode::Thread; } 263 264 const Decl * accept( Visitor & v ) const override { return v.visit( this ); } 265 266 std::string typeString() const override { return "struct"; } 268 267 269 268 private: … … 281 280 const Decl * accept( Visitor& v ) const override { return v.visit( this ); } 282 281 283 const char * typeString() const override { return aggrString( Union ); }282 std::string typeString() const override { return "union"; } 284 283 285 284 private: … … 300 299 const Decl * accept( Visitor & v ) const override { return v.visit( this ); } 301 300 302 const char * typeString() const override { return aggrString( Enum ); }301 std::string typeString() const override { return "enum"; } 303 302 304 303 private: … … 319 318 const Decl * accept( Visitor & v ) const override { return v.visit( this ); } 320 319 321 const char *typeString() const override { return "trait"; }320 std::string typeString() const override { return "trait"; } 322 321 323 322 private: … … 326 325 }; 327 326 328 /// With statement `with (...) ...`329 class WithStmt final : public Decl {330 public:331 std::vector<ptr<Expr>> exprs;332 ptr<Stmt> stmt;333 334 WithStmt( const CodeLocation & loc, std::vector<ptr<Expr>> && exprs, const Stmt * stmt )335 : Decl(loc, "", Storage::Auto, Linkage::Cforall), exprs(std::move(exprs)), stmt(stmt) {}336 337 const Decl * accept( Visitor & v ) const override { return v.visit( this ); }338 private:339 WithStmt * clone() const override { return new WithStmt{ *this }; }340 MUTATE_FRIEND341 };342 343 327 class AsmDecl : public Decl { 344 328 public: 345 329 ptr<AsmStmt> stmt; 346 330 347 AsmDecl( const CodeLocation & loc, AsmStmt * 331 AsmDecl( const CodeLocation & loc, AsmStmt *stmt ) 348 332 : Decl( loc, "", {}, {} ), stmt(stmt) {} 349 333 350 const AsmDecl * accept( Visitor & 351 private: 352 AsmDecl * 334 const AsmDecl * accept( Visitor &v ) const override { return v.visit( this ); } 335 private: 336 AsmDecl *clone() const override { return new AsmDecl( *this ); } 353 337 MUTATE_FRIEND 354 338 }; … … 362 346 : Decl( loc, "", {}, {} ), cond( condition ), msg( msg ) {} 363 347 364 const StaticAssertDecl * accept( Visitor & 348 const StaticAssertDecl * accept( Visitor &v ) const override { return v.visit( this ); } 365 349 private: 366 350 StaticAssertDecl * clone() const override { return new StaticAssertDecl( *this ); }
Note:
See TracChangeset
for help on using the changeset viewer.