From 985dec101dfa522f8b90fcee58ed1b2f1256e189 Mon Sep 17 00:00:00 2001 From: Fangrui Song Date: Sat, 3 Feb 2018 18:34:07 -0800 Subject: [PATCH] Generation --- src/query.cc | 89 +++++++++++++++++++++++++++++++++++------------ src/query.h | 12 ------- src/query_utils.h | 5 +-- 3 files changed, 70 insertions(+), 36 deletions(-) diff --git a/src/query.cc b/src/query.cc index 2ee7478c..2582a908 100644 --- a/src/query.cc +++ b/src/query.cc @@ -373,6 +373,46 @@ void RemoveRangeWithGen(std::vector>* dest, const std::vector& to_ dest->end()); } +void UpdateGen(QueryDatabase* db, WithGen& ref) { + ref.gen = db->funcs[ref.value.id].gen; +} +void UpdateGen(QueryDatabase* db, WithGen& ref) { + ref.gen = db->types[ref.value.id].gen; +} +void UpdateGen(QueryDatabase* db, WithGen& ref) { + ref.gen = db->vars[ref.value.id].gen; +} + +template +void UpdateGen(QueryDatabase* db, Maybe& ref) { + if (ref) + UpdateGen(db, *ref); +} + +template +void UpdateGen(QueryDatabase* db, std::vector& ref) { + for (T& x : ref) + UpdateGen(db, x); +} + +void UpdateGen(QueryDatabase* db, QueryFunc::Def& def) { + UpdateGen(db, def.declaring_type); + UpdateGen(db, def.base); + UpdateGen(db, def.locals); +} + +void UpdateGen(QueryDatabase* db, QueryType::Def& def) { + UpdateGen(db, def.alias_of); + UpdateGen(db, def.parents); + UpdateGen(db, def.funcs); + UpdateGen(db, def.types); + UpdateGen(db, def.vars); +} + +void UpdateGen(QueryDatabase* db, QueryVar::Def& def) { + UpdateGen(db, def.variable_type); +} + } // namespace template <> @@ -769,6 +809,7 @@ void QueryDatabase::RemoveUsrs(SymbolKind usr_kind, if (type.symbol_idx) symbols[type.symbol_idx->id].kind = SymbolKind::Invalid; type.gen++; + //type.def = QueryType::Def(); type.def = nullopt; } break; @@ -779,6 +820,7 @@ void QueryDatabase::RemoveUsrs(SymbolKind usr_kind, if (func.symbol_idx) symbols[func.symbol_idx->id].kind = SymbolKind::Invalid; func.gen++; + //func.def = QueryFunc::Def(); func.def = nullopt; } break; @@ -789,6 +831,7 @@ void QueryDatabase::RemoveUsrs(SymbolKind usr_kind, if (var.symbol_idx) symbols[var.symbol_idx->id].kind = SymbolKind::Invalid; var.gen++; + //var.def = QueryVar::Def(); var.def = nullopt; } break; @@ -820,6 +863,7 @@ void QueryDatabase::ApplyIndexUpdate(IndexUpdate* update) { AddRangeWithGen(&def.def_var_name, merge_update.to_add, def.gen); \ RemoveRangeWithGen(&def.def_var_name, merge_update.to_remove); \ VerifyUnique(def.def_var_name); \ + UpdateGen(this, def.def_var_name); \ } for (const std::string& filename : update->files_removed) @@ -876,13 +920,13 @@ void QueryDatabase::ImportOrUpdate( QueryType& existing = types[it->second.id]; // Keep the existing definition if it is higher quality. - if (existing.def && existing.def->definition_spelling && - !def.value.definition_spelling) - continue; - - existing.def = def.value; - UpdateSymbols(&existing.symbol_idx, SymbolKind::Type, - it->second.id); + if (!(existing.def && existing.def->definition_spelling && + !def.value.definition_spelling)) { + existing.def = def.value; + UpdateSymbols(&existing.symbol_idx, SymbolKind::Type, + it->second.id); + } + UpdateGen(this, *existing.def); } } @@ -900,13 +944,13 @@ void QueryDatabase::ImportOrUpdate( QueryFunc& existing = funcs[it->second.id]; // Keep the existing definition if it is higher quality. - if (existing.def && existing.def->definition_spelling && - !def.value.definition_spelling) - continue; - - existing.def = def.value; - UpdateSymbols(&existing.symbol_idx, SymbolKind::Func, - it->second.id); + if (!(existing.def && existing.def->definition_spelling && + !def.value.definition_spelling)) { + existing.def = def.value; + UpdateSymbols(&existing.symbol_idx, SymbolKind::Func, + it->second.id); + } + UpdateGen(this, *existing.def); } } @@ -924,14 +968,14 @@ void QueryDatabase::ImportOrUpdate( QueryVar& existing = vars[it->second.id]; // Keep the existing definition if it is higher quality. - if (existing.def && existing.def->definition_spelling && - !def.value.definition_spelling) - continue; - - existing.def = def.value; - if (!def.value.is_local()) - UpdateSymbols(&existing.symbol_idx, SymbolKind::Var, - it->second.id); + if (!(existing.def && existing.def->definition_spelling && + !def.value.definition_spelling)) { + existing.def = def.value; + if (!def.value.is_local()) + UpdateSymbols(&existing.symbol_idx, SymbolKind::Var, + it->second.id); + } + UpdateGen(this, *existing.def); } } @@ -994,6 +1038,7 @@ std::string_view QueryDatabase::GetSymbolShortName(RawId symbol_idx) const { return ""; } + TEST_SUITE("query") { IndexUpdate GetDelta(IndexFile previous, IndexFile current) { QueryDatabase db; diff --git a/src/query.h b/src/query.h index 2ee981b0..18d700ec 100644 --- a/src/query.h +++ b/src/query.h @@ -478,18 +478,6 @@ struct IdMap { QueryFuncRef ToQuery(IndexFuncRef ref) const; QueryLocation ToQuery(IndexFunc::Declaration decl) const; template - optional::type> ToQuery(optional id) const { - if (!id) - return nullopt; - return ToQuery(*id); - } - template - optional::type>> ToQuery(optional id, int) const { - if (!id) - return nullopt; - return ToQuery(*id, 0); - } - template Maybe::type> ToQuery(Maybe id) const { if (!id) return nullopt; diff --git a/src/query_utils.h b/src/query_utils.h index 6df1dca1..5eddf697 100644 --- a/src/query_utils.h +++ b/src/query_utils.h @@ -77,7 +77,8 @@ template void EachWithGen(std::vector& collection, WithGen> x, std::function fn) { Q& obj = collection[x.value.id]; // FIXME Deprecate optional def - if (obj.gen == x.gen && obj.def) + // if (obj.gen == x.gen && obj.def) + if (obj.def) fn(obj); } @@ -86,7 +87,7 @@ void EachWithGen(std::vector& collection, std::vector>>& ids, s size_t j = 0; for (WithGen> x : ids) { Q& obj = collection[x.value.id]; - if (obj.gen == x.gen) { + if (1 /*obj.gen == x.gen*/) { if (obj.def) // FIXME Deprecate optional def fn(obj); ids[j++] = x;