Skip to content

Commit 627e900

Browse files
committed
[flang][NFC] Change how error symbols are recorded
When an error is associated with a symbol, it was marked with a flag from Symbol::Flag. The problem with that is that you need a mutable symbol to do that. Instead, store the set of error symbols in the SemanticsContext. This allows for some const_casts to be eliminated. Also, improve the internal error that occurs if SetError is called but no fatal error has been reported. Differential Revision: https://reviews.llvm.org/D86740
1 parent 316d336 commit 627e900

File tree

8 files changed

+29
-26
lines changed

8 files changed

+29
-26
lines changed

flang/include/flang/Semantics/semantics.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ class SemanticsContext {
131131
bool HasError(const Symbol &);
132132
bool HasError(const Symbol *);
133133
bool HasError(const parser::Name &);
134-
void SetError(Symbol &, bool = true);
134+
void SetError(const Symbol &, bool = true);
135135

136136
template <typename... A> parser::Message &Say(A &&...args) {
137137
CHECK(location_);
@@ -175,7 +175,7 @@ class SemanticsContext {
175175
private:
176176
void CheckIndexVarRedefine(
177177
const parser::CharBlock &, const Symbol &, parser::MessageFixedText &&);
178-
bool CheckError(bool);
178+
void CheckError(const Symbol &);
179179

180180
const common::IntrinsicTypeDefaultKinds &defaultKinds_;
181181
const common::LanguageFeatureControl languageFeatures_;
@@ -197,6 +197,7 @@ class SemanticsContext {
197197
IndexVarKind kind;
198198
};
199199
std::map<SymbolRef, const IndexVarInfo> activeIndexVars_;
200+
std::set<SymbolRef> errorSymbols_;
200201
std::vector<std::string> tempNames_;
201202
};
202203

flang/include/flang/Semantics/symbol.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,6 @@ std::string DetailsToString(const Details &);
478478
class Symbol {
479479
public:
480480
ENUM_CLASS(Flag,
481-
Error, // an error has been reported on this symbol
482481
Function, // symbol is a function
483482
Subroutine, // symbol is a subroutine
484483
StmtFunction, // symbol is a statement function (Function is set too)

flang/include/flang/Semantics/type.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ class DerivedTypeSpec {
263263
// Converts the raw parameter list to a map, naming each actual parameter.
264264
void CookParameters(evaluate::FoldingContext &);
265265
// Evaluates type parameter expressions.
266-
void EvaluateParameters(evaluate::FoldingContext &);
266+
void EvaluateParameters(SemanticsContext &);
267267
void AddParamValue(SourceName, ParamValue &&);
268268
// Creates a Scope for the type and populates it with component
269269
// instantiations that have been specialized with actual type parameter

flang/lib/Semantics/expression.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -922,7 +922,7 @@ MaybeExpr ExpressionAnalyzer::Analyze(const parser::ArrayElement &ae) {
922922
if (const Symbol * symbol{GetLastSymbol(*baseExpr)}) {
923923
if (!context_.HasError(symbol)) {
924924
Say("'%s' is not an array"_err_en_US, symbol->name());
925-
context_.SetError(const_cast<Symbol &>(*symbol));
925+
context_.SetError(*symbol);
926926
}
927927
}
928928
} else if (std::optional<DataRef> dataRef{
@@ -1824,13 +1824,13 @@ bool ExpressionAnalyzer::ResolveForward(const Symbol &symbol) {
18241824
// allowed in specification parts (10.1.11 para 5).
18251825
Say("The module function '%s' may not be referenced recursively in a specification expression"_err_en_US,
18261826
symbol.name());
1827-
context_.SetError(const_cast<Symbol &>(symbol));
1827+
context_.SetError(symbol);
18281828
return false;
18291829
}
18301830
} else { // 10.1.11 para 4
18311831
Say("The internal function '%s' may not be referenced in a specification expression"_err_en_US,
18321832
symbol.name());
1833-
context_.SetError(const_cast<Symbol &>(symbol));
1833+
context_.SetError(symbol);
18341834
return false;
18351835
}
18361836
}

flang/lib/Semantics/resolve-names.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3598,7 +3598,7 @@ void DeclarationVisitor::Post(const parser::DerivedTypeSpec &x) {
35983598
// Normalize parameters to produce a better search key.
35993599
spec->CookParameters(GetFoldingContext());
36003600
if (!spec->MightBeParameterized()) {
3601-
spec->EvaluateParameters(GetFoldingContext());
3601+
spec->EvaluateParameters(context());
36023602
}
36033603
if (const DeclTypeSpec *
36043604
extant{currScope().FindInstantiatedDerivedType(*spec, category)}) {
@@ -3647,7 +3647,7 @@ bool DeclarationVisitor::Pre(const parser::DerivedTypeDef &x) {
36473647
BeginAttrs();
36483648
Symbol *typeParam{MakeTypeSymbol(
36493649
paramName, TypeParamDetails{common::TypeParamAttr::Len})};
3650-
typeParam->set(Symbol::Flag::Error);
3650+
context().SetError(*typeParam);
36513651
EndAttrs();
36523652
} else if (!symbol->has<TypeParamDetails>()) {
36533653
Say2(paramName, "'%s' is not defined as a type parameter"_err_en_US,
@@ -4778,7 +4778,7 @@ bool DeclarationVisitor::OkToAddComponent(
47784778
for (const Scope *scope{&currScope()}; scope;) {
47794779
CHECK(scope->IsDerivedType());
47804780
if (auto *prev{FindInScope(*scope, name)}) {
4781-
if (!prev->test(Symbol::Flag::Error)) {
4781+
if (!context().HasError(*prev)) {
47824782
auto msg{""_en_US};
47834783
if (extends) {
47844784
msg = "Type cannot be extended as it has a component named"

flang/lib/Semantics/semantics.cpp

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -221,23 +221,28 @@ bool SemanticsContext::AnyFatalError() const {
221221
(warningsAreErrors_ || messages_.AnyFatalError());
222222
}
223223
bool SemanticsContext::HasError(const Symbol &symbol) {
224-
return CheckError(symbol.test(Symbol::Flag::Error));
224+
return errorSymbols_.count(symbol) > 0;
225225
}
226226
bool SemanticsContext::HasError(const Symbol *symbol) {
227-
return CheckError(!symbol || HasError(*symbol));
227+
return !symbol || HasError(*symbol);
228228
}
229229
bool SemanticsContext::HasError(const parser::Name &name) {
230230
return HasError(name.symbol);
231231
}
232-
void SemanticsContext::SetError(Symbol &symbol, bool value) {
232+
void SemanticsContext::SetError(const Symbol &symbol, bool value) {
233233
if (value) {
234-
CHECK(AnyFatalError());
235-
symbol.set(Symbol::Flag::Error);
234+
CheckError(symbol);
235+
errorSymbols_.emplace(symbol);
236236
}
237237
}
238-
bool SemanticsContext::CheckError(bool error) {
239-
CHECK(!error || AnyFatalError());
240-
return error;
238+
void SemanticsContext::CheckError(const Symbol &symbol) {
239+
if (!AnyFatalError()) {
240+
std::string buf;
241+
llvm::raw_string_ostream ss{buf};
242+
ss << symbol;
243+
common::die(
244+
"No error was reported but setting error on: %s", ss.str().c_str());
245+
}
241246
}
242247

243248
const Scope &SemanticsContext::FindScope(parser::CharBlock source) const {

flang/lib/Semantics/tools.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -965,8 +965,7 @@ SymbolVector OrderParameterDeclarations(const Symbol &typeSymbol) {
965965
const DeclTypeSpec &FindOrInstantiateDerivedType(Scope &scope,
966966
DerivedTypeSpec &&spec, SemanticsContext &semanticsContext,
967967
DeclTypeSpec::Category category) {
968-
spec.CookParameters(semanticsContext.foldingContext());
969-
spec.EvaluateParameters(semanticsContext.foldingContext());
968+
spec.EvaluateParameters(semanticsContext);
970969
if (const DeclTypeSpec *
971970
type{scope.FindInstantiatedDerivedType(spec, category)}) {
972971
return *type;

flang/lib/Semantics/type.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,8 @@ void DerivedTypeSpec::CookParameters(evaluate::FoldingContext &foldingContext) {
9797
}
9898
}
9999

100-
void DerivedTypeSpec::EvaluateParameters(
101-
evaluate::FoldingContext &foldingContext) {
100+
void DerivedTypeSpec::EvaluateParameters(SemanticsContext &context) {
101+
evaluate::FoldingContext &foldingContext{context.foldingContext()};
102102
CookParameters(foldingContext);
103103
if (evaluated_) {
104104
return;
@@ -123,7 +123,7 @@ void DerivedTypeSpec::EvaluateParameters(
123123
continue;
124124
}
125125
}
126-
if (!symbol.test(Symbol::Flag::Error)) {
126+
if (!context.HasError(symbol)) {
127127
evaluate::SayWithDeclaration(messages, symbol,
128128
"Value of type parameter '%s' (%s) is not convertible to its"
129129
" type"_err_en_US,
@@ -150,7 +150,7 @@ void DerivedTypeSpec::EvaluateParameters(
150150
auto expr{
151151
evaluate::Fold(foldingContext, common::Clone(details.init()))};
152152
AddParamValue(name, ParamValue{std::move(*expr), details.attr()});
153-
} else if (!symbol.test(Symbol::Flag::Error)) {
153+
} else if (!context.HasError(symbol)) {
154154
messages.Say(name_,
155155
"Type parameter '%s' lacks a value and has no default"_err_en_US,
156156
name);
@@ -220,8 +220,7 @@ void DerivedTypeSpec::Instantiate(
220220
typeSymbol_.name());
221221
return;
222222
}
223-
CookParameters(foldingContext);
224-
EvaluateParameters(foldingContext);
223+
EvaluateParameters(context);
225224
const Scope &typeScope{DEREF(typeSymbol_.scope())};
226225
if (!MightBeParameterized()) {
227226
scope_ = &typeScope;

0 commit comments

Comments
 (0)