File: BaseCodeDomTreeGenerator.cs

package info (click to toggle)
mono 6.8.0.105%2Bdfsg-3.3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,284,512 kB
  • sloc: cs: 11,172,132; xml: 2,850,069; ansic: 671,653; cpp: 122,091; perl: 59,366; javascript: 30,841; asm: 22,168; makefile: 20,093; sh: 15,020; python: 4,827; pascal: 925; sql: 859; sed: 16; php: 1
file content (872 lines) | stat: -rw-r--r-- 34,260 bytes parent folder | download | duplicates (7)
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
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
//------------------------------------------------------------------------------
// <copyright file="BaseCodeDomTreeGenerator.cs" company="Microsoft">
//     Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>
//------------------------------------------------------------------------------

/**********************************************

Class hierarchy:

BaseCodeDomTreeGenerator
    BaseTemplateCodeDomTreeGenerator
        TemplateControlCodeDomTreeGenerator
            PageCodeDomTreeGenerator
            UserControlCodeDomTreeGenerator
        PageThemeCodeDomTreeGenerator
    ApplicationFileCodeDomTreeGenerator
***********************************************/



namespace System.Web.Compilation {

using System.Text;
using System.Runtime.Serialization.Formatters;
using System.ComponentModel;
using System;
using System.Collections;
using System.Collections.Specialized;
using System.Reflection;
using System.IO;
using Microsoft.Win32;
using System.Security.Cryptography;
using System.Web.Caching;
using System.Web.Util;
using System.Web.UI;
using System.Web.SessionState;
using System.CodeDom;
using System.CodeDom.Compiler;
using Util = System.Web.UI.Util;
using System.Web.Hosting;
using System.Web.Profile;
using System.Web.Configuration;
using System.Globalization;


internal abstract class BaseCodeDomTreeGenerator {

    protected CodeDomProvider _codeDomProvider;
    protected CodeCompileUnit _codeCompileUnit;
    private CodeNamespace _sourceDataNamespace;
    protected CodeTypeDeclaration _sourceDataClass;
    protected CodeTypeDeclaration _intermediateClass;
    private CompilerParameters _compilParams;
    protected StringResourceBuilder _stringResourceBuilder;
    protected bool _usingVJSCompiler;
    private static IDictionary _generatedColumnOffsetDictionary;

    private VirtualPath _virtualPath;

    // The constructors
    protected CodeConstructor _ctor;

    protected CodeTypeReferenceExpression _classTypeExpr;

    internal const string defaultNamespace = "ASP";

    // Used for things that we don't want the user to see
    internal const string internalAspNamespace = "__ASP";

    private const string initializedFieldName = "__initialized";

    private const string _dummyVariable = "__dummyVar";
    private const int _defaultColumnOffset = 4;

    private TemplateParser _parser;
    TemplateParser Parser { get { return _parser; } }

    // We generate different code for the designer
    protected bool _designerMode;
    internal void SetDesignerMode() { _designerMode = true; }

    private IDictionary _linePragmasTable;
    internal IDictionary LinePragmasTable { get { return _linePragmasTable; } }

    // Used to generate indexed into the LinePragmasTable
    private int _pragmaIdGenerator=1;

    private static bool _urlLinePragmas;

    static BaseCodeDomTreeGenerator() {
        CompilationSection config = MTConfigUtil.GetCompilationAppConfig();

        _urlLinePragmas = config.UrlLinePragmas;
    }

#if DBG
    private bool _addedDebugComment;
#endif

#if DBG
    protected void AppendDebugComment(CodeStatementCollection statements) {
        if (!_addedDebugComment) {
            _addedDebugComment = true;
            StringBuilder debugComment = new StringBuilder();

            debugComment.Append("\r\n");
            debugComment.Append("** DEBUG INFORMATION **");
            debugComment.Append("\r\n");

            statements.Add(new CodeCommentStatement(debugComment.ToString()));
        }
    }
#endif

    internal /*public*/ CodeCompileUnit GetCodeDomTree(CodeDomProvider codeDomProvider,
        StringResourceBuilder stringResourceBuilder, VirtualPath virtualPath) {

        Debug.Assert(_codeDomProvider == null && _stringResourceBuilder == null);

        _codeDomProvider = codeDomProvider;
        _stringResourceBuilder = stringResourceBuilder;
        _virtualPath = virtualPath;

        // Build the data tree that needs to be compiled
        if (!BuildSourceDataTree())
            return null;

        // Tell the root builder that the CodeCompileUnit is now fully complete
        if (Parser.RootBuilder != null) {
            Parser.RootBuilder.OnCodeGenerationComplete();
        }

        return _codeCompileUnit;
    }

    protected /*public*/ CompilerParameters CompilParams { get { return _compilParams; } }

    internal string GetInstantiatableFullTypeName() {

        // In updatable mode, we never build the final type, so return null
        if (PrecompilingForUpdatableDeployment)
            return null;

        return Util.MakeFullTypeName(_sourceDataNamespace.Name, _sourceDataClass.Name);
    }

    internal string GetIntermediateFullTypeName() {

        return Util.MakeFullTypeName(Parser.BaseTypeNamespace, _intermediateClass.Name);
    }

    /*
     * Set some fields that are needed for code generation
     */
    protected BaseCodeDomTreeGenerator(TemplateParser parser) {
        _parser = parser;

        Debug.Assert(Parser.BaseType != null);
    }

    protected void ApplyEditorBrowsableCustomAttribute(CodeTypeMember member) {
        Debug.Assert(_designerMode, "This method should only be used in design mode.");

        // Generate EditorBrowsableAttribute to hide the generated methods from the tool
        // [EditorBrowsable(EditorBrowsableState.Never)]
        CodeAttributeDeclaration editorBrowsableAttribute = new CodeAttributeDeclaration();
        editorBrowsableAttribute.Name = typeof(EditorBrowsableAttribute).FullName;
        editorBrowsableAttribute.Arguments.Add(new CodeAttributeArgument(new CodeFieldReferenceExpression(new CodeTypeReferenceExpression(typeof(EditorBrowsableState)), "Never")));
        member.CustomAttributes.Add(editorBrowsableAttribute);
    }


    /// <devdoc>
    ///     Create a name for the generated class
    /// </devdoc>
    protected virtual string GetGeneratedClassName() {
        string className;

        // If the user specified the class name, just use that
        if (Parser.GeneratedClassName != null)
            return Parser.GeneratedClassName;

        // Use the input file name to generate the class name

        className = _virtualPath.FileName;

        // Prepend the class name with the directory path within the app (DevDiv 42063)
        string appRelVirtualDir = _virtualPath.Parent.AppRelativeVirtualPathStringOrNull;
        if (appRelVirtualDir != null) {
            Debug.Assert(UrlPath.IsAppRelativePath(appRelVirtualDir));
            className = appRelVirtualDir.Substring(2) + className;
        }

        // Change invalid chars to underscores
        className = Util.MakeValidTypeNameFromString(className);

        // Make it lower case to make it more predictable (VSWhidbey 503369)
        className = className.ToLowerInvariant();

        // If it's the same as the base type name, prepend it with an underscore to prevent
        // a compile error.
        string baseTypeName = Parser.BaseTypeName != null ? Parser.BaseTypeName : Parser.BaseType.Name;
        if (StringUtil.EqualsIgnoreCase(className, baseTypeName)) {
            className = "_" + className;
        }

        return className;
    }

    internal static bool IsAspNetNamespace(string ns) {
        return (ns == defaultNamespace);
    }

    private bool PrecompilingForUpdatableDeployment {
        get {
            // For global.asax, this never applies
            if (IsGlobalAsaxGenerator)
                return false;

            return BuildManager.PrecompilingForUpdatableDeployment;
        }
    }

    private bool BuildSourceDataTree() {

        _compilParams = Parser.CompilParams;

        _codeCompileUnit = new CodeCompileUnit();
        _codeCompileUnit.UserData["AllowLateBound"] = !Parser.FStrict;
        _codeCompileUnit.UserData["RequireVariableDeclaration"] = Parser.FExplicit;

        // Set a flag indicating if we're using the VJS compiler.  See comment in BuildExtractMethod for more information.
        _usingVJSCompiler = (_codeDomProvider.FileExtension == ".jsl");

        _sourceDataNamespace = new CodeNamespace(Parser.GeneratedNamespace);

        string generatedClassName = GetGeneratedClassName();

        if (Parser.BaseTypeName != null) {

            Debug.Assert(Parser.CodeFileVirtualPath != null);

            // This is the case where the page has a CodeFile attribute

            CodeNamespace intermediateNamespace = new CodeNamespace(Parser.BaseTypeNamespace);
            _codeCompileUnit.Namespaces.Add(intermediateNamespace);
            _intermediateClass = new CodeTypeDeclaration(Parser.BaseTypeName);

            // Specify the base class in the UserData in case the CodeDom provider needs
            // to reflect on it when generating code from the CodeCompileUnit (VSWhidbey 475294)
            // In design mode, use the default base type (e.g. Page or UserControl) to avoid
            // ending up with a type that can't be serialized to the Venus domain (VSWhidbey 545535)
            if (_designerMode)
                _intermediateClass.UserData["BaseClassDefinition"] = Parser.DefaultBaseType;
            else
                _intermediateClass.UserData["BaseClassDefinition"] = Parser.BaseType;

            intermediateNamespace.Types.Add(_intermediateClass);

            // Generate a partial class
            _intermediateClass.IsPartial = true;

            // Unless we're precompiling for updatable deployment, create the derived class
            if (!PrecompilingForUpdatableDeployment) {

                _sourceDataClass = new CodeTypeDeclaration(generatedClassName);
                // VSWhidbey 411701. Always use global type reference for the baseType 
                // when codefile is present.
                _sourceDataClass.BaseTypes.Add(CodeDomUtility.BuildGlobalCodeTypeReference(
                    Util.MakeFullTypeName(Parser.BaseTypeNamespace, Parser.BaseTypeName)));

                _sourceDataNamespace.Types.Add(_sourceDataClass);
            }
        }
        else {

            // The page is not using code besides

            _intermediateClass = new CodeTypeDeclaration(generatedClassName);
            _intermediateClass.BaseTypes.Add(CodeDomUtility.BuildGlobalCodeTypeReference(Parser.BaseType));
            _sourceDataNamespace.Types.Add(_intermediateClass);

            // There is only one class, so make both fields point to the same thing
            _sourceDataClass = _intermediateClass;
        }

        // Add the derived class namespace after the base partial class so C# parser
        // can still parse the code correctly in case the derived class contains error.
        // VSWhidbey 397646
        _codeCompileUnit.Namespaces.Add(_sourceDataNamespace);

        // We don't generate any code during updatable precompilation of a single (inline) page,
        // except for global.asax
        if (PrecompilingForUpdatableDeployment && Parser.CodeFileVirtualPath == null)
            return false;

        // Add metadata attributes to the class
        GenerateClassAttributes();

        // In VB, always import Microsoft.VisualBasic (VSWhidbey 256475)
        if (_codeDomProvider is Microsoft.VisualBasic.VBCodeProvider)
            _sourceDataNamespace.Imports.Add(new CodeNamespaceImport("Microsoft.VisualBasic"));

        // Add all the namespaces
        if (Parser.NamespaceEntries != null) {
            foreach (NamespaceEntry entry in Parser.NamespaceEntries.Values) {
                // Create a line pragma if available
                CodeLinePragma linePragma;
                if (entry.VirtualPath != null) {
                    linePragma = CreateCodeLinePragma(entry.VirtualPath, entry.Line);
                }
                else {
                    linePragma = null;
                }

                CodeNamespaceImport nsi = new CodeNamespaceImport(entry.Namespace);
                nsi.LinePragma = linePragma;

                _sourceDataNamespace.Imports.Add(nsi);
            }
        }

        if (_sourceDataClass != null) {
            // We need to generate a global reference to avoid ambiguities (VSWhidbey 284936)
            string fullClassName = Util.MakeFullTypeName(_sourceDataNamespace.Name, _sourceDataClass.Name);
            CodeTypeReference classTypeRef = CodeDomUtility.BuildGlobalCodeTypeReference(fullClassName);

            // Since this is needed in several places, store it in a member variable
            _classTypeExpr = new CodeTypeReferenceExpression(classTypeRef);
        }

        // Add the implemented interfaces
        GenerateInterfaces();

        // Build various properties, fields, methods
        BuildMiscClassMembers();

        // Build the default constructors
        if (!_designerMode && _sourceDataClass != null) {
            _ctor = new CodeConstructor();
            AddDebuggerNonUserCodeAttribute(_ctor);
            _sourceDataClass.Members.Add(_ctor);
            BuildDefaultConstructor();
        }

        return true;
    }

    /*
     * Add metadata attributes to the class
     */
    protected virtual void GenerateClassAttributes() {
        // If this is a debuggable page, generate a
        // CompilerGlobalScopeAttribute attribute (ASURT 33027)
        if (CompilParams.IncludeDebugInformation && _sourceDataClass != null) {
            CodeAttributeDeclaration attribDecl = new CodeAttributeDeclaration(
                "System.Runtime.CompilerServices.CompilerGlobalScopeAttribute");
            _sourceDataClass.CustomAttributes.Add(attribDecl);
        }
    }

    /*
     * Generate the list of implemented interfaces
     */
    protected virtual void GenerateInterfaces() {
        if (Parser.ImplementedInterfaces != null) {
            foreach (Type t in Parser.ImplementedInterfaces) {
                _intermediateClass.BaseTypes.Add(new CodeTypeReference(t));
            }
        }
    }

    /*
     * Build first-time intialization statements
     */
    protected virtual void BuildInitStatements(CodeStatementCollection trueStatements, CodeStatementCollection topLevelStatements) {
    }


    /*
     * Build the default constructor
     */
    protected virtual void BuildDefaultConstructor() {

        _ctor.Attributes &= ~MemberAttributes.AccessMask;
        _ctor.Attributes |= MemberAttributes.Public;

        // private static bool __initialized;
        CodeMemberField initializedField = new CodeMemberField(typeof(bool), initializedFieldName);
        initializedField.Attributes |= MemberAttributes.Static;
        _sourceDataClass.Members.Add(initializedField);


        // if (__intialized == false)
        CodeConditionStatement initializedCondition = new CodeConditionStatement();
        initializedCondition.Condition = new CodeBinaryOperatorExpression(
                                                new CodeFieldReferenceExpression(
                                                    _classTypeExpr,
                                                    initializedFieldName),
                                                CodeBinaryOperatorType.ValueEquality,
                                                new CodePrimitiveExpression(false));

        this.BuildInitStatements(initializedCondition.TrueStatements, _ctor.Statements);

        initializedCondition.TrueStatements.Add(new CodeAssignStatement(
                                                    new CodeFieldReferenceExpression(
                                                        _classTypeExpr,
                                                        initializedFieldName),
                                                    new CodePrimitiveExpression(true)));

        // i.e. __intialized = true;
        _ctor.Statements.Add(initializedCondition);
    }

    /*
     * Build various properties, fields, methods
     */
    protected virtual void BuildMiscClassMembers() {

        // Build the Profile property
        if (NeedProfileProperty)
            BuildProfileProperty();

        // Skip the rest if we're only generating the intermediate class
        if (_sourceDataClass == null)
            return;

        // Build the injected properties from the global.asax <object> tags
        BuildApplicationObjectProperties();
        BuildSessionObjectProperties();

        // Build the injected properties for objects scoped to the page
        BuildPageObjectProperties();

        // Add all the <script runat=server> code blocks
        foreach (ScriptBlockData script in Parser.ScriptList) {

            // Pad the code block so its generated offset matches the aspx
            string code = script.Script;
            code = code.PadLeft(code.Length + script.Column - 1);

            CodeSnippetTypeMember literal = new CodeSnippetTypeMember(code);
            literal.LinePragma = CreateCodeLinePragma(script.VirtualPath, script.Line,
                script.Column, script.Column, script.Script.Length, false);
            _sourceDataClass.Members.Add(literal);
        }
    }

    /*
     * Build the Profile property
     */
    private void BuildProfileProperty() {

        if (!ProfileManager.Enabled)
            return;

        CodeMemberProperty prop;
        string typeName = ProfileBase.GetProfileClassName();

        prop = new CodeMemberProperty();
        prop.Attributes &= ~MemberAttributes.AccessMask;
        prop.Attributes &= ~MemberAttributes.ScopeMask;
        prop.Attributes |= MemberAttributes.Final | MemberAttributes.Family;
        prop.Name = "Profile";

        if (_designerMode) {
            ApplyEditorBrowsableCustomAttribute(prop);
        }

        //if (ProfileBase.GetPropertiesForCompilation().Count == 0)
        //    typeName = "System.Web.Profile.DefaultProfile";
        //else
        //    typeName = "ASP.Profile";
        prop.Type = new CodeTypeReference(typeName);

        CodePropertyReferenceExpression propRef = new CodePropertyReferenceExpression(
            new CodeThisReferenceExpression(), "Context");
        propRef = new CodePropertyReferenceExpression(propRef, "Profile");

        prop.GetStatements.Add(new CodeMethodReturnStatement(new CodeCastExpression(
            typeName, propRef)));
        _intermediateClass.Members.Add(prop);
    }

    // By default, we build the Profile property
    protected virtual bool NeedProfileProperty { get { return true; } }

    protected void BuildAccessorProperty(string propName, CodeFieldReferenceExpression fieldRef,
        Type propType, MemberAttributes attributes, CodeAttributeDeclarationCollection attrDeclarations) {

        // e.g.
        // [attrDeclaration]
        // public SomeType SomeProp {
        //    get {
        //        return this.__SomeProp;
        //    }
        // }
        CodeMemberProperty prop = new CodeMemberProperty();
        prop.Attributes = attributes;
        prop.Name = propName;
        prop.Type = new CodeTypeReference(propType);
        prop.GetStatements.Add(new CodeMethodReturnStatement(fieldRef));
        prop.SetStatements.Add(new CodeAssignStatement(
            fieldRef, new CodePropertySetValueReferenceExpression()));

        if (attrDeclarations != null) {
            prop.CustomAttributes = attrDeclarations;
        }

        _sourceDataClass.Members.Add(prop);
    }

    protected void BuildFieldAndAccessorProperty(string propName, string fieldName,
        Type propType, bool fStatic, CodeAttributeDeclarationCollection attrDeclarations) {

        // e.g. private SomeType __SomeProp;
        CodeMemberField field = new CodeMemberField(propType, fieldName);
        if (fStatic) {
            field.Attributes |= MemberAttributes.Static;
        }
        _sourceDataClass.Members.Add(field);

        CodeFieldReferenceExpression fieldRef = new CodeFieldReferenceExpression(
            new CodeThisReferenceExpression(), fieldName);
        BuildAccessorProperty(propName, fieldRef, propType, MemberAttributes.Public, attrDeclarations);
    }

    /*
     * Helper method used to build the properties of injected
     * global.asax properties.  These look like:
     *   PropType __propName;
     *   protected PropType propName
     *   {
     *       get
     *       {
     *           if (__propName == null)
     *               __propName = [some expression];
     *
     *           return __propName;
     *       }
     *   }
     */
    private void BuildInjectedGetPropertyMethod(string propName,
                                                Type propType,
                                                CodeExpression propertyInitExpression,
                                                bool fPublicProp) {

        string fieldName = "cached" + propName;

        CodeExpression fieldAccess = new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), fieldName);

        // Add a private field for the object
        _sourceDataClass.Members.Add(new CodeMemberField(propType, fieldName));

        CodeMemberProperty prop = new CodeMemberProperty();
        if (fPublicProp) {
            prop.Attributes &= ~MemberAttributes.AccessMask;
            prop.Attributes |= MemberAttributes.Public;
        }
        prop.Name = propName;
        prop.Type = new CodeTypeReference(propType);


        CodeConditionStatement ifStmt = new CodeConditionStatement();
        ifStmt.Condition = new CodeBinaryOperatorExpression(fieldAccess, CodeBinaryOperatorType.IdentityEquality, new CodePrimitiveExpression(null));
        ifStmt.TrueStatements.Add(new CodeAssignStatement(fieldAccess, propertyInitExpression));

        prop.GetStatements.Add(ifStmt);
        prop.GetStatements.Add(new CodeMethodReturnStatement(fieldAccess));

        _sourceDataClass.Members.Add(prop);
    }

    /*
     * Helper method for building application and session scope injected
     * properties.  If useApplicationState, build application properties, otherwise
     * build session properties.
     */
    private void BuildObjectPropertiesHelper(IDictionary objects, bool useApplicationState) {

        IDictionaryEnumerator en = objects.GetEnumerator();
        while (en.MoveNext()) {
            HttpStaticObjectsEntry entry = (HttpStaticObjectsEntry)en.Value;

            // e.g. (PropType)Session.StaticObjects["PropName"]

            // Use the appropriate collection
            CodePropertyReferenceExpression stateObj = new CodePropertyReferenceExpression(new CodePropertyReferenceExpression(new CodeThisReferenceExpression(),
                                                                                                                               useApplicationState ? "Application" : "Session"),
                                                                                           "StaticObjects");

            CodeMethodInvokeExpression getObject = new CodeMethodInvokeExpression(stateObj, "GetObject");
            getObject.Parameters.Add(new CodePrimitiveExpression(entry.Name));


            Type declaredType = entry.DeclaredType;
            Debug.Assert(!Util.IsLateBoundComClassicType(declaredType));

            if (useApplicationState) {
                // for application state use property that does caching in a member
                BuildInjectedGetPropertyMethod(entry.Name, declaredType,
                                               new CodeCastExpression(declaredType, getObject),
                                               false /*fPublicProp*/);
            }
            else {
                // for session state use lookup every time, as one application instance deals with many sessions
                CodeMemberProperty prop = new CodeMemberProperty();
                prop.Name = entry.Name;
                prop.Type = new CodeTypeReference(declaredType);
                prop.GetStatements.Add(new CodeMethodReturnStatement(new CodeCastExpression(declaredType, getObject)));
                _sourceDataClass.Members.Add(prop);
            }
        }
    }

    /*
     * Build the injected properties from the global.asax <object> tags
     * declared with scope=application
     */
    private void BuildApplicationObjectProperties() {
        if (Parser.ApplicationObjects != null)
            BuildObjectPropertiesHelper(Parser.ApplicationObjects.Objects, true);
    }

    /*
     * Build the injected properties from the global.asax <object> tags
     * declared with scope=session
     */
    private void BuildSessionObjectProperties() {
        if (Parser.SessionObjects != null)
            BuildObjectPropertiesHelper(Parser.SessionObjects.Objects, false);
    }

    protected virtual bool IsGlobalAsaxGenerator { get { return false; } }

    /*
     * Build the injected properties from the global.asax <object> tags
     * declared with scope=appinstance, or the aspx/ascx tags with scope=page.
     */
    private void BuildPageObjectProperties() {
        if (Parser.PageObjectList == null) return;

        foreach (ObjectTagBuilder obj in Parser.PageObjectList) {

            CodeExpression propertyInitExpression;

            if (obj.Progid != null) {
                // If we are dealing with a COM classic object that hasn't been tlbreg'ed,
                // we need to call HttpServerUtility.CreateObject(progid) to create it
                CodeMethodInvokeExpression createObjectCall = new CodeMethodInvokeExpression();

                createObjectCall.Method.TargetObject = new CodePropertyReferenceExpression(
                    new CodeThisReferenceExpression(), "Server");
                createObjectCall.Method.MethodName = "CreateObject";
                createObjectCall.Parameters.Add(new CodePrimitiveExpression(obj.Progid));

                propertyInitExpression = createObjectCall;
            }
            else if (obj.Clsid != null) {
                // Same as previous case, but with a clsid instead of a progId
                CodeMethodInvokeExpression createObjectCall = new CodeMethodInvokeExpression();
                createObjectCall.Method.TargetObject = new CodePropertyReferenceExpression(
                    new CodeThisReferenceExpression(), "Server");
                createObjectCall.Method.MethodName = "CreateObjectFromClsid";
                createObjectCall.Parameters.Add(new CodePrimitiveExpression(obj.Clsid));

                propertyInitExpression = createObjectCall;
            }
            else {
                propertyInitExpression = new CodeObjectCreateExpression(obj.ObjectType);
            }

            // Make the appinstance properties public for global.asax (ASURT 63253)
            BuildInjectedGetPropertyMethod(obj.ID, obj.DeclaredType,
                propertyInitExpression, IsGlobalAsaxGenerator /*fPublicProp*/);
        }
    }

    protected CodeLinePragma CreateCodeLinePragma(ControlBuilder builder) {

        string virtualPath = builder.PageVirtualPath;
        int line = builder.Line;
        int column = 1;
        int generatedColumn = 1;
        int codeLength = -1;

        CodeBlockBuilder codeBlockBuilder = builder as CodeBlockBuilder;
        if (codeBlockBuilder != null) {
            column = codeBlockBuilder.Column;
            codeLength = codeBlockBuilder.Content.Length;

            if (codeBlockBuilder.BlockType == CodeBlockType.Code) {
                // If it's a <% ... %> block, the generated column is the same as the source
                generatedColumn = column;
            }
            else {
                // If it's a <%= ... %> block, we always generate '__o = expr' is
                // designer mode, so the column is fixed
                // 
                generatedColumn = BaseTemplateCodeDomTreeGenerator.tempObjectVariable.Length + 
                    GetGeneratedColumnOffset(_codeDomProvider);
            }
        }

        return CreateCodeLinePragma(virtualPath, line, column, generatedColumn, codeLength);
    }

    internal static int GetGeneratedColumnOffset(CodeDomProvider codeDomProvider) {
        object o = null;

        if (_generatedColumnOffsetDictionary == null) {
            _generatedColumnOffsetDictionary = new ListDictionary();
        }
        else {
            o = _generatedColumnOffsetDictionary[codeDomProvider.GetType()];
        }

        if (o == null) {
            CodeCompileUnit ccu = new CodeCompileUnit();

            CodeNamespace cnamespace = new CodeNamespace("ASP");
            ccu.Namespaces.Add(cnamespace);

            CodeTypeDeclaration type = new CodeTypeDeclaration("ColumnOffsetCalculator");
            type.IsClass = true;
            cnamespace.Types.Add(type);

            CodeMemberMethod method = new CodeMemberMethod();
            method.ReturnType = new CodeTypeReference(typeof(void));
            method.Name = "GenerateMethod";
            type.Members.Add(method);

            CodeStatement simpleAssignment = new CodeAssignStatement(
                new CodeVariableReferenceExpression(BaseTemplateCodeDomTreeGenerator.tempObjectVariable),
                new CodeSnippetExpression(_dummyVariable));
            method.Statements.Add(simpleAssignment);

            StringBuilder sb = new StringBuilder();
            StringWriter w = new StringWriter(sb, CultureInfo.InvariantCulture);

            codeDomProvider.GenerateCodeFromCompileUnit(ccu, w, null);

            StringReader reader = new StringReader(sb.ToString());
            String line = null;

            int offset = _defaultColumnOffset;

            while ((line = reader.ReadLine()) != null) {
                int index = 0;
                line = line.TrimStart();
                if ((index = line.IndexOf(_dummyVariable, StringComparison.Ordinal)) != -1) {
                    offset = index - BaseTemplateCodeDomTreeGenerator.tempObjectVariable.Length + 1;
                }
            }

            // Save the offset per type.
            _generatedColumnOffsetDictionary[codeDomProvider.GetType()] = offset;

            return offset;
        }

        return (int)o;
    }

    protected CodeLinePragma CreateCodeLinePragma(string virtualPath, int lineNumber) {
        return CreateCodeLinePragma(virtualPath, lineNumber, 1, 1, -1, true);
    }

    protected CodeLinePragma CreateCodeLinePragma(string virtualPath, int lineNumber,
        int column, int generatedColumn, int codeLength) {
        return CreateCodeLinePragma(virtualPath, lineNumber, column, generatedColumn, codeLength, true);
    }

    protected CodeLinePragma CreateCodeLinePragma(string virtualPath, int lineNumber,
        int column, int generatedColumn, int codeLength, bool isCodeNugget) {

        // Return null if we're not supposed to generate line pragmas
        if (!Parser.FLinePragmas)
            return null;

        // The problem with disabling pragmas in non-debug is that we no longer
        // get line information on compile errors, while in v1 we did. So
        // unless we find a better solution, don't disable pragmas in non-debug
/*
        // Also, don't bother with pragmas unless we're compiling for debugging
        if (!CompilParams.IncludeDebugInformation)
            return null;
*/

        if (String.IsNullOrEmpty(virtualPath))
            return null;

        if (_designerMode) {

            // Only generate pragmas for code blocks in designer mode
            if (codeLength < 0)
                return null;

            LinePragmaCodeInfo codeInfo = new LinePragmaCodeInfo();
            codeInfo._startLine = lineNumber;
            codeInfo._startColumn = column;
            codeInfo._startGeneratedColumn = generatedColumn;
            codeInfo._codeLength = codeLength;
            codeInfo._isCodeNugget = isCodeNugget;
            lineNumber = _pragmaIdGenerator++;

            if (_linePragmasTable == null)
                _linePragmasTable = new Hashtable();

            _linePragmasTable[lineNumber] = codeInfo;
        }

        return CreateCodeLinePragmaHelper(virtualPath, lineNumber);
    }

    internal static CodeLinePragma CreateCodeLinePragmaHelper(string virtualPath, int lineNumber) {

        string pragmaFile = null;

        if (UrlPath.IsAbsolutePhysicalPath(virtualPath)) {

            // Due to config system limitations, we can end up with virtualPath
            // actually being a physical path.  If that's the case, just use it as is.
            // 

            pragmaFile = virtualPath;
        }
        else {

            if (_urlLinePragmas) {

                // If specified in config, generate URL's for the line pragmas
                // instead of physical path.  This is used for VS debugging.
                // We don't know the server name, so we just used a fixed one.  This should be
                // fine, as VS will ignore the server name.

                pragmaFile = ErrorFormatter.MakeHttpLinePragma(virtualPath);
            }
            else {
                try {
                    // Try using the physical path for the line pragma
                    pragmaFile = HostingEnvironment.MapPathInternal(virtualPath);

                    // If the physical path doesn't exist, use the URL instead.
                    // This can happen when using a VirtualPathProvider (VSWhidbey 272259)
                    if (!File.Exists(pragmaFile)) {
                        pragmaFile = ErrorFormatter.MakeHttpLinePragma(virtualPath);
                    }
                }
                catch {
                    // If MapPath failed, use the URL instead
                    pragmaFile = ErrorFormatter.MakeHttpLinePragma(virtualPath);
                }
            }
        }

        return new CodeLinePragma(pragmaFile, lineNumber);
    }

    // Adds [DebuggerNonUserCode] to the method to prevent debugger from stepping into generated code
    protected void AddDebuggerNonUserCodeAttribute(CodeMemberMethod method) {
        if (method == null) return;
        // If LinePragmas is false, the user might want to debug generated code, so we do not add the attribute
        if (!Parser.FLinePragmas) return; 

        CodeAttributeDeclaration attributeDeclaration = new CodeAttributeDeclaration(new CodeTypeReference(typeof(System.Diagnostics.DebuggerNonUserCodeAttribute)));
        method.CustomAttributes.Add(attributeDeclaration);
    }
}

}