~ubuntu-branches/ubuntu/trusty/monodevelop/trusty-proposed

« back to all changes in this revision

Viewing changes to contrib/ICSharpCode.Decompiler/Ast/Transforms/PatternStatementTransform.cs

  • Committer: Package Import Robot
  • Author(s): Jo Shields
  • Date: 2013-05-12 09:46:03 UTC
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20130512094603-mad323bzcxvmcam0
Tags: upstream-4.0.5+dfsg
ImportĀ upstreamĀ versionĀ 4.0.5+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
716
716
                                        return null;
717
717
                        }
718
718
                        FieldReference cachedDictField = m.Get<AstNode>("cachedDict").Single().Annotation<FieldReference>();
719
 
                        if (cachedDictField == null || !cachedDictField.DeclaringType.Name.StartsWith("<PrivateImplementationDetails>", StringComparison.Ordinal))
 
719
                        if (cachedDictField == null)
720
720
                                return null;
721
721
                        List<Statement> dictCreation = m.Get<BlockStatement>("dictCreation").Single().Statements.ToList();
722
722
                        List<KeyValuePair<string, int>> dict = BuildDictionary(dictCreation);
765
765
                
766
766
                List<KeyValuePair<string, int>> BuildDictionary(List<Statement> dictCreation)
767
767
                {
 
768
                        if (context.Settings.ObjectOrCollectionInitializers && dictCreation.Count == 1)
 
769
                                return BuildDictionaryFromInitializer(dictCreation[0]);
 
770
 
 
771
                        return BuildDictionaryFromAddMethodCalls(dictCreation);
 
772
                }
 
773
 
 
774
                static readonly Statement assignInitializedDictionary = new ExpressionStatement {
 
775
                        Expression = new AssignmentExpression {
 
776
                                Left = new AnyNode().ToExpression(),
 
777
                                Right = new ObjectCreateExpression {
 
778
                                        Type = new AnyNode(),
 
779
                                        Arguments = { new Repeat(new AnyNode()) },
 
780
                                        Initializer = new ArrayInitializerExpression {
 
781
                                                Elements = { new Repeat(new AnyNode("dictJumpTable")) }
 
782
                                        }
 
783
                                },
 
784
                        },
 
785
                };
 
786
 
 
787
                private List<KeyValuePair<string, int>> BuildDictionaryFromInitializer(Statement statement)
 
788
                {
 
789
                        List<KeyValuePair<string, int>> dict = new List<KeyValuePair<string, int>>();
 
790
                        Match m = assignInitializedDictionary.Match(statement);
 
791
                        if (!m.Success)
 
792
                                return dict;
 
793
 
 
794
                        foreach (ArrayInitializerExpression initializer in m.Get<ArrayInitializerExpression>("dictJumpTable")) {
 
795
                                KeyValuePair<string, int> pair;
 
796
                                if (TryGetPairFrom(initializer.Elements, out pair))
 
797
                                        dict.Add(pair);
 
798
                        }
 
799
 
 
800
                        return dict;
 
801
                }
 
802
 
 
803
                private static List<KeyValuePair<string, int>> BuildDictionaryFromAddMethodCalls(List<Statement> dictCreation)
 
804
                {
768
805
                        List<KeyValuePair<string, int>> dict = new List<KeyValuePair<string, int>>();
769
806
                        for (int i = 0; i < dictCreation.Count; i++) {
770
807
                                ExpressionStatement es = dictCreation[i] as ExpressionStatement;
773
810
                                InvocationExpression ie = es.Expression as InvocationExpression;
774
811
                                if (ie == null)
775
812
                                        continue;
776
 
                                PrimitiveExpression arg1 = ie.Arguments.ElementAtOrDefault(0) as PrimitiveExpression;
777
 
                                PrimitiveExpression arg2 = ie.Arguments.ElementAtOrDefault(1) as PrimitiveExpression;
778
 
                                if (arg1 != null && arg2 != null && arg1.Value is string && arg2.Value is int)
779
 
                                        dict.Add(new KeyValuePair<string, int>((string)arg1.Value, (int)arg2.Value));
 
813
 
 
814
                                KeyValuePair<string, int> pair;
 
815
                                if (TryGetPairFrom(ie.Arguments, out pair))
 
816
                                        dict.Add(pair);
780
817
                        }
781
818
                        return dict;
782
819
                }
 
820
 
 
821
                private static bool TryGetPairFrom(AstNodeCollection<Expression> expressions, out KeyValuePair<string, int> pair)
 
822
                {
 
823
                        PrimitiveExpression arg1 = expressions.ElementAtOrDefault(0) as PrimitiveExpression;
 
824
                        PrimitiveExpression arg2 = expressions.ElementAtOrDefault(1) as PrimitiveExpression;
 
825
                        if (arg1 != null && arg2 != null && arg1.Value is string && arg2.Value is int) {
 
826
                                pair = new KeyValuePair<string, int>((string)arg1.Value, (int)arg2.Value);
 
827
                                return true;
 
828
                        }
 
829
 
 
830
                        pair = default(KeyValuePair<string, int>);
 
831
                        return false;
 
832
                }
 
833
 
783
834
                #endregion
784
835
                
785
836
                #region Automatic Properties