<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=utf-8">
<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;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{margin:0in;
font-size:11.0pt;
font-family:"Calibri",sans-serif;}
a:link, span.MsoHyperlink
{mso-style-priority:99;
color:blue;
text-decoration:underline;}
span.EmailStyle18
{mso-style-type:personal-reply;
font-family:"Calibri",sans-serif;
color:windowtext;}
.MsoChpDefault
{mso-style-type:export-only;
font-family:"Calibri",sans-serif;}
@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="blue" vlink="purple">
<div class="WordSection1">
<p class="MsoNormal">The reason it’s represented this way is to preserve the source as written. Suppose you have something like the following:<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">typedef int a[10];<o:p></o:p></p>
<p class="MsoNormal">typedef const a b;<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">We want to preserve the TypedefType, so the const has to stay outside it.<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">Because of this, you usually want to use ASTContext:: getAsArrayType(), not dyn_cast<ArrayType>(MyQualType). The ASTContext method will rewrite the element type so it has the right qualifiers.<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">-Eli<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<div style="border:none;border-top:solid #E1E1E1 1.0pt;padding:3.0pt 0in 0in 0in">
<p class="MsoNormal"><b>From:</b> cfe-dev <cfe-dev-bounces@lists.llvm.org> <b>On Behalf Of
</b>Ray Zhang via cfe-dev<br>
<b>Sent:</b> Friday, October 9, 2020 1:23 AM<br>
<b>To:</b> Clang Dev <cfe-dev@lists.llvm.org><br>
<b>Subject:</b> [EXT] [cfe-dev] Why qualifiers for array elements on the array itself?<o:p></o:p></p>
</div>
<p class="MsoNormal"><o:p> </o:p></p>
<div>
<div>
<p class="MsoNormal">Hi,<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">I'm currently working on a clang cast tool and ran into a small oddity. This might be a stupid mistake but I'd love some pointers(no pun intended) or insight into it:<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">const int arr[2] {1,2};<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">should be "an array of fixed size 2 of constant integers". The QualType associated with arr is an array type, and we can observe that with Expr->isArrayType(). I checked the qualifiers and found that the qualifier for const is actually
on this level, and not for the individual elements of ints. I found the elements inside of the array with the following:<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">const ArrayType* ArrType = dyn_cast<ArrayType>(MyQualType);<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal">ArrType->getElementType();<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">which I was hoping could give me the const qualifier for the integers. If it helps at all, I'm using the getLocalFastQualifiers() function to return an unsigned int, and I'm seeing if 0x1 is masked for constness.<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">Since arrays can't really have qualifiers(the only example I see is on the<a href="https://en.cppreference.com/w/c/language/array"> standards</a> for function parameters for C99 and not C++), was there a reason for why clang decided to
put the qualifiers on the array qualtype rather than the elements' qualtype?<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">If I made any unreasonable assumptions to reach this conclusion, please let me know :)<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">Best,<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal">Ray<o:p></o:p></p>
</div>
</div>
</div>
</body>
</html>