<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/98637>98637</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            [Clang] AST has inconsistent ImplicitCast expression type for `ParenType` based on qualifiers
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            clang
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          Jezurko
      </td>
    </tr>
</table>

<pre>
    ### Description
I have discovered an inconsistency in `ImplicitCastExpr` that happens when `ParenType` is involved.
Consider the following program:
```
int main() {
   int (a) = 0;
   a + 0;
}
```
Now when you dump the AST for the binary expression, you get:
```
    `-BinaryOperator 0x55790e9b6fb8 <line:3:5, col:9> 'int' '+'
      |-ImplicitCastExpr 0x55790e9b6fa0 <col:5> 'int' <LValueToRValue>
      | `-DeclRefExpr 0x55790e9b6f60 <col:5> 'int' lvalue Var 0x55790e9b6ec0 'a' 'int'
      `-IntegerLiteral 0x55790e9b6f80 <col:9> 'int' 0

```
Which seems fine. But actually dumping the result type of the `ImplicitCast` we get this:
```
ParenType 0x55556b68fd10 'int' sugar
`-BuiltinType 0x55556b
```

Now if we modify the program by adding `const` to the integer variable:
```
int main() {
   const int (a) = 0;
   a + 0;
}
```
The AST for the binary expression looks almost the same:
```
`-BinaryOperator 0x557c6784ffb8 <line:3:5, col:9> 'int' '+'
      |-ImplicitCastExpr 0x557c6784ffa0 <col:5> 'int' <LValueToRValue>
      | `-DeclRefExpr 0x557c6784ff60 <col:5> 'const int' lvalue Var 0x557c6784fec0 'a' 'const int'
      `-IntegerLiteral 0x557c6784ff80 <col:9> 'int' 0
```
But dumping the result type of the `Implicit cast yields an unexpected result (at least in my opinion):
```
BuiltinType 0x55556b62e0e0 'int'
```
### Cause
When I looked into why this happens, I discovered that in `clang/lib/Sema/SemaExpr.cpp` in the `ExprResult Sema::DefaultLvalueConversion(Expr *E)` function the qualifiers get stripped. But stripping the qualifiers also removes the `ParenType` from the type:
![lldb screenshot showing that stripping qualifiers removes the `ParenType`](https://github.com/user-attachments/assets/060b1b92-a533-4fd3-a72c-2e1f16df151d)
### Expected behaviour
I would expect these to behave the same in relation to the `ParenType`, so that either both cases keep it, or both remove it.
Personally, I would prefer the solution that it gets stripped by the cast, as it would make my life easier, but I am not sure if that's the correct way.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJy0Vt9v4zYM_muUFyKBLMd28pCH_GiBDoftcFfcnmmbjrXKkifJabO_fpCctGmW3g7DDgjiRKZIit_3kULn5F4TrVi2YdlugoNvjV39Qn8N9slMSlMfV0yk4wd25Corey-NZnzH-PoBWjwQ1NJV5kCWakANUldGO-k86eoIUgPL-UPXK1lJv0Xn7156y3IOvkUPLfY9aQfPLUXDz2hJPx57ChbSgdQHow5Uz8aA2-C5Jgu-JWiMUuZZ6j301uwtdixdj2Ys56dP_Cu1hw6lZmLBxBJYsRnXASC8YmKBcT3dAWfp20sEJjYXS6zY3fT_q3keD3A0A9RD18f01l8foTFjqqXUaI9AL70l50L9xDZa78l_lDUAhIpMN3Hvbz1Z9MYCf8myYslpWeZNuQCWbpXUxNJ1ytJ1FvxWRrF0vWTpHTBRSO2ZKMIvJjbh-9U5ACu202to3gVAHgKMDrMrh-n20zdUAz2aL_HJ0rsr3zH9HVXqCzX_cJ1_7Fodgj_4hu92UMWDDZ5OM9q-i5jz6YP2tCf7SXqyqN4FXFwEvCrOqeI3Yfi9lVULjqhz0EhNM9gMHrDyAyp1jHAHCgaULblBefDHnsA0cemK-4HVzxRQB99K9xH0ryqIB8iyvMwXTZ3wi5TdsEf7unm6GaTy8v2em67fGCubkEpnatkcY64nFUF5BKzrcCiW86DlmLY30UiOBYYDWomlov-guejy_1He47_pDJQxTw5Qdcb5aOKw-zDpj-RW5cVi3vxEuZ0C_Ay5nVzfktsrELdEN-67Et3ljh-R3in4D0jvPRBBYj-uLKjQeThKUrULE2j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