@@ -81,18 +81,19 @@ class TypeAndShape {
8181 bool operator !=(const TypeAndShape &that) const { return !(*this == that); }
8282
8383 static std::optional<TypeAndShape> Characterize (
84- const semantics::Symbol &, FoldingContext &, bool invariantOnly = false );
84+ const semantics::Symbol &, FoldingContext &, bool invariantOnly = true );
8585 static std::optional<TypeAndShape> Characterize (
8686 const semantics::DeclTypeSpec &, FoldingContext &,
87- bool invariantOnly = false );
87+ bool invariantOnly = true );
8888 static std::optional<TypeAndShape> Characterize (
89- const ActualArgument &, FoldingContext &, bool invariantOnly = false );
89+ const ActualArgument &, FoldingContext &, bool invariantOnly = true );
9090
9191 // General case for Expr<T>, &c.
9292 template <typename A>
9393 static std::optional<TypeAndShape> Characterize (
94- const A &x, FoldingContext &context, bool invariantOnly = false ) {
95- if (const auto *symbol{UnwrapWholeSymbolDataRef (x)}) {
94+ const A &x, FoldingContext &context, bool invariantOnly = true ) {
95+ const auto *symbol{UnwrapWholeSymbolOrComponentDataRef (x)};
96+ if (symbol && !symbol->owner ().IsDerivedType ()) { // Whole variable
9697 if (auto result{Characterize (*symbol, context, invariantOnly)}) {
9798 return result;
9899 }
@@ -106,6 +107,9 @@ class TypeAndShape {
106107 }
107108 }
108109 }
110+ if (symbol) { // component
111+ result.AcquireAttrs (*symbol);
112+ }
109113 return std::move (result.Rewrite (context));
110114 }
111115 return std::nullopt ;
@@ -116,15 +120,21 @@ class TypeAndShape {
116120 static std::optional<TypeAndShape> Characterize (
117121 const Designator<Type<TypeCategory::Character, KIND>> &x,
118122 FoldingContext &context, bool invariantOnly = true ) {
119- if (const auto *symbol{UnwrapWholeSymbolDataRef (x)}) {
123+ const auto *symbol{UnwrapWholeSymbolOrComponentDataRef (x)};
124+ if (symbol && !symbol->owner ().IsDerivedType ()) { // Whole variable
120125 if (auto result{Characterize (*symbol, context, invariantOnly)}) {
121126 return result;
122127 }
123128 }
124129 if (auto type{x.GetType ()}) {
125130 TypeAndShape result{*type, GetShape (context, x, invariantOnly)};
126- if (auto length{x.LEN ()}) {
127- result.set_LEN (std::move (*length));
131+ if (type->category () == TypeCategory::Character) {
132+ if (auto length{x.LEN ()}) {
133+ result.set_LEN (std::move (*length));
134+ }
135+ }
136+ if (symbol) { // component
137+ result.AcquireAttrs (*symbol);
128138 }
129139 return std::move (result.Rewrite (context));
130140 }
@@ -133,7 +143,7 @@ class TypeAndShape {
133143
134144 template <typename A>
135145 static std::optional<TypeAndShape> Characterize (const std::optional<A> &x,
136- FoldingContext &context, bool invariantOnly = false ) {
146+ FoldingContext &context, bool invariantOnly = true ) {
137147 if (x) {
138148 return Characterize (*x, context, invariantOnly);
139149 } else {
@@ -142,7 +152,7 @@ class TypeAndShape {
142152 }
143153 template <typename A>
144154 static std::optional<TypeAndShape> Characterize (
145- A *ptr, FoldingContext &context, bool invariantOnly = false ) {
155+ A *ptr, FoldingContext &context, bool invariantOnly = true ) {
146156 if (ptr) {
147157 return Characterize (std::as_const (*ptr), context, invariantOnly);
148158 } else {
0 commit comments