metatype of Int


(Jon Rafkind) #1

Given a swift::MetaType how can I tell if it corresponds to the MetaType of the standard Int type? Is there a way to get a reference to the MetaType from the StdlibModule or TheBuiltinModule?

For now I can convert the MetaType to a string and compare it to "Int.Type" but that is yucky.

···

--


(Dave Abrahams) #2

Given a swift::MetaType how can I tell if it corresponds to the MetaType of the standard Int type? Is there a way to get a reference to the MetaType from the StdlibModule or TheBuiltinModule?

For now I can convert the MetaType to a string and compare it to "Int.Type" but that is yucky.

Equality comparison works:

(swift) String.self == Int.self
// r2 : Bool = false
(swift) Int.self == Int.self
// r3 : Bool = true

--
_______________________________________________
swift-dev mailing list
swift-dev@swift.org
https://lists.swift.org/mailman/listinfo/swift-dev

-Dave

···

On Dec 18, 2015, at 11:16 AM, Rafkind, Jon via swift-dev <swift-dev@swift.org> wrote:


(Jon Rafkind) #3

Sorry I should have been more specific. I am working at the C++ level. I have a swift::MetaType* and want to compare it to something.

···

On 12/18/2015 12:10 PM, Dave Abrahams wrote:

On Dec 18, 2015, at 11:16 AM, Rafkind, Jon via swift-dev <swift-dev@swift.org><mailto:swift-dev@swift.org> wrote:

Given a swift::MetaType how can I tell if it corresponds to the MetaType of the standard Int type? Is there a way to get a reference to the MetaType from the StdlibModule or TheBuiltinModule?

For now I can convert the MetaType to a string and compare it to "Int.Type" but that is yucky.

Equality comparison works:

(swift) String.self == Int.self
// r2 : Bool = false
(swift) Int.self == Int.self
// r3 : Bool = true

--
_______________________________________________
swift-dev mailing list
swift-dev@swift.org<mailto:swift-dev@swift.org>
https://lists.swift.org/mailman/listinfo/swift-dev

-Dave

--


(Slava Pestov) #4

You can probably get the mangled name of the metatype, and refer to that. Eg,

public func f() -> Any.Type {
  return Int.self
}

f()

Compile this with -emit-ir:

@_TMSi = external global %swift.type, align 8

define %swift.type* @_TF4blah1fFT_PMP_() #0 {
entry:
  ret %swift.type* @_TMSi
}

So in your C++ program,

extern "C" {
  extern void *_TMSi;
}

Note that this is completely unsupported, and probably insane. You have been warned.

Slava

···

On Dec 18, 2015, at 12:14 PM, Rafkind, Jon via swift-dev <swift-dev@swift.org> wrote:

Sorry I should have been more specific. I am working at the C++ level. I have a swift::MetaType* and want to compare it to something.

On 12/18/2015 12:10 PM, Dave Abrahams wrote:

On Dec 18, 2015, at 11:16 AM, Rafkind, Jon via swift-dev <swift-dev@swift.org><mailto:swift-dev@swift.org> wrote:

Given a swift::MetaType how can I tell if it corresponds to the MetaType of the standard Int type? Is there a way to get a reference to the MetaType from the StdlibModule or TheBuiltinModule?

For now I can convert the MetaType to a string and compare it to "Int.Type" but that is yucky.

Equality comparison works:

(swift) String.self == Int.self
// r2 : Bool = false
(swift) Int.self == Int.self
// r3 : Bool = true

--
_______________________________________________
swift-dev mailing list
swift-dev@swift.org<mailto:swift-dev@swift.org>
https://lists.swift.org/mailman/listinfo/swift-dev

-Dave

--
_______________________________________________
swift-dev mailing list
swift-dev@swift.org
https://lists.swift.org/mailman/listinfo/swift-dev


(Jordan Rose) #5

I think Jon's trying to build something on top of swiftc or SourceKit. Yes?

You can do something like this to build your own metatype:

ASTContext &ctx = …
Type intTy = ctx.getIntDecl()->getDeclaredType();
auto *intMetaTy = MetatypeType::get(intTy, ctx);

And then compare types using TypeBase::isEqual (which looks through typealiases and such—it's comparing "canonical" types).

Type unknownTy = …
if (unknownTy->isEqual(intMetaTy)) {
  // celebrate
}

Hope that helps,
Jordan

···

On Dec 18, 2015, at 13:47 , Slava Pestov via swift-dev <swift-dev@swift.org> wrote:

You can probably get the mangled name of the metatype, and refer to that. Eg,

public func f() -> Any.Type {
return Int.self
}

f()

Compile this with -emit-ir:

@_TMSi = external global %swift.type, align 8

define %swift.type* @_TF4blah1fFT_PMP_() #0 {
entry:
ret %swift.type* @_TMSi
}

So in your C++ program,

extern "C" {
extern void *_TMSi;
}

Note that this is completely unsupported, and probably insane. You have been warned.

Slava

On Dec 18, 2015, at 12:14 PM, Rafkind, Jon via swift-dev <swift-dev@swift.org> wrote:

Sorry I should have been more specific. I am working at the C++ level. I have a swift::MetaType* and want to compare it to something.

On 12/18/2015 12:10 PM, Dave Abrahams wrote:

On Dec 18, 2015, at 11:16 AM, Rafkind, Jon via swift-dev <swift-dev@swift.org><mailto:swift-dev@swift.org> wrote:

Given a swift::MetaType how can I tell if it corresponds to the MetaType of the standard Int type? Is there a way to get a reference to the MetaType from the StdlibModule or TheBuiltinModule?

For now I can convert the MetaType to a string and compare it to "Int.Type" but that is yucky.

Equality comparison works:

(swift) String.self == Int.self
// r2 : Bool = false
(swift) Int.self == Int.self
// r3 : Bool = true

--
_______________________________________________
swift-dev mailing list
swift-dev@swift.org<mailto:swift-dev@swift.org>
https://lists.swift.org/mailman/listinfo/swift-dev

-Dave

--
_______________________________________________
swift-dev mailing list
swift-dev@swift.org
https://lists.swift.org/mailman/listinfo/swift-dev

_______________________________________________
swift-dev mailing list
swift-dev@swift.org
https://lists.swift.org/mailman/listinfo/swift-dev


(Jon Rafkind) #6

Yes, context.getIntDecl() was exactly what I was looking for. Thank you Jordan!

···

On 12/18/2015 08:49 PM, Jordan Rose wrote:
I think Jon's trying to build something on top of swiftc or SourceKit. Yes?

You can do something like this to build your own metatype:

ASTContext &ctx = …
Type intTy = ctx.getIntDecl()->getDeclaredType();
auto *intMetaTy = MetatypeType::get(intTy, ctx);

And then compare types using TypeBase::isEqual (which looks through typealiases and such—it's comparing "canonical" types).

Type unknownTy = …
if (unknownTy->isEqual(intMetaTy)) {
  // celebrate
}

Hope that helps,
Jordan

On Dec 18, 2015, at 13:47 , Slava Pestov via swift-dev <swift-dev@swift.org<mailto:swift-dev@swift.org>> wrote:

You can probably get the mangled name of the metatype, and refer to that. Eg,

public func f() -> Any.Type {
return Int.self
}

f()

Compile this with -emit-ir:

@_TMSi = external global %swift.type, align 8

define %swift.type* @_TF4blah1fFT_PMP_() #0 {
entry:
ret %swift.type* @_TMSi
}

So in your C++ program,

extern "C" {
extern void *_TMSi;
}

Note that this is completely unsupported, and probably insane. You have been warned.

Slava

On Dec 18, 2015, at 12:14 PM, Rafkind, Jon via swift-dev <<mailto:swift-dev@swift.org>swift-dev@swift.org<mailto:swift-dev@swift.org>> wrote:

Sorry I should have been more specific. I am working at the C++ level. I have a swift::MetaType* and want to compare it to something.

On 12/18/2015 12:10 PM, Dave Abrahams wrote:

On Dec 18, 2015, at 11:16 AM, Rafkind, Jon via swift-dev <swift-dev@swift.org<mailto:swift-dev@swift.org>><<mailto:swift-dev@swift.org>mailto:swift-dev@swift.org> wrote:

Given a swift::MetaType how can I tell if it corresponds to the MetaType of the standard Int type? Is there a way to get a reference to the MetaType from the StdlibModule or TheBuiltinModule?

For now I can convert the MetaType to a string and compare it to "Int.Type" but that is yucky.

Equality comparison works:

(swift) String.self == Int.self
// r2 : Bool = false
(swift) Int.self == Int.self
// r3 : Bool = true

--
_______________________________________________
swift-dev mailing list
swift-dev@swift.org<mailto:swift-dev@swift.org><<mailto:swift-dev@swift.org>mailto:swift-dev@swift.org>
https://lists.swift.org/mailman/listinfo/swift-dev

-Dave

--
_______________________________________________
swift-dev mailing list
swift-dev@swift.org<mailto:swift-dev@swift.org>
https://lists.swift.org/mailman/listinfo/swift-dev

_______________________________________________
swift-dev mailing list
swift-dev@swift.org<mailto:swift-dev@swift.org>
https://lists.swift.org/mailman/listinfo/swift-dev

--