Commit 8d2b8ff6 authored by Sarah Spall's avatar Sarah Spall Committed by Vikram Narayanan

scoping changes to support new syntax

parent 05359166
#include "lcd_ast.h"
#include <stdio.h>
RootScope* RootScope::instance_ = 0;
GlobalScope* GlobalScope::instance_ = 0;
RootScope::RootScope()
GlobalScope::GlobalScope()
{
// init builtin int types
this->types_ = new std::map<std::string, Type*>();
this->outer_scope_ = 0x0; // global scope
// move this code to wherever we create the root scope.
// instert for each builtin in type, add size to type if not done alreayd
this->types_->insert(std::pair<std::string,Type*>("void", new VoidType()));
this->types_->insert( std::pair<std::string,Type*>("char"
this->types_definitions_.insert(std::pair<std::string,Type*>("void", new VoidType()));
this->types_definitions_.insert( std::pair<std::string,Type*>("char"
, new IntegerType(pt_char_t, false, sizeof(char))));
this->types_->insert( std::pair<std::string,Type*>("unsigned char"
this->types_definitions_.insert( std::pair<std::string,Type*>("unsigned char"
, new IntegerType(pt_char_t, true, sizeof(char))));
this->types_->insert( std::pair<std::string,Type*>("short"
this->types_definitions_.insert( std::pair<std::string,Type*>("short"
, new IntegerType(pt_short_t, false, sizeof(short))));
this->types_->insert( std::pair<std::string,Type*>("unsigned short"
this->types_definitions_.insert( std::pair<std::string,Type*>("unsigned short"
, new IntegerType(pt_short_t, true, sizeof(short))));
this->types_->insert( std::pair<std::string,Type*>("int"
this->types_definitions_.insert( std::pair<std::string,Type*>("int"
, new IntegerType(pt_int_t, false, sizeof(int))));
this->types_->insert( std::pair<std::string,Type*>("unsigned int"
this->types_definitions_.insert( std::pair<std::string,Type*>("unsigned int"
, new IntegerType(pt_int_t, true, sizeof(int))));
this->types_->insert( std::pair<std::string,Type*>("long"
this->types_definitions_.insert( std::pair<std::string,Type*>("long"
, new IntegerType(pt_long_t, false, sizeof(long))));
this->types_->insert( std::pair<std::string,Type*>("unsigned long"
this->types_definitions_.insert( std::pair<std::string,Type*>("unsigned long"
, new IntegerType(pt_long_t, true, sizeof(long))));
this->types_->insert( std::pair<std::string,Type*>("long long"
this->types_definitions_.insert( std::pair<std::string,Type*>("long long"
, new IntegerType(pt_longlong_t, false, sizeof(long long))));
this->types_->insert( std::pair<std::string,Type*>("unsigned long long"
this->types_definitions_.insert( std::pair<std::string,Type*>("unsigned long long"
, new IntegerType(pt_longlong_t, true, sizeof(long long))));
this->types_->insert( std::pair<std::string,Type*>("capability"
this->types_definitions_.insert( std::pair<std::string,Type*>("capability"
, new IntegerType(pt_capability_t, false, sizeof(int))));
}
RootScope* RootScope::instance()
GlobalScope* GlobalScope::instance()
{
if(!RootScope::instance_)
RootScope::instance_ = new RootScope();
if(!GlobalScope::instance_)
GlobalScope::instance_ = new GlobalScope();
return instance_;
}
Marshal_type* RootScope::accept(MarshalVisitor *visitor, Registers* reg)
{
return visitor->visit(this, reg);
}
Type * RootScope::lookup_symbol(const char * sym, int* err)
Type * GlobalScope::lookup(const char * sym, int* err)
{
std::string temp(sym);
if(this->types_->find(temp) == this->types_->end())
if(this->types_definitions_.find(temp) == this->type_definitions_.end())
{
*err = 0;
return 0;
......@@ -58,55 +54,46 @@ Type * RootScope::lookup_symbol(const char * sym, int* err)
else
{
*err = 1;
return (*(this->types_))[temp];
return type_definitions_[temp];
}
}
int RootScope::insert_symbol(const char* sym, Type * value)
int GlobalScope::insert(const char* sym, Type * value)
{
std::string temp(sym);
printf("insert %s\n",temp.c_str());
std::pair<std::map<std::string,Type*>::iterator,bool> ret;
ret = types_->insert(std::pair<std::string, Type*>(temp, value));
ret = type_definitions_.insert(std::pair<std::string, Type*>(temp, value));
return ret.second;
}
// file scope
Type* FileScope::lookup_symbol(const char* sym, int* err)
{
std::string temp(sym);
if(this->types_->find(temp) == this->types_->end())
{
return this->root_->lookup_symbol(sym, err);
}
else
{
*err = 1;
return (*types_)[temp];
}
}
/* -------------------------------------------------------------- */
int FileScope::insert_symbol(const char* sym, Type* value)
LexicalScope::LexicalScope()
{
std::string temp(sym);
// redefinition of something at root scope?
std::pair<std::map<std::string,Type*>::iterator, bool> ret;
// filescope map not rootscope
ret = this->types_->insert(std::pair<std::string,Type*>(temp, value));
return ret.second;
this->outer_scope_ = 0x0;
}
FileScope::FileScope(RootScope* root)
Type* LexicalScope::lookup(const char *symbol, int *err)
{
this->root_ = root;
this->types_ = new std::map<std::string, Type*>();
std::string temp(symbol);
if(this->type_definitions_.find(temp) == this->type_definitions_.end()) {
return this->outer_scope_->lookup_symbol(symbol, err);
}
else {
*err = 1;
return type_definitions_[temp];
}
}
Marshal_type* FileScope::accept(MarshalVisitor *visitor, Registers *r)
int LexicalScope::insert(const char *symbol, Type *type)
{
return visitor->visit(this, r);
std::string temp(sym);
printf("insert %s\n",temp.c_str());
std::pair<std::map<std::string,Type*>::iterator,bool> ret;
ret = type_definitions_.insert(std::pair<std::string, Type*>(temp, value));
return ret.second;
}
......@@ -23,6 +23,33 @@ class Base
virtual Marshal_type* accept(MarshalVisitor *worker, Registers *data) = 0;
};
class LexicalScope : public Base
{
LexicalScope *outer_scope_;
std::map<std::string, Type*> type_definitions_;
std::vector<Rpc*> rpc_definitions_; // rpc or function pointer
std::vector<LexicalScope*> inner_scopes_;
public:
LexicalScope();
virtual Type* lookup(const char* sym, int* err);
virtual int insert(const char* sym, Type* type);
};
class GlobalScope : public LexicalScope
{
static GlobalScope *instance_;
LexicalScope *outer_scope_;
std::map<std::string, Type*> type_definitions_;
std::vector<Rpc*> rpc_definitions_; // rpc or function pointer
std::vector<LexicalScope*> inner_scopes_;
public:
GlobalScope();
virtual Type* lookup(const char *symbol, int *err);
virtual int insert(const char *symbol, Type *type);
static GlobalScope* instance();
};
class Type : public Base
{
public:
......@@ -44,45 +71,7 @@ class Variable : public Base
virtual Marshal_type* marshal_info() = 0;
virtual Rpc* scope() = 0;
};
class LexicalScope : public Base
{
LexicalScope *outer_scope_;
std::map<std::string, Type*> type_definitions_;
std::map<std::string, Rpc*> rpc_definitions_; // rpc or function pointer
public:
virtual Type* lookup_symbol(const char* sym, int* err) =0;
virtual int insert_symbol(const char* sym, Type* type) =0;
virtual Marshal_type* accept(MarshalVisitor *worker, Registers *data) = 0;
};
class RootScope : public Scope
{
static RootScope* instance_;
std::map<std::string,Type*>* types_;
void init_types();
RootScope(void);
public:
static int test();
static RootScope* instance();
virtual Type* lookup_symbol(const char* sym, int* err);
virtual int insert_symbol(const char* sym, Type* type);
virtual Marshal_type* accept(MarshalVisitor *worker, Registers *data);
};
class FileScope : public Scope
{
RootScope* root_;
std::map<std::string, Type*> *types_;
public:
FileScope(RootScope* root);
virtual Type* lookup_symbol(const char* sym, int* err);
virtual int insert_symbol(const char* type, Type* t);
virtual Marshal_type* accept(MarshalVisitor *worker, Registers *data);
};
class Typedef : public Type
{
Type* type_;
......@@ -272,18 +261,23 @@ class Rpc : public Base
SymbolTable* symbol_table();
};
class File : public Base
class Module : public Base
{
const char* verbatim_;
FileScope * scope_; // has pointer to root scope
std::vector<Rpc *> rpc_defs_;
// std::vector<Message *>* message_defs_;
const char *verbatim_;
LexicalScope *module_scope_;
std::vector<Rpc*> rpc_defs_;
public:
File(const char* verbatim, FileScope* fs, std::vector<Rpc* > rpc_definitions);
virtual Marshal_type* accept(MarshalVisitor *worker, Registers *data);
std::vector<Rpc*> rpc_defs();
File(const char* verbatim, LexicalScope* scope, std::vector<Rpc*> rpc_definitions);
std::vector<Rpc*> rpc_definitions();
};
class Project : public Base
{
LexicalScope *project_scope_;
std::vector<Module*> project_modules_;
public:
Project(LexicalScope global, std::vector<Module*> modules);
};
class TypeNameVisitor // generates CCSTTypeName for each type.
......@@ -296,6 +290,8 @@ class TypeNameVisitor // generates CCSTTypeName for each type.
CCSTTypeName* visit(ProjectionType *pt);
};
class AllocateTypeVisitor
{
Type* first_non_pointer(PointerType *pt);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment