Finding Method Attributes from a MethodInvocationExpression

View: New views
4 Messages — Rating Filter:   Alert me  

Finding Method Attributes from a MethodInvocationExpression

by Jason DeFontes-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


I'm trying to write a pipeline step such that certain
MethodInvocationExpressions are only allowed to call methods which are
decorated with a particular attribute. For example:

// safelibrary.boo
[Safe]
def SafeMethod():
  pass

// clientcode.boo
def UnsafeMethod():
  pass

SafeMethod() // This is ok
UnsafeMethod() // My pipeline step will generate a compiler error here


I have a step that finds the method invocations I'm interested in
checking, but I don't know:

1) how find the Attributes associated with the Target of the
MethodInvocationExpression
2) what pipeline step mine needs to run after so that the type
information I want is available

Thanks,

-Jason
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Boo Programming Language" group.
To post to this group, send email to boolang@...
To unsubscribe from this group, send email to boolang-unsubscribe@...
For more options, visit this group at http://groups.google.com/group/boolang
-~----------~----~----~----~------~----~------~--~---


Re: Finding Method Attributes from a MethodInvocationExpression

by Rodrigo B. de Oliveira :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


On Sat, Jun 28, 2008 at 11:56 AM, Jason DeFontes <jason@...> wrote:
> ...
> I have a step that finds the method invocations I'm interested in
> checking, but I don't know:
>
> 1) how find the Attributes associated with the Target of the
> MethodInvocationExpression

This is different depending if the method is an ExternalMethod (a
method defined in an referenced assembly) or an InternalMethod (a
method being compiled). See sample at the end.

> 2) what pipeline step mine needs to run after so that the type
> information I want is available
>

Theoretically, all type information should be available after
ProcessMethodBodies but some normalizations still occur after it so
it's generally easier to rely on the state of the ast just before
EmitAssembly.

    namespace boospikes

    import Boo.Lang.Compiler
    import Boo.Lang.Compiler.Steps
    import Boo.Lang.Compiler.Ast
    import Boo.Lang.Compiler.TypeSystem

    class SafeAttribute(System.Attribute):
        pass

    [safe] def foo():
        pass

    def bar():
        pass

    class DisallowUnsafeCalls(AbstractVisitorCompilerStep):
        override def Run():
            Visit(CompileUnit)

        override def OnMethodInvocationExpression(node as
MethodInvocationExpression):
            entity = GetEntity(node.Target)
            return if entity isa IConstructor // assume all ctors are safe
            return if IsSafeMethod(entity)

            Errors.Add(
                CompilerErrorFactory.CustomError(
                    node.LexicalInfo,
                    "Unsafe method call to '${entity}'"))

        def IsSafeMethod(entity as IMethod):
            return IsAttributeDefined(entity, SafeAttribute)

        def IsAttributeDefined(entity as IMethod, attributeType as System.Type):
            internalMethod = entity as InternalMethod
            if internalMethod is not null:
                return
MetadataUtil.IsAttributeDefined(internalMethod.Method,
TypeSystemServices.Map(attributeType))

            externalMethod = entity as ExternalMethod
            return externalMethod.MethodInfo.IsDefined(attributeType, true)


    code = """
    import boospikes

    [safe] def baz():
        pass

    def gazong():
        pass

    foo()
    bar()
    baz()
    gazong()
    """

    compiler = BooCompiler()
    compiler.Parameters.Pipeline = Pipelines.CompileToMemory()
    compiler.Parameters.Pipeline.InsertBefore(EmitAssembly,
DisallowUnsafeCalls())
    compiler.Parameters.References.Add(System.Reflection.Assembly.GetExecutingAssembly())
    compiler.Parameters.Input.Add(IO.StringInput("code.boo", code))
    result = compiler.Run()
    print result.Errors

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Boo Programming Language" group.
To post to this group, send email to boolang@...
To unsubscribe from this group, send email to boolang-unsubscribe@...
For more options, visit this group at http://groups.google.com/group/boolang
-~----------~----~----~----~------~----~------~--~---


Re: Finding Method Attributes from a MethodInvocationExpression

by Rodrigo B. de Oliveira :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


On Sat, Jun 28, 2008 at 12:58 PM, Rodrigo B. de Oliveira
<rodrigobamboo@...> wrote:

> On Sat, Jun 28, 2008 at 11:56 AM, Jason DeFontes <jason@...> wrote:
>> ...
>> I have a step that finds the method invocations I'm interested in
>> checking, but I don't know:
>>
>> 1) how find the Attributes associated with the Target of the
>> MethodInvocationExpression
>
> This is different depending if the method is an ExternalMethod (a
> method defined in an referenced assembly) or an InternalMethod (a
> method being compiled). See sample at the end.
>

After looking at the code it was clear that this should be solved with
polymorphism. So if you get the latest code from svn, IsSafeMethod can
be written in a simpler way:

    def IsSafeMethod(method as IMethod):
        return method.IsDefined(TypeSystemServices.Map(SafeAttribute))

IsDefined is also available for types, properties, fields and events
and should work with generics.

Best wishes,
Rodrigo

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Boo Programming Language" group.
To post to this group, send email to boolang@...
To unsubscribe from this group, send email to boolang-unsubscribe@...
For more options, visit this group at http://groups.google.com/group/boolang
-~----------~----~----~----~------~----~------~--~---


Re: Finding Method Attributes from a MethodInvocationExpression

by Jason DeFontes-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


Thanks Rodrigo! That's a great help. I'll give it a try in the
morning.

-Jason
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Boo Programming Language" group.
To post to this group, send email to boolang@...
To unsubscribe from this group, send email to boolang-unsubscribe@...
For more options, visit this group at http://groups.google.com/group/boolang
-~----------~----~----~----~------~----~------~--~---

LightInTheBox - Buy quality products at wholesale price