aboutsummaryrefslogtreecommitdiffstats
path: root/QtVsTools.Package/QML/Parser/QmlParserInterop.cs
blob: 9052f4012625a7eb8ffe48bb1d3ee664ce822104 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
/****************************************************************************
**
** Copyright (C) 2018 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the Qt VS Tools.
**
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

/// This file contains the integration with the Qt Declarative parser.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;

namespace QtVsTools.Qml
{
    using Syntax;

    /// <summary>
    /// Implements the integration with the Qt declarative parser, including:
    ///   * Managed-unmanaged interop with the vsqml DLL;
    ///   * Unmarshaling of syntax element data types (e.g. AST nodes);
    ///   * Visitor role for AST traversal.
    /// </summary>
    class Parser : IDisposable
    {
        internal static class NativeMethods
        {
            [DllImport("vsqml",
                CallingConvention = CallingConvention.Cdecl,
                EntryPoint = "qmlGetTokens")]
            internal static extern bool GetTokens(
                IntPtr qmlText,
                int qmlTextLength,
                ref IntPtr tokens,
                ref int tokensLength);

            [DllImport("vsqml",
                CallingConvention = CallingConvention.Cdecl,
                EntryPoint = "qmlFreeTokens")]
            internal static extern bool FreeTokens(IntPtr tokens);

            [DllImport("vsqml",
                CallingConvention = CallingConvention.Cdecl,
                EntryPoint = "qmlParse")]
            internal static extern bool Parse(
                IntPtr qmlText,
                int qmlTextLength,
                ref IntPtr parser,
                ref bool parsedCorrectly,
                ref IntPtr diagnosticMessages,
                ref int diagnosticMessagesLength,
                ref IntPtr comments,
                ref int commentsLength);

            [DllImport("vsqml",
                CallingConvention = CallingConvention.Cdecl,
                EntryPoint = "qmlFreeParser")]
            internal static extern bool FreeParser(IntPtr parser);

            [DllImport("vsqml",
                CallingConvention = CallingConvention.Cdecl,
                EntryPoint = "qmlFreeDiagnosticMessages")]
            internal static extern bool FreeDiagnosticMessages(IntPtr diagnosticMessages);

            [DllImport("vsqml",
                CallingConvention = CallingConvention.Cdecl,
                EntryPoint = "qmlFreeComments")]
            internal static extern bool FreeComments(IntPtr comments);

            [DllImport("vsqml",
                CallingConvention = CallingConvention.Cdecl,
                EntryPoint = "qmlGetAstVisitor")]
            internal static extern IntPtr GetAstVisitor();

            [DllImport("vsqml",
                CallingConvention = CallingConvention.Cdecl,
                EntryPoint = "qmlFreeAstVisitor")]
            internal static extern bool FreeAstVisitor(IntPtr astVisitor);

            internal delegate bool Callback(
                IntPtr astVisitor,
                int nodeKind,
                IntPtr node,
                bool beginVisit,
                IntPtr nodeData,
                int nodeDataLength);

            [DllImport("vsqml",
                CallingConvention = CallingConvention.Cdecl,
                EntryPoint = "qmlSetAstVisitorCallback")]
            internal static extern bool SetAstVisitorCallback(
                IntPtr astVisitor,
                int nodeKindFilter,
                Callback visitCallback);

            [DllImport("vsqml",
                CallingConvention = CallingConvention.Cdecl,
                EntryPoint = "qmlAcceptAstVisitor")]
            internal static extern bool AcceptAstVisitor(
                IntPtr parser,
                IntPtr node,
                IntPtr astVisitor);
        }

        /// <summary>
        /// List of "interesting" AST node types. To optimize the managed-unmanaged interop,
        /// during AST traversal, only these node types will be reported.
        /// </summary>
        static readonly List<AstNodeKind> CallbackFilters =
            new List<AstNodeKind> {
                AstNodeKind.UiImport,
                AstNodeKind.UiQualifiedId,
                AstNodeKind.UiObjectDefinition,
                AstNodeKind.UiObjectBinding,
                AstNodeKind.UiObjectInitializer,
                AstNodeKind.UiScriptBinding,
                AstNodeKind.UiArrayBinding,
                AstNodeKind.UiPublicMember,
                AstNodeKind.FieldMemberExpression,
                AstNodeKind.IdentifierExpression
            };

        IntPtr qmlTextPtr = IntPtr.Zero;
        IntPtr qmlParserPtr = IntPtr.Zero;
        readonly List<Token> tokens;
        public IEnumerable<Token> Tokens
        {
            get { return tokens; }
        }

        readonly List<DiagnosticMessage> diagnosticMessages;
        public IEnumerable<DiagnosticMessage> DiagnosticMessages
        {
            get { return diagnosticMessages; }
        }

        private int FirstErrorOffset { get; set; }

        readonly List<AstNode> visitedNodes;
        public IEnumerable<AstNode> AstNodes { get { return visitedNodes; } }

        public bool ParsedCorrectly { get; private set; }

        readonly Dictionary<IntPtr, AstNode> nodesBytPtr;
        readonly Dictionary<IntPtr, List<KeyValuePair<AstNode, PropertyInfo>>> pendingDereferences;

        Parser()
        {
            tokens = new List<Token>();
            diagnosticMessages = new List<DiagnosticMessage>();
            nodesBytPtr = new Dictionary<IntPtr, AstNode>();
            pendingDereferences = new Dictionary<IntPtr,
                List<KeyValuePair<AstNode, PropertyInfo>>>();

            visitedNodes = new List<AstNode>();
        }

        public static Parser Parse(string qmlText)
        {
            var parser = new Parser();
            parser.Work(qmlText);
            return parser;
        }

        void Work(string qmlText)
        {
            // The Qt Declarative parser ignores CR's. However, the Visual Studio editor does not.
            // To ensure that offsets are compatible, CR's are replaced with spaces.
            string qmlTextNormalized = qmlText.Replace('\r', ' ');
            var qmlTextData = Encoding.UTF8.GetBytes(qmlTextNormalized);

            qmlTextPtr = Marshal.AllocHGlobal(qmlTextData.Length);
            Marshal.Copy(qmlTextData, 0, qmlTextPtr, qmlTextData.Length);

            IntPtr tokensPtr = IntPtr.Zero;
            int tokensLength = 0;

            NativeMethods.GetTokens(qmlTextPtr, qmlTextData.Length,
                ref tokensPtr, ref tokensLength);

            if (tokensPtr != IntPtr.Zero) {
                var tokensData = new byte[tokensLength];
                Marshal.Copy(tokensPtr, tokensData, 0, tokensLength);

                using (var rdr = new BinaryReader(new MemoryStream(tokensData))) {
                    while (rdr.BaseStream.Position + (3 * sizeof(int)) <= tokensLength) {
                        int kind = rdr.ReadInt32();
                        int offset = rdr.ReadInt32();
                        int length = rdr.ReadInt32();
                        tokens.Add(Token.Create((TokenKind)kind, offset, length));
                    }
                }
                NativeMethods.FreeTokens(tokensPtr);
            }

            bool parsedCorrectly = false;
            IntPtr diagnosticMessagesPtr = IntPtr.Zero;
            int diagnosticMessagesLength = 0;
            IntPtr commentsPtr = IntPtr.Zero;
            int commentsLength = 0;

            NativeMethods.Parse(qmlTextPtr, qmlTextData.Length,
                ref qmlParserPtr, ref parsedCorrectly,
                ref diagnosticMessagesPtr, ref diagnosticMessagesLength,
                ref commentsPtr, ref commentsLength);

            ParsedCorrectly = parsedCorrectly;

            if (diagnosticMessagesPtr != IntPtr.Zero) {
                var diagnosticMessagesData = new byte[diagnosticMessagesLength];
                Marshal.Copy(
                    diagnosticMessagesPtr,
                    diagnosticMessagesData, 0, diagnosticMessagesLength);

                FirstErrorOffset = qmlTextNormalized.Length + 1;
                using (var rdr = new BinaryReader(new MemoryStream(diagnosticMessagesData))) {
                    while (rdr.BaseStream.Position + (3 * sizeof(int)) <= diagnosticMessagesLength) {
                        var kind = (DiagnosticMessageKind)rdr.ReadInt32();
                        int offset = rdr.ReadInt32();
                        int length = rdr.ReadInt32();
                        diagnosticMessages.Add(new DiagnosticMessage(kind, offset, length));
                        if (kind == DiagnosticMessageKind.Error && offset < FirstErrorOffset)
                            FirstErrorOffset = offset;
                    }
                }
                NativeMethods.FreeDiagnosticMessages(diagnosticMessagesPtr);
            }

            if (commentsPtr != IntPtr.Zero) {
                var commentsData = new byte[commentsLength];
                Marshal.Copy(commentsPtr, commentsData, 0, commentsLength);

                using (var rdr = new BinaryReader(new MemoryStream(commentsData))) {
                    while (rdr.BaseStream.Position + (2 * sizeof(int)) <= commentsLength) {
                        int offset = rdr.ReadInt32();
                        int length = rdr.ReadInt32();
                        tokens.Add(Token.Create(TokenKind.T_COMMENT, offset, length));
                    }
                }
                NativeMethods.FreeComments(commentsPtr);
            }

            var astVisitor = NativeMethods.GetAstVisitor();
            var callback = new NativeMethods.Callback(VisitorCallback);

            foreach (var callbackFilter in CallbackFilters)
                NativeMethods.SetAstVisitorCallback(astVisitor, (int)callbackFilter, callback);

            NativeMethods.AcceptAstVisitor(qmlParserPtr, IntPtr.Zero, astVisitor);

            while (pendingDereferences.Count > 0) {
                var deref = pendingDereferences.First();
                NativeMethods.AcceptAstVisitor(qmlParserPtr, deref.Key, astVisitor);
                pendingDereferences.Remove(deref.Key);
            }

            GC.KeepAlive(callback);
            NativeMethods.FreeAstVisitor(astVisitor);
        }

        SourceLocation UnmarshalLocation(BinaryReader nodeData)
        {
            try {
                return new SourceLocation
                {
                    Offset = nodeData.ReadInt32(),
                    Length = nodeData.ReadInt32(),
                };
            } catch (Exception) {
                return new SourceLocation();
            }
        }

        void UnmarshalPointer(BinaryReader nodeData, AstNode node, PropertyInfo nodeProperty)
        {
            if (nodeData == null || node == null || nodeProperty == null)
                return;

            IntPtr ptrRef;
            try {
                long ptrHi = nodeData.ReadInt32();
                long ptrLo = nodeData.ReadInt32();
                ptrRef = new IntPtr((ptrHi << 32) | (ptrLo & 0xFFFFFFFFL));
            } catch (Exception) {
                return;
            }

            if (ptrRef == IntPtr.Zero)
                return;

            if (nodesBytPtr.TryGetValue(ptrRef, out AstNode nodeRef)) {
                nodeProperty.SetValue(node, nodeRef);
            } else {
                List<KeyValuePair<AstNode, PropertyInfo>> pendingRefList;
                if (!pendingDereferences.TryGetValue(ptrRef, out pendingRefList)) {
                    pendingDereferences[ptrRef] = pendingRefList =
                        new List<KeyValuePair<AstNode, PropertyInfo>>();
                }
                pendingRefList.Add(new KeyValuePair<AstNode, PropertyInfo>(node, nodeProperty));
            }
        }

        void UnmarshalNode(BinaryReader nodeData, AstNode node)
        {
            node.FirstSourceLocation = UnmarshalLocation(nodeData);
            node.LastSourceLocation = UnmarshalLocation(nodeData);
        }

        AstNode UnmarshalNode(BinaryReader nodeData, AstNodeKind nodeKind)
        {
            var node = new AstNode(nodeKind);
            UnmarshalNode(nodeData, node);
            return node;
        }

        UiImport UnmarshalUiImport(BinaryReader nodeData)
        {
            var node = new UiImport();
            UnmarshalNode(nodeData, node);
            node.ImportToken = UnmarshalLocation(nodeData);
            node.FileNameToken = UnmarshalLocation(nodeData);
            node.VersionToken = UnmarshalLocation(nodeData);
            node.AsToken = UnmarshalLocation(nodeData);
            node.ImportIdToken = UnmarshalLocation(nodeData);
            node.SemicolonToken = UnmarshalLocation(nodeData);
            return node;
        }

        UiQualifiedId UnmarshalUiQualifiedId(BinaryReader nodeData)
        {
            var node = new UiQualifiedId();
            UnmarshalNode(nodeData, node);
            UnmarshalPointer(nodeData, node, GetPropertyRef(() => node.Next));
            node.IdentifierToken = UnmarshalLocation(nodeData);
            return node;
        }

        UiObjectDefinition UnmarshalUiObjectDefinition(BinaryReader nodeData)
        {
            var node = new UiObjectDefinition();
            UnmarshalNode(nodeData, node);
            UnmarshalPointer(nodeData, node, GetPropertyRef(() => node.QualifiedTypeNameId));
            UnmarshalPointer(nodeData, node, GetPropertyRef(() => node.Initializer));
            return node;
        }

        UiObjectBinding UnmarshalUiObjectBinding(BinaryReader nodeData)
        {
            var node = new UiObjectBinding();
            UnmarshalNode(nodeData, node);
            UnmarshalPointer(nodeData, node, GetPropertyRef(() => node.QualifiedId));
            UnmarshalPointer(nodeData, node, GetPropertyRef(() => node.QualifiedTypeNameId));
            UnmarshalPointer(nodeData, node, GetPropertyRef(() => node.Initializer));
            node.ColonToken = UnmarshalLocation(nodeData);
            return node;
        }

        UiScriptBinding UnmarshalUiScriptBinding(BinaryReader nodeData)
        {
            var node = new UiScriptBinding();
            UnmarshalNode(nodeData, node);
            UnmarshalPointer(nodeData, node, GetPropertyRef(() => node.QualifiedId));
            UnmarshalPointer(nodeData, node, GetPropertyRef(() => node.Statement));
            node.ColonToken = UnmarshalLocation(nodeData);
            return node;
        }

        UiArrayBinding UnmarshalUiArrayBinding(BinaryReader nodeData)
        {
            var node = new UiArrayBinding();
            UnmarshalNode(nodeData, node);
            UnmarshalPointer(nodeData, node, GetPropertyRef(() => node.QualifiedId));
            UnmarshalPointer(nodeData, node, GetPropertyRef(() => node.Members));
            node.ColonToken = UnmarshalLocation(nodeData);
            node.LBracketToken = UnmarshalLocation(nodeData);
            node.RBracketToken = UnmarshalLocation(nodeData);
            return node;
        }

        UiPublicMember UnmarshalUiPublicMember(BinaryReader nodeData)
        {
            var node = new UiPublicMember();
            UnmarshalNode(nodeData, node);
            node.Type = (UiPublicMemberType)nodeData.ReadInt32();
            UnmarshalPointer(nodeData, node, GetPropertyRef(() => node.MemberType));
            UnmarshalPointer(nodeData, node, GetPropertyRef(() => node.Statement));
            UnmarshalPointer(nodeData, node, GetPropertyRef(() => node.Binding));
            node.IsDefaultMember = (nodeData.ReadInt32() != 0);
            node.IsReadonlyMember = (nodeData.ReadInt32() != 0);
            UnmarshalPointer(nodeData, node, GetPropertyRef(() => node.Parameters));
            node.DefaultToken = UnmarshalLocation(nodeData);
            node.ReadonlyToken = UnmarshalLocation(nodeData);
            node.PropertyToken = UnmarshalLocation(nodeData);
            node.TypeModifierToken = UnmarshalLocation(nodeData);
            node.TypeToken = UnmarshalLocation(nodeData);
            node.IdentifierToken = UnmarshalLocation(nodeData);
            node.ColonToken = UnmarshalLocation(nodeData);
            node.SemicolonToken = UnmarshalLocation(nodeData);
            return node;
        }

        /// <summary>
        /// This delegate method is called during the AST traversal when entering/exiting a node.
        /// </summary>
        /// <param name="astVisitor">Unmanaged AST visitor object ref</param>
        /// <param name="nodeKind">Type of node being visited</param>
        /// <param name="nodePtr">Node object ref</param>
        /// <param name="beginVisit">"true" when entering node, "false" when exiting</param>
        /// <param name="nodeDataPtr">Serialized content of AST node</param>
        /// <param name="nodeDataLength">Length in bytes of serialized content</param>
        /// <returns></returns>
        bool VisitorCallback(
            IntPtr astVisitor,
            int nodeKind,
            IntPtr nodePtr,
            bool beginVisit,
            IntPtr nodeDataPtr,
            int nodeDataLength)
        {
            if (!beginVisit)
                return true;

            AstNode node = null;
            var nodeData = new byte[nodeDataLength];
            Marshal.Copy(nodeDataPtr, nodeData, 0, nodeDataLength);
            using (var rdr = new BinaryReader(new MemoryStream(nodeData))) {
                switch ((AstNodeKind)nodeKind) {
                case AstNodeKind.UiImport:
                    node = UnmarshalUiImport(rdr);
                    break;
                case AstNodeKind.UiQualifiedId:
                    node = UnmarshalUiQualifiedId(rdr);
                    break;
                case AstNodeKind.UiObjectDefinition:
                    node = UnmarshalUiObjectDefinition(rdr);
                    break;
                case AstNodeKind.UiObjectBinding:
                    node = UnmarshalUiObjectBinding(rdr);
                    break;
                case AstNodeKind.UiScriptBinding:
                    node = UnmarshalUiScriptBinding(rdr);
                    break;
                case AstNodeKind.UiArrayBinding:
                    node = UnmarshalUiArrayBinding(rdr);
                    break;
                case AstNodeKind.UiPublicMember:
                    node = UnmarshalUiPublicMember(rdr);
                    break;
                default:
                    node = UnmarshalNode(rdr, (AstNodeKind)nodeKind);
                    break;
                }
            }
            if (node == null)
                return true;

            visitedNodes.Add(node);
            nodesBytPtr[nodePtr] = node;

            List<KeyValuePair<AstNode, PropertyInfo>> derefs;
            if (pendingDereferences.TryGetValue(nodePtr, out derefs)) {
                foreach (var deref in derefs) {
                    try {
                        deref.Value.SetValue(deref.Key, node);
                    } catch (Exception) { }
                }
                pendingDereferences.Remove(nodePtr);
            }

            return true;
        }

        void FreeManaged()
        {
        }

        void FreeUnmanaged()
        {
            if (qmlParserPtr != IntPtr.Zero) {
                NativeMethods.FreeParser(qmlParserPtr);
                qmlParserPtr = IntPtr.Zero;
            }
            if (qmlTextPtr != IntPtr.Zero) {
                Marshal.FreeHGlobal(qmlTextPtr);
                qmlTextPtr = IntPtr.Zero;
            }
        }

        #region IDisposable
        bool disposed = false;

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        protected virtual void Dispose(bool disposing)
        {
            if (disposed)
                return;

            if (disposing)
                FreeManaged();

            FreeUnmanaged();

            disposed = true;
        }

        ~Parser()
        {
            Dispose(false);
        }
        #endregion

        /// <summary>
        ///     Get a reference to a static or instance class member from a member access lambda.
        ///     Adapted from:
        ///         https://stackoverflow.com/questions/2820660/get-name-of-property-as-a-string
        /// </summary>
        /// <param name="memberLambda">
        ///     Lambda expression of the form: '() => Class.Member'or '() => object.Member'
        /// </param>
        /// <returns>Reference to the class member</returns>
        public static MemberInfo GetMemberRef<T>(Expression<Func<T>> memberLambda)
        {
            var me = memberLambda.Body as MemberExpression;
            if (me == null)
                return null;
            return me.Member;
        }

        public static PropertyInfo GetPropertyRef<T>(Expression<Func<T>> memberLambda)
        {
            return GetMemberRef(memberLambda) as PropertyInfo;
        }
    }
}