Skip to content
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

Parsing and type-checking generics #671

Open
wants to merge 39 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
72adaf1
add support for parsing generic types and functions
koflin Apr 23, 2023
caea3b6
add support for parsing generic types and functions
koflin Apr 23, 2023
4f5555b
Merge branch 'generics' of https://github.com/koflin/gobra into generics
koflin Apr 24, 2023
a90ed72
modified parse tree translator
koflin May 2, 2023
2917a1a
changed typeArgument rule to index instead
koflin May 8, 2023
b1f7878
fix a few things such that compilation can succeed
koflin May 8, 2023
864e185
add some tests for generics
koflin May 15, 2023
f822f7f
started expression typing
koflin May 17, 2023
e8c0846
expression typing of generic function instantiation working
koflin May 24, 2023
e9c0878
implement parse tree translator for instantiated generic types
koflin May 29, 2023
6b10b39
implement typing of generic types
koflin May 30, 2023
7971b35
add first prototype of assignability
koflin May 31, 2023
1159896
implement assignability with type parameters
koflin Jun 5, 2023
ca0c310
changed type constraint to be an interface
koflin Jun 8, 2023
c0b00aa
handle expression typing of integer constants properly
koflin Jun 12, 2023
4253b7e
embed non interface type constraints into interfaces
koflin Jun 12, 2023
4139ed6
implemented satisfiability, comparability, identity, type merging and…
koflin Jun 15, 2023
4aa1b02
implement generic type instantiation with PIndexedExp
koflin Jun 19, 2023
04836b0
add fact test
koflin Jun 22, 2023
162b257
implement simple Go type inference
koflin Jun 22, 2023
b00f8fb
adjust GhostErasureUnitTest
koflin Jun 22, 2023
1ed090d
Trigger CI/CD
koflin Jun 22, 2023
1ed4471
[skip ci] remove lost line
koflin Jul 6, 2023
9e21e21
fix bug introduced in ghost assignability
koflin Jul 6, 2023
26e627c
fix printing of types
koflin Jul 6, 2023
30fe086
fix type inference
koflin Aug 24, 2023
ee2cf0a
Add type parameters to the desugar
koflin Aug 24, 2023
d9f6eed
added some tests from master
koflin Aug 24, 2023
3725a9b
remove change to be consistent with master
koflin Aug 24, 2023
d0fd09a
Revert "remove change to be consistent with master"
koflin Aug 24, 2023
91da1d2
clean up some stuff
koflin Aug 24, 2023
79dd96f
Merge branch 'generics-before-merge' into generics-merged
koflin Aug 24, 2023
a13e324
regenerate parser after merge
koflin Aug 24, 2023
ecfd59e
add dependency
koflin Aug 24, 2023
4bf94a5
adapted member typing unit test
koflin Aug 24, 2023
fc874a6
fix type parameter inference and refactor
koflin Aug 24, 2023
aa375f8
make parameterizable stateless
koflin Aug 25, 2023
f537564
unapplied type args
koflin Aug 25, 2023
b11a8d8
document type set, underlying type and rename syntaxImplements
koflin Aug 25, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 52 additions & 38 deletions src/main/antlr4/GoParser.g4
Original file line number Diff line number Diff line change
@@ -1,39 +1,33 @@
/*
[The "BSD licence"]
Copyright (c) 2017 Sasa Coh, Michał Błotniak
Copyright (c) 2019 Ivan Kochurkin, [email protected], Positive Technologies
Copyright (c) 2019 Dmitry Rassadin, [email protected], Positive Technologies
Copyright (c) 2021 Martin Mirchev, [email protected]
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. The name of the author may not be used to endorse or promote products
derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
[The "BSD licence"] Copyright (c) 2017 Sasa Coh, Michał Błotniak Copyright (c) 2019 Ivan Kochurkin,
[email protected], Positive Technologies Copyright (c) 2019 Dmitry Rassadin,
[email protected],Positive Technologies All rights reserved. Copyright (c) 2021 Martin Mirchev,
[email protected]

Redistribution and use in source and binary forms, with or without modification, are permitted
provided that the following conditions are met: 1. Redistributions of source code must retain the
above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in
binary form must reproduce the above copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials provided with the distribution. 3. The name
of the author may not be used to endorse or promote products derived from this software without
specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/

/*
* A Go grammar for ANTLR 4 derived from the Go Language Specification https://golang.org/ref/spec
*/

// Imported to Gobra from https://github.com/antlr/grammars-v4/tree/fae6a8500e9c6a1ec895fca1495b0384b9144091/golang
// Imported to Gobra from https://github.com/antlr/grammars-v4/blob/5493f3e2458d443f5a475359bf5e7cda87b25559/golang
// Extended with Generics

parser grammar GoParser;

Expand Down Expand Up @@ -68,11 +62,15 @@ expressionList: expression (COMMA expression)*;

typeDecl: TYPE (typeSpec | L_PAREN (typeSpec eos)* R_PAREN);

typeSpec: IDENTIFIER ASSIGN? type_;
typeSpec: aliasDecl | typeDef;

aliasDecl: IDENTIFIER ASSIGN type_;

typeDef: IDENTIFIER typeParameters? type_;

// Function declarations

functionDecl: FUNC IDENTIFIER (signature block?);
functionDecl: FUNC IDENTIFIER typeParameters? (signature block?);

methodDecl: FUNC receiver IDENTIFIER ( signature block?);

Expand Down Expand Up @@ -207,7 +205,7 @@ rangeClause: (

goStmt: GO expression;

type_: typeName | typeLit | L_PAREN type_ R_PAREN;
type_: typeName index? | typeLit | L_PAREN type_ R_PAREN;

typeName: qualifiedIdent | IDENTIFIER;

Expand All @@ -230,7 +228,13 @@ elementType: type_;
pointerType: STAR type_;

interfaceType:
INTERFACE L_CURLY ((methodSpec | typeName) eos)* R_CURLY;
INTERFACE L_CURLY (interfaceElem eos)* R_CURLY;

interfaceElem: methodSpec | typeElem;

typeElem: typeTerm (OR typeTerm)*;

typeTerm: type_;

sliceType: L_BRACKET R_BRACKET elementType;

Expand All @@ -256,6 +260,14 @@ parameters:

parameterDecl: identifierList? ELLIPSIS? type_;

typeParameters: L_BRACKET typeParamList COMMA? R_BRACKET;

typeParamList: typeParamDecl (COMMA typeParamDecl)*;

typeParamDecl: identifierList typeConstraint;

typeConstraint: typeElem;

expression:
primaryExpr
| unary_op = (
Expand Down Expand Up @@ -301,7 +313,9 @@ primaryExpr:
);


conversion: nonNamedType L_PAREN expression COMMA? R_PAREN;


conversion: type_ L_PAREN expression COMMA? R_PAREN;

nonNamedType: typeLit | L_PAREN nonNamedType R_PAREN;

Expand Down Expand Up @@ -335,7 +349,7 @@ literalType:
| L_BRACKET ELLIPSIS R_BRACKET elementType
| sliceType
| mapType
| typeName;
| typeName index?;

literalValue: L_CURLY (elementList COMMA?)? R_CURLY;

Expand All @@ -356,11 +370,11 @@ fieldDecl: (

string_: RAW_STRING_LIT | INTERPRETED_STRING_LIT;

embeddedField: STAR? typeName;
embeddedField: STAR? typeName index?;

functionLit: FUNC signature block; // function

index: L_BRACKET expression R_BRACKET;
index: L_BRACKET expression (COMMA expression)* COMMA? R_BRACKET;

slice_:
L_BRACKET (
Expand Down
10 changes: 6 additions & 4 deletions src/main/antlr4/GobraParser.g4
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ new_: NEW L_PAREN type_ R_PAREN;

specMember: specification (functionDecl[$specification.trusted, $specification.pure] | methodDecl[$specification.trusted, $specification.pure]);

functionDecl[boolean trusted, boolean pure]: FUNC IDENTIFIER (signature blockWithBodyParameterInfo?);
functionDecl[boolean trusted, boolean pure]: FUNC IDENTIFIER typeParameters? (signature blockWithBodyParameterInfo?);

methodDecl[boolean trusted, boolean pure]: FUNC receiver IDENTIFIER (signature blockWithBodyParameterInfo?);

Expand Down Expand Up @@ -393,7 +393,9 @@ predConstructArgs: L_PRED expressionList? COMMA? R_PRED;

// Added predicate spec and method specifications
interfaceType:
INTERFACE L_CURLY ((methodSpec | typeName| predicateSpec) eos)* R_CURLY;
INTERFACE L_CURLY (interfaceElem eos)* R_CURLY;

interfaceElem: methodSpec | typeElem | predicateSpec;

predicateSpec: PRED IDENTIFIER parameters;

Expand All @@ -402,7 +404,7 @@ methodSpec:
| GHOST? specification IDENTIFIER parameters;

// Added ghostTypeLiterals
type_: typeName | typeLit | ghostTypeLit | L_PAREN type_ R_PAREN;
type_: typeName index? | typeLit | ghostTypeLit | L_PAREN type_ R_PAREN;

// Added pred types
typeLit:
Expand All @@ -428,7 +430,7 @@ literalType:
| sliceType
| mapType
| ghostTypeLit
| typeName;
| typeName index?;

implicitArray: L_BRACKET ELLIPSIS R_BRACKET elementType;

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/viper/gobra/frontend/GobraLexer.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Generated from src/main/antlr4/GobraLexer.g4 by ANTLR 4.12.0
// Generated from S:/GitHub/gobra/src/main/antlr4\GobraLexer.g4 by ANTLR 4.12.0
package viper.gobra.frontend;
import org.antlr.v4.runtime.Lexer;
import org.antlr.v4.runtime.CharStream;
Expand Down
Loading
Loading