[swift-evolution] [Draft Proposal] Refactor SILParser::parseSILInstruction

Joe Groff jgroff at apple.com
Wed Jan 13 15:11:52 CST 2016


Sounds great, but SIL is an implementation detail of the compiler and not formally part of the language spec. swift-dev would be a better venue for this discussion.

-Joe

> On Jan 13, 2016, at 1:07 PM, Sergey Bolshedvorsky via swift-evolution <swift-evolution at swift.org> wrote:
> 
> Hi everyone,
> 
> I’m interested in taking SR-340 <https://bugs.swift.org/browse/SR-340> issue:
> "parseSILInstruction is horrible and makes me cry every time I see it. It is a method that is ~1900 lines with a huge switch in it. We should refactor it into a visitor structure. In fact it is large enough that we should consider moving it into its own file if it is possible."
> 
> Here is the first draft of the myl idea, how it could be approached:
> 
> 1. We will define an abstract method on ValueBase class:
> 
>     class ValueBase
>     {
>     public:
>         virtual void parse(class SILParseInstruction*) = 0;
>     };
>     
> 
> 2. Each of the SIL instruction classes will override this method and will provide the following implementation:
>     
>     class SILArgument: public ValueBase
>     {
>     public:
>         /*virtual*/void parse(SILParseInstruction*);
>     };
>     void SILArgument::parse(SILParseInstruction *i) {
>         i->parseInstruction(this);
>     }
>     
>     class PartialApplyInst: public ValueBase
>     {
>     public:
>         /*virtual*/void parse(SILParseInstruction*);
>     };
>     void PartialApplyInst::parse(SILParseInstruction *i)
>     {
>         i->parseInstruction(this);
>     }
>     
> 
> 3. We will define an abstract class for the callbacks from each of the SIL instruction classes
> 
>     class SILParseInstruction
>     {
>     public:
>         virtual void parseInstruction(SILArgument*) = 0;
>         virtual void parseInstruction(PartialApplyInst*) = 0;
>     };
> 
> 4. SILParser will implement these callbacks with the actual handling operations for each instruction. 
>     
>     class SILParser: public SILParseInstruction
>     {
>     public:
>         /*virtual*/void parseInstruction(SILArgument *r)
>         {
>             // Parse StringLiteralInst instruction
>         }
>         /*virtual*/void parseInstruction(PartialApplyInst *b)
>         {
>             // Parse PartialApplyInst instruction
>         }
>     };
>     
> 5. The huge switch statement will be replaced with a single call:
> 	Opcode->parse(this);
> 
> What are your thoughts?
> 
> Sergey
> 
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160113/8b3c54ab/attachment.html>


More information about the swift-evolution mailing list