<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
<meta name="Generator" content="Microsoft Word 15 (filtered medium)">
<style><!--
/* Font Definitions */
@font-face
        {font-family:"Cambria Math";
        panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
        {font-family:Calibri;
        panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
        {font-family:Verdana;
        panose-1:2 11 6 4 3 5 4 4 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0in;
        font-size:12.0pt;
        font-family:"Calibri",sans-serif;}
a:link, span.MsoHyperlink
        {mso-style-priority:99;
        color:#0563C1;
        text-decoration:underline;}
span.EmailStyle19
        {mso-style-type:personal-reply;
        font-family:"Calibri",sans-serif;
        color:windowtext;}
.MsoChpDefault
        {mso-style-type:export-only;
        font-size:10.0pt;}
@page WordSection1
        {size:8.5in 11.0in;
        margin:1.0in 1.0in 1.0in 1.0in;}
div.WordSection1
        {page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]-->
</head>
<body lang="EN-US" link="#0563C1" vlink="#954F72" style="word-wrap:break-word">
<div class="WordSection1">
<p class="MsoNormal"><span style="font-size:11.0pt">Hi Craig,<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt">I think you may be the first having to deal with optional derived type arguments in intrinsic functions. First, I would like to clarify where you are having an issue with
</span><span style="font-size:11.0pt">IntrinsicTypeDefaultKinds::GetDefaultKind being called with a derived type category after your first three changes.<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt">By “TypeAndShape constructor calls GetDefaultKind()”, you mean that GetDefaultKind is called in the arguments of a TypeAndShape constructor, not inside the TypeAndShape constructor function itself right ?<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt">Is this at: </span><span style="font-size:11.0pt"><a href="https://github.com/llvm/llvm-project/blob/d8805574c183484f055552855fa82d2e8932415e/flang/lib/Evaluate/intrinsics.cpp#L1666">https://github.com/llvm/llvm-project/blob/d8805574c183484f055552855fa82d2e8932415e/flang/lib/Evaluate/intrinsics.cpp#L1666</a>
 ?</span><span style="font-size:11.0pt"><o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt"><o:p> </o:p></span></p>
<p class="MsoNormal" style="margin-left:.5in"><span style="font-size:11.0pt">} else {<o:p></o:p></span></p>
<p class="MsoNormal" style="margin-left:.5in"><span style="font-size:11.0pt">      // optional argument is absent<o:p></o:p></span></p>
<p class="MsoNormal" style="margin-left:.5in"><span style="font-size:11.0pt">      CHECK(d.optionality != Optionality::required);<o:p></o:p></span></p>
<p class="MsoNormal" style="margin-left:.5in"><span style="font-size:11.0pt">      if (d.typePattern.kindCode == KindCode::same) {<o:p></o:p></span></p>
<p class="MsoNormal" style="margin-left:.5in"><span style="font-size:11.0pt">        dummyArgs.emplace_back(dummyArgs[sameDummyArg.value()]);<o:p></o:p></span></p>
<p class="MsoNormal" style="margin-left:.5in"><span style="font-size:11.0pt">      } else {<o:p></o:p></span></p>
<p class="MsoNormal" style="margin-left:.5in"><span style="font-size:11.0pt">        auto category{d.typePattern.categorySet.LeastElement().value()};<o:p></o:p></span></p>
<p class="MsoNormal" style="margin-left:.5in"><span style="font-size:11.0pt">        characteristics::TypeAndShape typeAndShape{<o:p></o:p></span></p>
<p class="MsoNormal" style="margin-left:.5in"><span style="font-size:11.0pt">            DynamicType{category<b>, defaults.GetDefaultKind(category)}};</b><o:p></o:p></span></p>
<p class="MsoNormal" style="margin-left:.5in"><span style="font-size:11.0pt">        dummyArgs.emplace_back(std::string{d.keyword},<o:p></o:p></span></p>
<p class="MsoNormal" style="margin-left:.5in"><span style="font-size:11.0pt">            characteristics::DummyDataObject{std::move(typeAndShape)});<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt">Looking at the code you propose, the overall idea looks good to me, the issue is that DynamicType cannot be created from the simply the TypeCategory for derived type. A semantics::DerivedTypeSpec that precisely
 described the derived type specification of a derived type entity needs to be provided (DynamicType must be different for objects of different derived types, there cannot be a single DynamicType for all derived types).<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt">So going that way, I think you would need a semantics::DerivedTypeSpec for the team type. I am not sure there currently is a way to generate such DerivedTypeSpec without a user object (which I think would
 be your case, since there is no actual argument to get the type from). That is because TEAM_TYPE, like all derived types defined by the standard, are defined by means of Fortran source files (see module/iso_fortran_env.f90 and module/__fortran_builtins.f90),
 so the compiler has no knowledge of what these derived types actually are without access to these source/module files (For user objects, these module files are made accessible by the user via use statements to iso_fortran_env).<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt">Jean<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt"><o:p> </o:p></span></p>
<div>
<div style="border:none;border-top:solid #E1E1E1 1.0pt;padding:3.0pt 0in 0in 0in">
<p class="MsoNormal"><b><span style="font-size:11.0pt">From:</span></b><span style="font-size:11.0pt"> flang-dev <flang-dev-bounces@lists.llvm.org>
<b>On Behalf Of </b>Rasmussen, Craig E. via flang-dev<br>
<b>Sent:</b> Monday, May 3, 2021 10:39 PM<br>
<b>To:</b> flang-dev@lists.llvm.org<br>
<b>Subject:</b> [flang-dev] Intrinsic function with an optional TEAM_TYPE dummy argument<o:p></o:p></span></p>
</div>
</div>
<p class="MsoNormal"><o:p> </o:p></p>
<table class="MsoNormalTable" border="1" cellspacing="5" cellpadding="0" style="background:#FFEB9C">
<tbody>
<tr>
<td style="padding:.75pt .75pt .75pt .75pt">
<p class="MsoNormal"><b><span style="font-size:7.5pt;font-family:"Verdana",sans-serif;color:black">External email: Use caution opening links or attachments</span></b><span style="font-size:7.5pt;font-family:"Verdana",sans-serif;color:black">
</span><span style="font-size:11.0pt"><o:p></o:p></span></p>
</td>
</tr>
</tbody>
</table>
<p class="MsoNormal"><span style="font-size:11.0pt"><o:p> </o:p></span></p>
<div>
<p class="MsoNormal"><span style="font-size:11.0pt">I'm trying to implement team_number(team) in lib/Evaluate/intrinsics.cpp and I'm running into a problem.  The issue is how to handle team_type from the intrinsic module iso_fortran_env.  I'll outline the steps
 I've taken so far and then describe the problem I'm having.<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt">I’ve:<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt">  1. Changed the CategorySet in TypePattern TEAM_TYPE from IntType to DerivedType. I'm pretty sure this is the correct thing to do.<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt">  2. Added team_number to the IntrinsicInterface list with an optional TEAM_TYPE. Again, no problem here.<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt">  3. Implemented checking types and kinds of the actual argument against the team_number's interface. This is pretty straight forward:<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt">         case KindCode::teamType:<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt">           argOk = semantics::IsTeamType(GetDerivedTypeSpec(type));<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt">The problem occurs when the code tries to check the dummy argument (team) for the case in which the optional actual argument is missing. The normal control flow path that this would take would be to move a
 characteristics::DummyDataObject (constructed with characteristics::TypeAndShape) onto the dummyArgs list. However, the TypeAndShape constructor calls GetDefaultKind() with a TEAM_TYPE category argument (which is TypeCategory::DerivedType).<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt">Therein lies the problem. The function IntrinsicTypeDefaultKinds::GetDefaultKind(TypeCategory::DerivedType) fails with a hard error because a derived type is not an intrinsic type. At first I was thoroughly
 confused by the whole type kind thing for a derived type, specifically one from an intrinsic module. Then as a last (of course) resort, I checked the standard more carefully and found Note 16.34 which states that types in intrinsic modules are not themselves
 intrinsic. Thus IntrinsicTypeDefaultKinds::GetDefaultKind(TypeCategory::DerivedType) should fail.<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt">So the conclusion is that I need help fixing this (for function team_type() and several other intrinsic functions with a dummy argument of TEAM_TYPE). The code could look something like the following (at roughly
 line 1670 in intrinsics.cpp):<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt">      } else if (d.typePattern.kindCode == KindCode::teamType) {<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt">        characteristics::TypeAndShape typeAndShape{DynamicType{TypeCategory::Derived}};<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt">        dummyArgs.emplace_back(std::string{d.keyword},<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt">            characteristics::DummyDataObject{std::move(typeAndShape)});<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt">      }<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt">However, there is no constructor for TypeAndShape with a single argument of type TypeCategory::Derived. After staring at many lines of code in flang I'm left wondering if there shouldn't be?<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt">Thanks,<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt">Craig<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt"><o:p> </o:p></span></p>
</div>
</div>
</body>
</html>