Skip to content

Commit cbbaf92

Browse files
zhiayangzhiayang
zhiayang
authored andcommitted
fix long-standing bugs with exported types and lexer errors
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.
1 parent de7adba commit cbbaf92

File tree

5 files changed

+13
-23
lines changed

5 files changed

+13
-23
lines changed

source/frontend/collector.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -93,13 +93,13 @@ namespace frontend
9393
// note that we're guaranteed (because that's the whole point)
9494
// that any module we encounter here will have had all of its dependencies checked already
9595

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

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

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

113-
imports.push_back({ ithing, stree });
113+
imports.push_back({ ithing, dtree });
114114
}
115115

116116
// i guess we always add prelude definitions?

source/frontend/file.cpp

+1-11
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ namespace frontend
2525
std::vector<size_t> importIndices;
2626

2727
bool didLex = false;
28-
bool isLexing = false;
2928
};
3029

3130
static util::hash_map<std::string, FileInnards> fileList;
@@ -35,7 +34,7 @@ namespace frontend
3534
// break early if we can
3635
{
3736
auto it = fileList.find(fullPath);
38-
if(it != fileList.end() && it->second.didLex)
37+
if(it != fileList.end())
3938
return it->second;
4039
}
4140

@@ -89,21 +88,13 @@ namespace frontend
8988
}
9089
}
9190

92-
9391
Location pos;
9492
FileInnards& innards = fileList[fullPath];
95-
if(innards.isLexing)
96-
{
97-
warn("attempting to lex file while file is already being lexed, stop it");
98-
return innards;
99-
}
100-
10193
{
10294
pos.fileID = getFileIDFromFilename(fullPath);
10395

10496
innards.fileContents = std::move(fileContents);
10597
innards.lines = std::move(rawlines);
106-
innards.isLexing = true;
10798
}
10899

109100
lexer::TokenList& ts = innards.tokens;
@@ -133,7 +124,6 @@ namespace frontend
133124
}
134125

135126
innards.didLex = true;
136-
innards.isLexing = false;
137127
return innards;
138128
}
139129

source/include/typecheck.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -110,14 +110,14 @@ namespace sst
110110

111111
struct TypecheckState
112112
{
113-
TypecheckState(StateTree* st) : dtree(new DefinitionTree(st)), stree(dtree->base) { }
113+
TypecheckState(StateTree* st) : dtree(new DefinitionTree(st)), stree(dtree->base), typeDefnMap(dtree->typeDefnMap) { }
114114

115115
std::string moduleName;
116116

117117
DefinitionTree* dtree = 0;
118118
StateTree* stree = 0;
119119

120-
util::hash_map<fir::Type*, TypeDefn*> typeDefnMap;
120+
util::hash_map<fir::Type*, TypeDefn*>& typeDefnMap;
121121

122122
std::vector<Location> locationStack;
123123

@@ -207,7 +207,7 @@ namespace sst
207207
};
208208

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

212212

213213
StateTree* addTreeToExistingTree(StateTree* to, StateTree* from, StateTree* commonParent, bool pubImport, bool ignoreVis);

source/typecheck/dotop.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,7 @@ static sst::Expr* doExpressionDotOp(sst::TypecheckState* fs, ast::DotOperator* d
462462

463463
// ok.
464464
auto defn = fs->typeDefnMap[type];
465+
iceAssert(defn);
465466

466467
if(auto str = dcast(sst::StructDefn, defn))
467468
{

source/typecheck/toplevel.cpp

+4-5
Original file line numberDiff line numberDiff line change
@@ -296,8 +296,8 @@ namespace sst
296296

297297

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

358358
iceAssert(insertPoint);
359359

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

363363
fs->dtree->thingsImported.insert(ithing.name);
364+
fs->dtree->typeDefnMap.insert(import->typeDefnMap.begin(), import->typeDefnMap.end());
364365
}
365366

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

374375
fs->dtree->topLevel = tns;
375-
376-
fs->dtree->typeDefnMap = fs->typeDefnMap;
377376
return fs->dtree;
378377
}
379378
}

0 commit comments

Comments
 (0)