Just An Application

October 29, 2014

So Swift Then: Mangled Function Names And Function Types — Function Types Go Recursive

Now we have a model of how function types are encoded lets see if we can break it.

What happens if we use it on a function which returns a function.

Starting with the function

    func bass() -> ()
    {
    }

Now we have a function to return like so

    func cod() -> () -> ()
    {
        return bass
    }

Compiling cod gives us the symbol

    __TF4xper3codFT_FT_T_

giving us

    FT_FT_T_

as the function type.

Now for the model.

Starting by creating some useful objects.

    let encoder     = Encoder()
    let builder     = FunctionTypeBuilder()
    let ftVoidVoid  = builder.build()

then

    let ft6000 = builder.returnType(ftVoidVoid).build()

    println(ft6000.encode(encoder))

prints

    FT_FT_T_

We’re off to flying start.

This is a function which returns a function which returns a function

    func dab() -> () -> () -> ()
    {
        return cod
    }

and compiling it gives us the symbol

    __TF4xper3dabFT_FT_FT_T_

giving us

    FT_FT_FT_T_

as the function type.

This

    let ft6001 = builder.returnType(ft6000).build()
    
    println(ft6001.encode(encoder))

prints

    FT_FT_FT_T_

so that’s alright.

A function that takes an Int and a function as arguments

    func eel(x: Int, f: (Int) -> (Int)) -> Int
    {
        return f(x)
    }

Compiling it gives us the symbol

    __TF4xper3eelFTSiFSiSi_Si

giving us

    FTSiFSiSi_Si

as the function type.

    let ftIntInt = builder.intParam().intReturnType().build()
    let ft6002   = builder.intParam().param(ftIntInt).intReturnType().build()
    
    println(ft6002.encode(encoder))

prints

    FTSiFSiSi_Si

A function that takes a function that takes two Ints and returns a pair or Ints

    func flounder(f: (Int, Int) -> (Int, Int))
    {
    }

Compiling it gives us the symbol

    __TF4xper8flounderFFTSiSi_TSiSi_T_

giving us

    FFTSiSi_TSiSi_T_

as the function type.

    let intType  = BuiltinType.IntType
    let fnType   = builder.tupleTypeParam(intType, intType).tupleReturnType(intType, intType).build()
    let ft6003   = builder.param(fnType).build()
    
    println(ft6003.encode(encoder))

prints

    FFTSiSi_TSiSi_T_

which of course it would !

A function that takes a function which returns a function as its argument and returns the result of calling that function

    func goby(f: () -> () -> ()) -> () -> ()
    {
        return f()
    }

Pick the parentheses out of that.

The compiler having picked the parentheses out comes up with the symbol

    __TF4xper4gobyFFT_FT_T_FT_T_

which gives us the function type

    FFT_FT_T_FT_T_

and we’ll just have to trust that it knows what its doing.

    let ft6004   = builder.param(ft6000).returnType(ftVoidVoid).build()
    
    println(ft6004.encode(encoder))

prints

    FFT_FT_T_FT_T_

so if the compiler knows what its doing so does the model, and possibly vice versa.

And that’s enough of that without a dedicated test harness that can count ‘F’s and ‘T’s and stuff.

It certainly looks as though the model’s behaviour matches that of the compiler when it comes to function types as arguments and return types.


Copyright (c) 2014 By Simon Lewis. All Rights Reserved.

Unauthorized use and/or duplication of this material without express and written permission from this blog’s author and owner Simon Lewis is strictly prohibited.

Excerpts and links may be used, provided that full and clear credit is given to Simon Lewis and justanapplication.wordpress.com with appropriate and specific direction to the original content.

Advertisements

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: