Array Size Information of the Outermost Dimension in ParamValDecl in AST

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Array Size Information of the Outermost Dimension in ParamValDecl in AST

Nathan Ridge via cfe-dev
Dear all,

    Hi! I am trying to extracting the size of array from ParamValDecl in AST but I notice the following situation:
    The type of an array “int A[50][100]” in the function interface in source code will turn out to be "int (*)[100]" in AST, which means that I lose the information of the outermost dimension, i.e. the number "50".
    I wonder how I can get the size of the outermost dimension of the array with Clang AST visitor, except processing the string of source code or implementing a specific parser.
    THANKS a lot in advance for your time and patience!

Best regards,
------------------------------------------
Tingyuan LIANG
MPhil Student
Department of Electronic and Computer Engineering
The Hong Kong University of Science and Technology

_______________________________________________
cfe-dev mailing list
[hidden email]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: Array Size Information of the Outermost Dimension in ParamValDecl in AST

Nathan Ridge via cfe-dev
The AST actually does retain the information you are looking for. I introduced the DecayedType pointer type so that we could retain it. It's difficult to see with the AST dumper, but if you manually traverse the nodes and handle DecayedType, you will find what you are looking for.

Consider this program:
int f(int A[50][100]) {
  typedef decltype(A) P;
  P o;
  return A[0][0];
}

Dumping it shows how the decayed type appears inside decltype:
    | `-TypedefDecl 0x1cb1da4b4c8 <col:3, col:23> col:23 referenced P 'decltype(A)':'int (*)[100]'
    |   `-DecltypeType 0x1cb1da4b490 'decltype(A)' sugar
    |     |-DeclRefExpr 0x1cb1da4b470 <col:20> 'int (*)[100]':'int (*)[100]' lvalue ParmVar 0x1cb1da4b280 'A' 'int (*)[100]':'int (*)[100]' non_odr_use_unevaluated
    |     `-DecayedType 0x1cb1d816580 'int (*)[100]' sugar
    |       |-ConstantArrayType 0x1cb1d8164e0 'int [50][100]' 50
    |       | `-ConstantArrayType 0x1cb1d816480 'int [100]' 100
    |       |   `-BuiltinType 0x1cb1d8156d0 'int'
    |       `-PointerType 0x1cb1d816550 'int (*)[100]'
    |         `-ConstantArrayType 0x1cb1d816480 'int [100]' 100
    |           `-BuiltinType 0x1cb1d8156d0 'int'

Hopefully that helps.

On Mon, Jul 1, 2019 at 2:22 AM Tingyuan LIANG via cfe-dev <[hidden email]> wrote:
Dear all,

    Hi! I am trying to extracting the size of array from ParamValDecl in AST but I notice the following situation:
    The type of an array “int A[50][100]” in the function interface in source code will turn out to be "int (*)[100]" in AST, which means that I lose the information of the outermost dimension, i.e. the number "50".
    I wonder how I can get the size of the outermost dimension of the array with Clang AST visitor, except processing the string of source code or implementing a specific parser.
    THANKS a lot in advance for your time and patience!

Best regards,
------------------------------------------
Tingyuan LIANG
MPhil Student
Department of Electronic and Computer Engineering
The Hong Kong University of Science and Technology
_______________________________________________
cfe-dev mailing list
[hidden email]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev

_______________________________________________
cfe-dev mailing list
[hidden email]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
Reply | Threaded
Open this post in threaded view
|

Re: Array Size Information of the Outermost Dimension in ParamValDecl in AST

Nathan Ridge via cfe-dev
Dear Reid,

    THANKS a lot for your detailed explanation!
    According to your suggestion, I use the function getOriginalType() and successfully get complete information for the array!
    Thanks !!!^_^

Best regards,
------------------------------------------
Tingyuan LIANG
MPhil Student
Department of Electronic and Computer Engineering
The Hong Kong University of Science and Technology

From: Reid Kleckner <[hidden email]>
Sent: Tuesday, July 2, 2019 4:30 AM
To: Tingyuan LIANG
Cc: Clang Dev
Subject: Re: [cfe-dev] Array Size Information of the Outermost Dimension in ParamValDecl in AST
 
The AST actually does retain the information you are looking for. I introduced the DecayedType pointer type so that we could retain it. It's difficult to see with the AST dumper, but if you manually traverse the nodes and handle DecayedType, you will find what you are looking for.

Consider this program:
int f(int A[50][100]) {
  typedef decltype(A) P;
  P o;
  return A[0][0];
}

Dumping it shows how the decayed type appears inside decltype:
    | `-TypedefDecl 0x1cb1da4b4c8 <col:3, col:23> col:23 referenced P 'decltype(A)':'int (*)[100]'
    |   `-DecltypeType 0x1cb1da4b490 'decltype(A)' sugar
    |     |-DeclRefExpr 0x1cb1da4b470 <col:20> 'int (*)[100]':'int (*)[100]' lvalue ParmVar 0x1cb1da4b280 'A' 'int (*)[100]':'int (*)[100]' non_odr_use_unevaluated
    |     `-DecayedType 0x1cb1d816580 'int (*)[100]' sugar
    |       |-ConstantArrayType 0x1cb1d8164e0 'int [50][100]' 50
    |       | `-ConstantArrayType 0x1cb1d816480 'int [100]' 100
    |       |   `-BuiltinType 0x1cb1d8156d0 'int'
    |       `-PointerType 0x1cb1d816550 'int (*)[100]'
    |         `-ConstantArrayType 0x1cb1d816480 'int [100]' 100
    |           `-BuiltinType 0x1cb1d8156d0 'int'

Hopefully that helps.

On Mon, Jul 1, 2019 at 2:22 AM Tingyuan LIANG via cfe-dev <[hidden email]> wrote:
Dear all,

    Hi! I am trying to extracting the size of array from ParamValDecl in AST but I notice the following situation:
    The type of an array “int A[50][100]” in the function interface in source code will turn out to be "int (*)[100]" in AST, which means that I lose the information of the outermost dimension, i.e. the number "50".
    I wonder how I can get the size of the outermost dimension of the array with Clang AST visitor, except processing the string of source code or implementing a specific parser.
    THANKS a lot in advance for your time and patience!

Best regards,
------------------------------------------
Tingyuan LIANG
MPhil Student
Department of Electronic and Computer Engineering
The Hong Kong University of Science and Technology
_______________________________________________
cfe-dev mailing list
[hidden email]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev

_______________________________________________
cfe-dev mailing list
[hidden email]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev