Skip to content

[FIX] Add support for multi-level pointers #93

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed

Conversation

filipsajdak
Copy link
Contributor

@filipsajdak filipsajdak commented Oct 24, 2022

Current implementation supports only pointer of the form

p: *int = ...;

This change extends that to allow multi-level pointers and make below code compilable

main: (argc : int, argv : **char) -> int = {
    a:     int = 2;
    pa:   *int = a&;
    ppa: **int = pa&;

    return a*pa**ppa**; // 8
}

and generates the cpp1 code

// ----- Cpp2 support -----
#include "cpp2util.h"


#line 1 "tests/pointer-to-pointer.cpp2"
[[nodiscard]] auto main(cpp2::in<int> argc, cpp2::in<char * *> argv) -> int;

//=== Cpp2 definitions ==========================================================

#line 1 "tests/pointer-to-pointer.cpp2"
[[nodiscard]] auto main(cpp2::in<int> argc, cpp2::in<char * *> argv) -> int{
    int a { 2 }; 
    int* pa { &a }; 
    int** ppa { &pa }; 

    return a * *pa * **ppa; // 8
}

Closes #78 and makes possible to implement correct main function with arguments.

Open issues

This change still does not support deduced pointer types (just like the current implementation). That means:

pa = 0;

will cause an error as cppfront knows that pa is a pointer to pointer

tests/pointer-to-pointer.cpp2...
pointer-to-pointer.cpp2(6,8): error: = - pointer assignment from null or integer is illegal
  ==> program violates lifetime safety guarantee - see previous errors

but this code:

pa2 := ppa*;
pa2 = 0;

pa3 := a&;
pa3 = 0;

will pass lifetime safety guarantee checks and will compile to cpp1 code:

auto pa2 { *ppa }; 
pa2 = 0;

auto pa3 { &a }; 
pa3 = 0;

Having multi-level pointer makes that issue more important to be fixed as it is easier to deduce a pointer from pointer to pointer.

@filipsajdak filipsajdak changed the title [FIX] Add support of multi-level pointers [FIX] Add support for multi-level pointers Oct 24, 2022
@filipsajdak filipsajdak force-pushed the fsajdak-fix-pointer-to-pointer branch from e5c77bb to 9fe1be7 Compare October 24, 2022 15:51
@filipsajdak
Copy link
Contributor Author

On the second thought, I am also not using index so nevertheless the range-for is better

for (auto _ : n.pointer_declarators) {
    printer.print_cpp2("*", n.position());
}

I was thinking of using std::ignore but it is not valid in this context. I pushed the corrected version.

@filipsajdak
Copy link
Contributor Author

filipsajdak commented Oct 24, 2022

I just spend couple of minutes meditating @hsutter comment (that I have modified for this change)

//  TODO: Generalize this -- for now we detect only multi-level cases of the form "p: ***int = ...;"
//        We don't recognize pointer types that are deduced or from Cpp1

and I did some tests that makes me add Open issues to the description of this PR as this needs to be clear that this PR introduce support for multi-level pointers but does not address other issues. One of them: deduced pointers becomes more an issue after this PR as it will be easier to create a deduced pointer from pointer to pointer.

@filipsajdak
Copy link
Contributor Author

Handling of pointer deduced types are done here: #94

@filipsajdak
Copy link
Contributor Author

Pointers from cpp1 are handled here #96

@filipsajdak
Copy link
Contributor Author

Add support for pointers as template arguments

s : std::span<*char> = (argv, gsl::narrow_cast<std::size_t>(argc)); // works

@filipsajdak filipsajdak force-pushed the fsajdak-fix-pointer-to-pointer branch 2 times, most recently from 7e5c596 to be63722 Compare November 1, 2022 23:53
@filipsajdak filipsajdak force-pushed the fsajdak-fix-pointer-to-pointer branch from be63722 to 05a40f9 Compare November 7, 2022 23:35
@filipsajdak filipsajdak force-pushed the fsajdak-fix-pointer-to-pointer branch 2 times, most recently from f6bfe6e to bd15d7c Compare November 29, 2022 22:26
@filipsajdak filipsajdak force-pushed the fsajdak-fix-pointer-to-pointer branch from bd15d7c to dd1cdd0 Compare December 3, 2022 21:13
@filipsajdak filipsajdak force-pushed the fsajdak-fix-pointer-to-pointer branch from dd1cdd0 to 92e4add Compare December 6, 2022 22:18
@hsutter
Copy link
Owner

hsutter commented Dec 26, 2022

@filipsajdak Would you be interested in updating the good/current parts of #93, #94, and #96 and merging them into a new PR to replace these?

@filipsajdak
Copy link
Contributor Author

Yes. I will.

@filipsajdak
Copy link
Contributor Author

Replaced by #196

@filipsajdak filipsajdak closed this Jan 3, 2023
@filipsajdak filipsajdak deleted the fsajdak-fix-pointer-to-pointer branch January 12, 2023 21:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[BUG] Add support for defining pointer to pointers.
4 participants