Skip to content

Commit

Permalink
WIP: Add some typedef support for expression type calculations
Browse files Browse the repository at this point in the history
TODO: need to check whether all changes are OK/acceptable, but might be
too much to permanently solve out known typedefs from expressions.
(not wrong, but maybe unnecessarily different from the original header)

Fixes: #3
  • Loading branch information
dkl committed Jan 6, 2021
1 parent d594c50 commit fe043a7
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 14 deletions.
2 changes: 2 additions & 0 deletions src/c-parser.bas
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,8 @@ destructor CParser()
end destructor

sub CParser.addTypedef(byval id as const zstring ptr)
'' Note: C scope rules not handled, we add/overwrite typedefs only and never remove them.
'' Usually this works well enough for bindings, since usually all typedefs are declared at the toplevel/global scope.
typedefs.addOverwrite(id, NULL)
end sub

Expand Down
20 changes: 18 additions & 2 deletions src/highlevel.bas
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,18 @@ function ExpressionFixUp.calculateCTypes(byval n as AstNode ptr) as integer
var ldtype = n->head->dtype
var rdtype = n->tail->dtype
n->setType(typeCBop(n->kind, ldtype, rdtype), NULL)

case ASTKIND_CAST
'' Resolve some typedefs
'' FIXME: This currently drops the information that this was a typedef,
'' so the generated code will be uglier than necessary.
if typeGetDtAndPtr(n->dtype) = TYPE_UDT then
if calculateCTypes(n->subtype) then
if typeGetDtAndPtr(n->subtype->dtype) <> TYPE_UDT then
n->setType(n->subtype->dtype, n->subtype->subtype)
end if
end if
end if
end select

function = allresolved
Expand All @@ -297,12 +309,16 @@ sub ExpressionFixUp.maybeCollectSymbol(byval n as AstNode ptr)
elseif n->expr then
collectSymbolIfExprTypeIsKnown(n->text, n->expr)
end if
case ASTKIND_TYPEDEF
'' Note: C scope rules not handled, we add/overwrite typedefs only and never remove them.
'' Usually this works well enough for bindings, since usually all typedefs are declared at the toplevel/global scope.
collectSymbol(n->text, n->dtype)
end select
end sub

''
'' Collect #defines/constants/enumconsts and determine their types so we can do
'' lookups and determine the type behind such identifiers used in expressions.
'' Collect #defines/constants/enumconsts/typedefs and determine their types,
'' so we can do lookups and determine the type behind such identifiers used in expressions.
''
'' We walk from top to bottom. If a #define constant forward-references another
'' one defined later, we have to add it to a list for processing later in
Expand Down
4 changes: 2 additions & 2 deletions tests/c/expr.bi
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ enum
A = cshort(0)
A = clng(0)
A = clngint(0)
A = cast(MYINT, 0)
A = clng(0)
A = 1
A = &o1
A = &h1
Expand Down Expand Up @@ -429,7 +429,7 @@ const A38 = cbyte(0)
const A39 = cshort(0)
const A40 = clng(0)
const A41 = clngint(0)
const A42 = cast(MYINT, 0)
const A42 = clng(0)
#macro A43
scope
a(0)
Expand Down
2 changes: 1 addition & 1 deletion tests/highlevel/cast-addrof-strlit.bi
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

type myStr as byte ptr
#define A cptr(zstring ptr, @"foo")
#define B cast(myStr, @"foo")
#define B cptr(byte ptr, @"foo")
#define C cptr(long ptr, @"123")
#define D cptr(zstring ptr, @"foo")
#define E cptr(zstring ptr, @"a" "b")
12 changes: 6 additions & 6 deletions tests/highlevel/macro-param-name-conflicts.bi
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@

type HWND as long
type HICON as long
#define m0(hwnd_) f(cast(HWND, hwnd_))
#define m0(hwnd) f(clng(hwnd))
#define m10(and_) (and_ and and_)
#define m11(cast_) cast(HWND, cast_)
#define m11(cast) clng(cast)
#define m12(cptr_) cptr(long ptr, cptr_)
#define m13(integer_) cptr(integer ptr, integer_)
#define m14(_) _
#define m15(AND) (AND and AND)
#define m16(casT) cast(HWND, casT)
#define m16(casT) clng(casT)
#define m20(screen) f(screen)
#define m21(val) f(val)
#define m22(len) f(len)
#define m23(string) f(string)
#define m30(cast, Cast) (cast(HWND, cast) + Cast)
#define m30(cast, Cast) (clng(cast) + Cast)
#define m31(a, A) f(a, A)
#define m32(hwnd, Hwnd) Hwnd(cast(HWND, hwnd))
#define m33(hicon_, Hicon_manually_renamed) Hicon_manually_renamed(cast(HICON, hicon_))
#define m32(hwnd, Hwnd) Hwnd(clng(hwnd))
#define m33(hicon, Hicon_manually_renamed) Hicon_manually_renamed(clng(hicon))
6 changes: 3 additions & 3 deletions tests/highlevel/math-casts.bi
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ const B30 = culng(0u - 100u)
type UINT as ulong
const C00_MAX_UINT32 = cuint(culng(not 0u))
const C01_MAX_UINT32 = cuint(culng(not culng(0)))
const C02_MAX_UINT32 = cuint(not cast(UINT, 0))
const C02_MAX_UINT32 = cuint(culng(not culng(0)))
const C03_MAX_UINTPTR = cuint(not 0)
dim shared C04(0 to cuint(not cast(UINT, 0)) - 1) as uinteger = {cuint(not cast(UINT, 0))}
declare sub C05(byval param as uinteger = cuint(not cast(UINT, 0)))
dim shared C04(0 to cuint(culng(not culng(0))) - 1) as uinteger = {cuint(culng(not culng(0)))}
declare sub C05(byval param as uinteger = cuint(culng(not culng(0))))

end extern

0 comments on commit fe043a7

Please sign in to comment.