Number of elements in ArrayType?

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

Number of elements in ArrayType?

Kristof Beyls via cfe-dev
Hi!

How can I get the number of elements in ArrayType?


/k
_______________________________________________
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: Number of elements in ArrayType?

Kristof Beyls via cfe-dev
It depends on the type of the ArrayType.  ArrayType is the base class of ConstantArrayType, DependentSizedArrayType, IncompleteArrayType, and VariableArrayType.

You have to dyn_cast to whichever the array is.  Note that only ConstantArrayType has a constant size (with its function getSize(), or getSizeExpr for the expression itself).

DependentSizedArrayType will show up in a template, if the size itself is a template parameter (you can get THAT with getSizeExpr()).

VariableArrayType represents a VLA (variable length array), so it will also have a getSizeExpr() that isn't calculatable at compile time.

IncompleteArrayType does not have a size.

-----Original Message-----
From: cfe-dev <[hidden email]> On Behalf Of Kenth Eriksson via cfe-dev
Sent: Tuesday, October 29, 2019 6:59 AM
To: [hidden email]
Subject: [cfe-dev] Number of elements in ArrayType?

Hi!

How can I get the number of elements in ArrayType?


/k
_______________________________________________
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: Number of elements in ArrayType?

Kristof Beyls via cfe-dev
In reply to this post by Kristof Beyls via cfe-dev
Assume that you have the desired `VarDecl` `D`:
```
       const Expr *SizeExpr = nullptr;
        if (D->getType()->isArrayType())
          if (const TypeSourceInfo *TSI = D->getTypeSourceInfo())
            if (const auto ATL = TSI->getTypeLoc().getAs<ArrayTypeLoc>())
              SizeExpr = ATL.getSizeExpr();
```

On Tue, Oct 29, 2019 at 2:58 PM Kenth Eriksson via cfe-dev
<[hidden email]> wrote:

>
> Hi!
>
> How can I get the number of elements in ArrayType?
>
>
> /k
> _______________________________________________
> 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: Number of elements in ArrayType?

Kristof Beyls via cfe-dev
On Tue, 2019-10-29 at 15:03 +0100, Csaba Dabis wrote:

> CAUTION: This email originated from outside of the organization. Do not click links or open attachments unless you recognize the sender and know the content is safe.
>
>
> Assume that you have the desired `VarDecl` `D`:
> ```
>        const Expr *SizeExpr = nullptr;
>         if (D->getType()->isArrayType())
>           if (const TypeSourceInfo *TSI = D->getTypeSourceInfo())
>             if (const auto ATL = TSI->getTypeLoc().getAs<ArrayTypeLoc>())
>               SizeExpr = ATL.getSizeExpr();
> ```
>
I'm having a MemberExpr not a VarDecl. I thought it was thru TypeInfo I
could get the number of elements in the array?

_______________________________________________
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: Number of elements in ArrayType?

Kristof Beyls via cfe-dev
In reply to this post by Kristof Beyls via cfe-dev
On Tue, 2019-10-29 at 15:03 +0100, Csaba Dabis wrote:

>
> Assume that you have the desired `VarDecl` `D`:
> ```
>        const Expr *SizeExpr = nullptr;
>         if (D->getType()->isArrayType())
>           if (const TypeSourceInfo *TSI = D->getTypeSourceInfo())
>             if (const auto ATL = TSI->getTypeLoc().getAs<ArrayTypeLoc>())
>               SizeExpr = ATL.getSizeExpr();
> ```
>
Looks as it can be done like this (arr is ConstantArrayType)

arr->getSize().getZExtValue()

Is my understanding correct?
_______________________________________________
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: Number of elements in ArrayType?

Kristof Beyls via cfe-dev
Erich has pointed out the hierarchy of array types [1], where a
`ConstantArrayType` has such a method `getSize()` to obtain the
concrete size directly. However, most of the time you are able to
obtain the `VariableArrayType`'s size, being a `DeclRefExpr` to a
constant integer. My solution obtains the size expression and let you
decide what you want to do and how generic your solution needs to be.
I have not worked with `DependentSizedArrayType` yet.

[1] https://clang.llvm.org/doxygen/classclang_1_1ArrayType.html

On Tue, Oct 29, 2019 at 7:20 PM Kenth Eriksson
<[hidden email]> wrote:

>
> On Tue, 2019-10-29 at 15:03 +0100, Csaba Dabis wrote:
> >
> > Assume that you have the desired `VarDecl` `D`:
> > ```
> >        const Expr *SizeExpr = nullptr;
> >         if (D->getType()->isArrayType())
> >           if (const TypeSourceInfo *TSI = D->getTypeSourceInfo())
> >             if (const auto ATL = TSI->getTypeLoc().getAs<ArrayTypeLoc>())
> >               SizeExpr = ATL.getSizeExpr();
> > ```
> >
> Looks as it can be done like this (arr is ConstantArrayType)
>
> arr->getSize().getZExtValue()
>
> Is my understanding correct?
_______________________________________________
cfe-dev mailing list
[hidden email]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev