[flang-commits] [flang] [flang] Detect use-before-decl errors on type parameters (PR #99947)
via flang-commits
flang-commits at lists.llvm.org
Mon Jul 22 14:09:49 PDT 2024
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-flang-semantics
Author: Peter Klausler (klausler)
<details>
<summary>Changes</summary>
Ensure that type parameters are declared as such before being referenced within the derived type definition. (Previously, such references would resolve to symbols in the enclosing scope.)
This change causes the symbols for the type parameters to be created when the TYPE statement is processed in name resolution. They are TypeParamDetails symbols with no KIND/LEN attribute set, and they shadow any symbols of the same name in the enclosing scope.
When the type parameter declarations are processed, the KIND/LEN attributes are set. Any earlier reference to a type parameter with no KIND/LEN attribute elicits an error.
Some members of TypeParamDetails have been retyped &/or renamed.
---
Patch is 23.34 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/99947.diff
10 Files Affected:
- (modified) flang/include/flang/Semantics/symbol.h (+16-11)
- (modified) flang/include/flang/Semantics/tools.h (+1-1)
- (modified) flang/lib/Semantics/check-declarations.cpp (+1-1)
- (modified) flang/lib/Semantics/mod-file.cpp (+11-12)
- (modified) flang/lib/Semantics/resolve-names.cpp (+47-38)
- (modified) flang/lib/Semantics/runtime-type-info.cpp (+5-4)
- (modified) flang/lib/Semantics/symbol.cpp (+13-2)
- (modified) flang/lib/Semantics/tools.cpp (+4-4)
- (modified) flang/lib/Semantics/type.cpp (+18-16)
- (modified) flang/test/Semantics/resolve33.f90 (+11-3)
``````````diff
diff --git a/flang/include/flang/Semantics/symbol.h b/flang/include/flang/Semantics/symbol.h
index cdbe3e39386bb..cf0350735b5b9 100644
--- a/flang/include/flang/Semantics/symbol.h
+++ b/flang/include/flang/Semantics/symbol.h
@@ -460,15 +460,19 @@ class ProcEntityDetails : public EntityDetails, public WithPassArg {
// and specialized for each distinct set of type parameter values.
class DerivedTypeDetails {
public:
- const std::list<SourceName> ¶mNames() const { return paramNames_; }
- const SymbolVector ¶mDecls() const { return paramDecls_; }
+ const SymbolVector ¶mNameOrder() const { return paramNameOrder_; }
+ const SymbolVector ¶mDeclOrder() const { return paramDeclOrder_; }
bool sequence() const { return sequence_; }
bool isDECStructure() const { return isDECStructure_; }
std::map<SourceName, SymbolRef> &finals() { return finals_; }
const std::map<SourceName, SymbolRef> &finals() const { return finals_; }
bool isForwardReferenced() const { return isForwardReferenced_; }
- void add_paramName(const SourceName &name) { paramNames_.push_back(name); }
- void add_paramDecl(const Symbol &symbol) { paramDecls_.push_back(symbol); }
+ void add_paramNameOrder(const Symbol &symbol) {
+ paramNameOrder_.push_back(symbol);
+ }
+ void add_paramDeclOrder(const Symbol &symbol) {
+ paramDeclOrder_.push_back(symbol);
+ }
void add_component(const Symbol &);
void set_sequence(bool x = true) { sequence_ = x; }
void set_isDECStructure(bool x = true) { isDECStructure_ = x; }
@@ -491,12 +495,12 @@ class DerivedTypeDetails {
const Symbol *GetFinalForRank(int) const;
private:
- // These are (1) the names of the derived type parameters in the order
+ // These are (1) the symbols of the derived type parameters in the order
// in which they appear on the type definition statement(s), and (2) the
// symbols that correspond to those names in the order in which their
// declarations appear in the derived type definition(s).
- std::list<SourceName> paramNames_;
- SymbolVector paramDecls_;
+ SymbolVector paramNameOrder_;
+ SymbolVector paramDeclOrder_;
// These are the names of the derived type's components in component
// order. A parent component, if any, appears first in this list.
std::list<SourceName> componentNames_;
@@ -565,18 +569,19 @@ class MiscDetails {
class TypeParamDetails {
public:
- explicit TypeParamDetails(common::TypeParamAttr attr) : attr_{attr} {}
+ TypeParamDetails() = default;
TypeParamDetails(const TypeParamDetails &) = default;
- common::TypeParamAttr attr() const { return attr_; }
+ std::optional<common::TypeParamAttr> attr() const { return attr_; }
+ TypeParamDetails &set_attr(common::TypeParamAttr);
MaybeIntExpr &init() { return init_; }
const MaybeIntExpr &init() const { return init_; }
void set_init(MaybeIntExpr &&expr) { init_ = std::move(expr); }
const DeclTypeSpec *type() const { return type_; }
- void set_type(const DeclTypeSpec &);
+ TypeParamDetails &set_type(const DeclTypeSpec &);
void ReplaceType(const DeclTypeSpec &);
private:
- common::TypeParamAttr attr_;
+ std::optional<common::TypeParamAttr> attr_;
MaybeIntExpr init_;
const DeclTypeSpec *type_{nullptr};
};
diff --git a/flang/include/flang/Semantics/tools.h b/flang/include/flang/Semantics/tools.h
index 0fcba3131fad1..fe6ee1b293da8 100644
--- a/flang/include/flang/Semantics/tools.h
+++ b/flang/include/flang/Semantics/tools.h
@@ -261,7 +261,7 @@ std::optional<parser::MessageFixedText> GetImageControlStmtCoarrayMsg(
SymbolVector OrderParameterDeclarations(const Symbol &);
// Returns the complete list of derived type parameter names in the
// order defined by 7.5.3.2.
-std::list<SourceName> OrderParameterNames(const Symbol &);
+SymbolVector OrderParameterNames(const Symbol &);
// Return an existing or new derived type instance
const DeclTypeSpec &FindOrInstantiateDerivedType(Scope &, DerivedTypeSpec &&,
diff --git a/flang/lib/Semantics/check-declarations.cpp b/flang/lib/Semantics/check-declarations.cpp
index eb898bad53a4f..a52f013a70b9d 100644
--- a/flang/lib/Semantics/check-declarations.cpp
+++ b/flang/lib/Semantics/check-declarations.cpp
@@ -2919,7 +2919,7 @@ parser::Messages CheckHelper::WhyNotInteroperableDerivedType(
if (derived->sequence()) { // C1801
msgs.Say(symbol.name(),
"An interoperable derived type cannot have the SEQUENCE attribute"_err_en_US);
- } else if (!derived->paramDecls().empty()) { // C1802
+ } else if (!derived->paramNameOrder().empty()) { // C1802
msgs.Say(symbol.name(),
"An interoperable derived type cannot have a type parameter"_err_en_US);
} else if (const auto *parent{
diff --git a/flang/lib/Semantics/mod-file.cpp b/flang/lib/Semantics/mod-file.cpp
index a1c4c031ca509..c7e7716a7b481 100644
--- a/flang/lib/Semantics/mod-file.cpp
+++ b/flang/lib/Semantics/mod-file.cpp
@@ -410,15 +410,14 @@ bool ModFileWriter::PutComponents(const Symbol &typeSymbol) {
llvm::raw_string_ostream typeBindings{buf};
UnorderedSymbolSet emitted;
SymbolVector symbols{scope.GetSymbols()};
- // Emit type parameters first
- for (const Symbol &symbol : symbols) {
- if (symbol.has<TypeParamDetails>()) {
- PutSymbol(typeBindings, symbol);
- emitted.emplace(symbol);
- }
- }
- // Emit components in component order.
+ // Emit type parameter declarations first, in order
const auto &details{typeSymbol.get<DerivedTypeDetails>()};
+ for (const Symbol &symbol : details.paramDeclOrder()) {
+ CHECK(symbol.has<TypeParamDetails>());
+ PutSymbol(typeBindings, symbol);
+ emitted.emplace(symbol);
+ }
+ // Emit actual components in component order.
for (SourceName name : details.componentNames()) {
auto iter{scope.find(name)};
if (iter != scope.end()) {
@@ -549,10 +548,10 @@ void ModFileWriter::PutDerivedType(
decls_ << ",extends(" << extends->name() << ')';
}
decls_ << "::" << typeSymbol.name();
- if (!details.paramNames().empty()) {
+ if (!details.paramNameOrder().empty()) {
char sep{'('};
- for (const auto &name : details.paramNames()) {
- decls_ << sep << name;
+ for (const SymbolRef &ref : details.paramNameOrder()) {
+ decls_ << sep << ref->name();
sep = ',';
}
decls_ << ')';
@@ -1046,7 +1045,7 @@ void ModFileWriter::PutTypeParam(llvm::raw_ostream &os, const Symbol &symbol) {
os, symbol,
[&]() {
PutType(os, DEREF(symbol.GetType()));
- PutLower(os << ',', common::EnumToString(details.attr()));
+ PutLower(os << ',', common::EnumToString(details.attr().value()));
},
symbol.attrs());
PutInit(os, details.init());
diff --git a/flang/lib/Semantics/resolve-names.cpp b/flang/lib/Semantics/resolve-names.cpp
index f761355d0da32..95d021e3aa647 100644
--- a/flang/lib/Semantics/resolve-names.cpp
+++ b/flang/lib/Semantics/resolve-names.cpp
@@ -5456,34 +5456,14 @@ bool DeclarationVisitor::Pre(const parser::DerivedTypeDef &x) {
CHECK(scope.symbol());
CHECK(scope.symbol()->scope() == &scope);
auto &details{scope.symbol()->get<DerivedTypeDetails>()};
- std::set<SourceName> paramNames;
for (auto ¶mName : std::get<std::list<parser::Name>>(stmt.statement.t)) {
- details.add_paramName(paramName.source);
- auto *symbol{FindInScope(scope, paramName)};
- if (!symbol) {
- Say(paramName,
- "No definition found for type parameter '%s'"_err_en_US); // C742
- // No symbol for a type param. Create one and mark it as containing an
- // error to improve subsequent semantic processing
- BeginAttrs();
- Symbol *typeParam{MakeTypeSymbol(
- paramName, TypeParamDetails{common::TypeParamAttr::Len})};
- context().SetError(*typeParam);
- EndAttrs();
- } else if (!symbol->has<TypeParamDetails>()) {
- Say2(paramName, "'%s' is not defined as a type parameter"_err_en_US,
- *symbol, "Definition of '%s'"_en_US); // C741
- }
- if (!paramNames.insert(paramName.source).second) {
- Say(paramName,
- "Duplicate type parameter name: '%s'"_err_en_US); // C731
- }
- }
- for (const auto &[name, symbol] : currScope()) {
- if (symbol->has<TypeParamDetails>() && !paramNames.count(name)) {
- SayDerivedType(name,
- "'%s' is not a type parameter of this derived type"_err_en_US,
- currScope()); // C741
+ if (auto *symbol{FindInScope(scope, paramName)}) {
+ if (auto *details{symbol->detailsIf<TypeParamDetails>()}) {
+ if (!details->attr()) {
+ Say(paramName,
+ "No definition found for type parameter '%s'"_err_en_US); // C742
+ }
+ }
}
}
Walk(std::get<std::list<parser::Statement<parser::PrivateOrSequence>>>(x.t));
@@ -5499,7 +5479,7 @@ bool DeclarationVisitor::Pre(const parser::DerivedTypeDef &x) {
"A sequence type should have at least one component"_warn_en_US);
}
}
- if (!details.paramNames().empty()) { // C740
+ if (!details.paramDeclOrder().empty()) { // C740
Say(stmt.source,
"A sequence type may not have type parameters"_err_en_US);
}
@@ -5559,24 +5539,50 @@ void DeclarationVisitor::Post(const parser::DerivedTypeStmt &x) {
details.add_component(comp);
}
}
+ // Create symbols now for type parameters so that they shadow names
+ // from the enclosing specification part.
+ if (auto *details{symbol.detailsIf<DerivedTypeDetails>()}) {
+ for (const auto &name : std::get<std::list<parser::Name>>(x.t)) {
+ if (Symbol * symbol{MakeTypeSymbol(name, TypeParamDetails{})}) {
+ details->add_paramNameOrder(*symbol);
+ }
+ }
+ }
EndAttrs();
}
void DeclarationVisitor::Post(const parser::TypeParamDefStmt &x) {
auto *type{GetDeclTypeSpec()};
+ DerivedTypeDetails *derivedDetails{nullptr};
+ if (Symbol * dtSym{currScope().symbol()}) {
+ derivedDetails = dtSym->detailsIf<DerivedTypeDetails>();
+ }
auto attr{std::get<common::TypeParamAttr>(x.t)};
for (auto &decl : std::get<std::list<parser::TypeParamDecl>>(x.t)) {
auto &name{std::get<parser::Name>(decl.t)};
- if (Symbol * symbol{MakeTypeSymbol(name, TypeParamDetails{attr})}) {
- SetType(name, *type);
- if (auto &init{
- std::get<std::optional<parser::ScalarIntConstantExpr>>(decl.t)}) {
- if (auto maybeExpr{AnalyzeExpr(context(), *init)}) {
- if (auto *intExpr{std::get_if<SomeIntExpr>(&maybeExpr->u)}) {
- symbol->get<TypeParamDetails>().set_init(std::move(*intExpr));
+ if (Symbol * symbol{FindInScope(currScope(), name)}) {
+ if (auto *paramDetails{symbol->detailsIf<TypeParamDetails>()}) {
+ if (!paramDetails->attr()) {
+ paramDetails->set_attr(attr);
+ SetType(name, *type);
+ if (auto &init{std::get<std::optional<parser::ScalarIntConstantExpr>>(
+ decl.t)}) {
+ if (auto maybeExpr{AnalyzeExpr(context(), *init)}) {
+ if (auto *intExpr{std::get_if<SomeIntExpr>(&maybeExpr->u)}) {
+ paramDetails->set_init(std::move(*intExpr));
+ }
+ }
+ }
+ if (derivedDetails) {
+ derivedDetails->add_paramDeclOrder(*symbol);
}
+ } else {
+ Say(name,
+ "Type parameter '%s' was already declared in this derived type"_err_en_US);
}
}
+ } else {
+ Say(name, "'%s' is not a parameter of this derived type"_err_en_US);
}
}
EndDecl();
@@ -6779,9 +6785,6 @@ Symbol *DeclarationVisitor::MakeTypeSymbol(
}
Symbol &result{MakeSymbol(name, attrs, std::move(details))};
SetCUDADataAttr(name, result, cudaDataAttr());
- if (result.has<TypeParamDetails>()) {
- derivedType.symbol()->get<DerivedTypeDetails>().add_paramDecl(result);
- }
return &result;
}
}
@@ -7830,6 +7833,12 @@ const parser::Name *DeclarationVisitor::ResolveName(const parser::Name &name) {
CheckEntryDummyUse(name.source, symbol);
ConvertToObjectEntity(*symbol);
ApplyImplicitRules(*symbol);
+ } else if (const auto *tpd{symbol->detailsIf<TypeParamDetails>()};
+ tpd && !tpd->attr()) {
+ Say(name,
+ "Type parameter '%s' was referenced before being declared"_err_en_US,
+ name.source);
+ context().SetError(*symbol);
}
if (checkIndexUseInOwnBounds_ &&
*checkIndexUseInOwnBounds_ == name.source && !InModuleFile()) {
diff --git a/flang/lib/Semantics/runtime-type-info.cpp b/flang/lib/Semantics/runtime-type-info.cpp
index 8939dc4499ec4..6690924196673 100644
--- a/flang/lib/Semantics/runtime-type-info.cpp
+++ b/flang/lib/Semantics/runtime-type-info.cpp
@@ -42,7 +42,8 @@ static int FindLenParameterIndex(
if (&*ref == &symbol) {
return lenIndex;
}
- if (ref->get<TypeParamDetails>().attr() == common::TypeParamAttr::Len) {
+ if (auto attr{ref->get<TypeParamDetails>().attr()};
+ attr && *attr == common::TypeParamAttr::Len) {
++lenIndex;
}
}
@@ -371,7 +372,7 @@ static std::optional<std::string> GetSuffixIfTypeKindParameters(
std::optional<std::string> suffix;
for (SymbolRef ref : *parameters) {
const auto &tpd{ref->get<TypeParamDetails>()};
- if (tpd.attr() == common::TypeParamAttr::Kind) {
+ if (tpd.attr() && *tpd.attr() == common::TypeParamAttr::Kind) {
if (const auto *pv{derivedTypeSpec.FindParameter(ref->name())}) {
if (pv->GetExplicit()) {
if (auto instantiatedValue{evaluate::ToInt64(*pv->GetExplicit())}) {
@@ -497,7 +498,7 @@ const Symbol *RuntimeTableBuilder::DescribeType(Scope &dtScope) {
for (SymbolRef ref : *parameters) {
if (const auto *inst{dtScope.FindComponent(ref->name())}) {
const auto &tpd{inst->get<TypeParamDetails>()};
- if (tpd.attr() == common::TypeParamAttr::Kind) {
+ if (tpd.attr() && *tpd.attr() == common::TypeParamAttr::Kind) {
auto value{evaluate::ToInt64(tpd.init()).value_or(0)};
if (derivedTypeSpec) {
if (const auto *pv{derivedTypeSpec->FindParameter(inst->name())}) {
@@ -799,7 +800,7 @@ evaluate::StructureConstructor RuntimeTableBuilder::DescribeComponent(
specParams{GetTypeParameters(spec.typeSymbol())}) {
for (SymbolRef ref : *specParams) {
const auto &tpd{ref->get<TypeParamDetails>()};
- if (tpd.attr() == common::TypeParamAttr::Len) {
+ if (tpd.attr() && *tpd.attr() == common::TypeParamAttr::Len) {
if (const ParamValue *
paramValue{spec.FindParameter(ref->name())}) {
lenParams.emplace_back(GetValue(*paramValue, parameters));
diff --git a/flang/lib/Semantics/symbol.cpp b/flang/lib/Semantics/symbol.cpp
index 023ab7b64e4fc..31e91ee7355e3 100644
--- a/flang/lib/Semantics/symbol.cpp
+++ b/flang/lib/Semantics/symbol.cpp
@@ -588,7 +588,11 @@ llvm::raw_ostream &operator<<(llvm::raw_ostream &os, const Details &details) {
},
[&](const TypeParamDetails &x) {
DumpOptional(os, "type", x.type());
- os << ' ' << common::EnumToString(x.attr());
+ if (auto attr{x.attr()}) {
+ os << ' ' << common::EnumToString(*attr);
+ } else {
+ os << " (no attr)";
+ }
DumpExpr(os, "init", x.init());
},
[&](const MiscDetails &x) {
@@ -739,9 +743,16 @@ const Symbol *DerivedTypeDetails::GetFinalForRank(int rank) const {
return nullptr;
}
-void TypeParamDetails::set_type(const DeclTypeSpec &type) {
+TypeParamDetails &TypeParamDetails::set_attr(common::TypeParamAttr attr) {
+ CHECK(!attr_);
+ attr_ = attr;
+ return *this;
+}
+
+TypeParamDetails &TypeParamDetails::set_type(const DeclTypeSpec &type) {
CHECK(!type_);
type_ = &type;
+ return *this;
}
bool GenericKind::IsIntrinsicOperator() const {
diff --git a/flang/lib/Semantics/tools.cpp b/flang/lib/Semantics/tools.cpp
index 99381918fc638..cce8dce9643fc 100644
--- a/flang/lib/Semantics/tools.cpp
+++ b/flang/lib/Semantics/tools.cpp
@@ -1135,12 +1135,12 @@ std::optional<parser::MessageFormattedText> CheckAccessibleSymbol(
return std::nullopt;
}
-std::list<SourceName> OrderParameterNames(const Symbol &typeSymbol) {
- std::list<SourceName> result;
+SymbolVector OrderParameterNames(const Symbol &typeSymbol) {
+ SymbolVector result;
if (const DerivedTypeSpec * spec{typeSymbol.GetParentTypeSpec()}) {
result = OrderParameterNames(spec->typeSymbol());
}
- const auto ¶mNames{typeSymbol.get<DerivedTypeDetails>().paramNames()};
+ const auto ¶mNames{typeSymbol.get<DerivedTypeDetails>().paramNameOrder()};
result.insert(result.end(), paramNames.begin(), paramNames.end());
return result;
}
@@ -1150,7 +1150,7 @@ SymbolVector OrderParameterDeclarations(const Symbol &typeSymbol) {
if (const DerivedTypeSpec * spec{typeSymbol.GetParentTypeSpec()}) {
result = OrderParameterDeclarations(spec->typeSymbol());
}
- const auto ¶mDecls{typeSymbol.get<DerivedTypeDetails>().paramDecls()};
+ const auto ¶mDecls{typeSymbol.get<DerivedTypeDetails>().paramDeclOrder()};
result.insert(result.end(), paramDecls.begin(), paramDecls.end());
return result;
}
diff --git a/flang/lib/Semantics/type.cpp b/flang/lib/Semantics/type.cpp
index ed2474377173f..cfaee0b8ba6dc 100644
--- a/flang/lib/Semantics/type.cpp
+++ b/flang/lib/Semantics/type.cpp
@@ -63,7 +63,6 @@ void DerivedTypeSpec::CookParameters(evaluate::FoldingContext &foldingContext) {
// Parameters of the most deeply nested "base class" come first when the
// derived type is an extension.
auto parameterNames{OrderParameterNames(typeSymbol_)};
- auto parameterDecls{OrderParameterDeclarations(typeSymbol_)};
auto nextNameIter{parameterNames.begin()};
RawParameters raw{std::move(rawParameters_)};
for (auto &[maybeKeyword, value] : raw) {
@@ -71,25 +70,25 @@ void DerivedTypeSpec::CookParameters(evaluate::FoldingContext &foldingContext) {
common::TypeParamAttr attr{common::TypeParamAttr::Kind};
if (maybeKeyword) {
name = maybeKeyword->v.source;
- auto it{std::find_if(parameterDecls.begin(), parameterDecls.end(),
+ auto it{std::find_if(parameterNames.begin(), parameterNames.end(),
[&](const Symbol &symbol) { return symbol.name() == name; })};
- if (it == parameterDecls.end()) {
+ if (it == parameterNames.end()) {
messages.Say(name,
"'%s' is not the name of a parameter for derived type '%s'"_err_en_US,
name, typeSymbol_.name());
} else {
// Resolve the keyword's symbol
maybeKeyword->v.symbol = const_cast<Symbol *>(&it->get());
- attr = it->get().get<TypeParamDetails>().attr();
+ if (const auto *tpd{it->get().detailsIf<TypeParamDetails>()}) {
+ attr = tpd->attr().value_or(attr);
+ }
}
} else if (nextNameIter != parameterNames.end()) {
- name = *nextNameIter++;
- auto it{std::find_if(parameterDecls.begin(), parameterDecls.end(),
- [&](const Symbol &symbol) { return symbol.name() == name; })};
- if (it == parameterDecls.end()) {
- break;
+ name = nextNameIter->get().name();
+ if (const auto *tpd{nextNameIter->get().detailsIf<TypeParamDetails>()}) {
+ attr = tpd->attr().value_or(attr);
}
- attr = it->get().get<TypeParamDetails>().attr();
+ ++nextNameIter;
} else {
messages.Say(name_,
"Too many type parameters given for derived type '%s'"_err_en_US,
@@ -160,7 +159,7 @@ void DerivedTypeSpec::EvaluateParameters(SemanticsContext &context) {
// Default type parameter value expressions are folded within
// the scope of the derived type being instantiated.
const TypeParamDetails &details{symbol.get<TypeParamDetails>()};
- if (details.init()) {
+ if (details.init() && details.attr()) {
evaluate::DynamicType dyType{TypeCategory::Integer, parameterKind};
if (auto converted{
evaluate::ConvertToType(dyType, SomeExpr{*details.init()})}) {
@@ -169,8 +168,8 @@ void DerivedTypeSpec::EvaluateParameters(SemanticsContext &context) {
evaluate::Fold(foldingContext, std::move(*converted))};
ok = ok || evaluate::IsActuallyConstant(folded);
AddParamValue(name,...
[truncated]
``````````
</details>
https://github.com/llvm/llvm-project/pull/99947
More information about the flang-commits
mailing list