Skip to content

Commit

Permalink
fix long-standing bugs with exported types and lexer errors
Browse files Browse the repository at this point in the history
1. errors encountered while lexing no longer throw a spurious "already lexing file" error
2. types get exported properly across modules now... which was apparently broken for a long time.
  • Loading branch information
zhiayang authored and zhiayang committed Oct 4, 2019
1 parent de7adba commit cbbaf92
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 23 deletions.
8 changes: 4 additions & 4 deletions source/frontend/collector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,13 @@ namespace frontend
// note that we're guaranteed (because that's the whole point)
// that any module we encounter here will have had all of its dependencies checked already

std::vector<std::pair<ImportThing, sst::StateTree*>> imports;
std::vector<std::pair<ImportThing, sst::DefinitionTree*>> imports;
for(auto d : state->graph->getDependenciesOf(file))
{
auto imported = d->to;

auto stree = state->dtrees[imported->name]->base;
iceAssert(stree);
auto dtree = state->dtrees[imported->name];
iceAssert(dtree);

ImportThing ithing { imported->name, d->ithing.importAs, d->ithing.pubImport, d->ithing.loc };
if(auto it = std::find_if(imports.begin(), imports.end(), [&ithing](const auto& x) -> bool { return x.first.name == ithing.name; });
Expand All @@ -110,7 +110,7 @@ namespace frontend
->postAndQuit();
}

imports.push_back({ ithing, stree });
imports.push_back({ ithing, dtree });
}

// i guess we always add prelude definitions?
Expand Down
12 changes: 1 addition & 11 deletions source/frontend/file.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ namespace frontend
std::vector<size_t> importIndices;

bool didLex = false;
bool isLexing = false;
};

static util::hash_map<std::string, FileInnards> fileList;
Expand All @@ -35,7 +34,7 @@ namespace frontend
// break early if we can
{
auto it = fileList.find(fullPath);
if(it != fileList.end() && it->second.didLex)
if(it != fileList.end())
return it->second;
}

Expand Down Expand Up @@ -89,21 +88,13 @@ namespace frontend
}
}


Location pos;
FileInnards& innards = fileList[fullPath];
if(innards.isLexing)
{
warn("attempting to lex file while file is already being lexed, stop it");
return innards;
}

{
pos.fileID = getFileIDFromFilename(fullPath);

innards.fileContents = std::move(fileContents);
innards.lines = std::move(rawlines);
innards.isLexing = true;
}

lexer::TokenList& ts = innards.tokens;
Expand Down Expand Up @@ -133,7 +124,6 @@ namespace frontend
}

innards.didLex = true;
innards.isLexing = false;
return innards;
}

Expand Down
6 changes: 3 additions & 3 deletions source/include/typecheck.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,14 +110,14 @@ namespace sst

struct TypecheckState
{
TypecheckState(StateTree* st) : dtree(new DefinitionTree(st)), stree(dtree->base) { }
TypecheckState(StateTree* st) : dtree(new DefinitionTree(st)), stree(dtree->base), typeDefnMap(dtree->typeDefnMap) { }

std::string moduleName;

DefinitionTree* dtree = 0;
StateTree* stree = 0;

util::hash_map<fir::Type*, TypeDefn*> typeDefnMap;
util::hash_map<fir::Type*, TypeDefn*>& typeDefnMap;

std::vector<Location> locationStack;

Expand Down Expand Up @@ -207,7 +207,7 @@ namespace sst
};

DefinitionTree* typecheck(frontend::CollectorState* cs, const parser::ParsedFile& file,
const std::vector<std::pair<frontend::ImportThing, StateTree*>>& imports, bool addPreludeDefinitions);
const std::vector<std::pair<frontend::ImportThing, DefinitionTree*>>& imports, bool addPreludeDefinitions);


StateTree* addTreeToExistingTree(StateTree* to, StateTree* from, StateTree* commonParent, bool pubImport, bool ignoreVis);
Expand Down
1 change: 1 addition & 0 deletions source/typecheck/dotop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,7 @@ static sst::Expr* doExpressionDotOp(sst::TypecheckState* fs, ast::DotOperator* d

// ok.
auto defn = fs->typeDefnMap[type];
iceAssert(defn);

if(auto str = dcast(sst::StructDefn, defn))
{
Expand Down
9 changes: 4 additions & 5 deletions source/typecheck/toplevel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -296,8 +296,8 @@ namespace sst


using frontend::CollectorState;
DefinitionTree* typecheck(CollectorState* cs, const parser::ParsedFile& file, const std::vector<std::pair<frontend::ImportThing, StateTree*>>& imports,
bool addPreludeDefinitions)
DefinitionTree* typecheck(CollectorState* cs, const parser::ParsedFile& file,
const std::vector<std::pair<frontend::ImportThing, DefinitionTree*>>& imports, bool addPreludeDefinitions)
{
StateTree* tree = new StateTree(file.moduleName, file.name, 0);
tree->treeDefn = util::pool<TreeDefn>(Location());
Expand Down Expand Up @@ -357,10 +357,11 @@ namespace sst

iceAssert(insertPoint);

_addTreeToExistingTree(fs->dtree->thingsImported, insertPoint, import, /* commonParent: */ nullptr, ithing.pubImport,
_addTreeToExistingTree(fs->dtree->thingsImported, insertPoint, import->base, /* commonParent: */ nullptr, ithing.pubImport,
/* ignoreVis: */ false, file.name);

fs->dtree->thingsImported.insert(ithing.name);
fs->dtree->typeDefnMap.insert(import->typeDefnMap.begin(), import->typeDefnMap.end());
}

if(addPreludeDefinitions)
Expand All @@ -372,8 +373,6 @@ namespace sst
tns->name = file.moduleName;

fs->dtree->topLevel = tns;

fs->dtree->typeDefnMap = fs->typeDefnMap;
return fs->dtree;
}
}
Expand Down

0 comments on commit cbbaf92

Please sign in to comment.