8-Roslyn for microsoft software framework.pptx

ahmedosman389 8 views 69 slides Jun 19, 2024
Slide 1
Slide 1 of 69
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35
Slide 36
36
Slide 37
37
Slide 38
38
Slide 39
39
Slide 40
40
Slide 41
41
Slide 42
42
Slide 43
43
Slide 44
44
Slide 45
45
Slide 46
46
Slide 47
47
Slide 48
48
Slide 49
49
Slide 50
50
Slide 51
51
Slide 52
52
Slide 53
53
Slide 54
54
Slide 55
55
Slide 56
56
Slide 57
57
Slide 58
58
Slide 59
59
Slide 60
60
Slide 61
61
Slide 62
62
Slide 63
63
Slide 64
64
Slide 65
65
Slide 66
66
Slide 67
67
Slide 68
68
Slide 69
69

About This Presentation

recently replaced a square wave in the modified form of PC. Nevertheless, in order to maximize the charging current, both forms of PC techniques need to operate at the ideal charging frequency, which can only be reached at low impedance.Because the optimization method requires iterations that increa...


Slide Content

Introducing Roslyn Presented by mohammad rahimi University of Science and Technology of Iran

Installation instructions There are two different ways to find the  .NET Compiler Platform SDK  in the  Visual Studio Installer :

Installation instructions Install using the Visual Studio Installer - Workloads view The .NET Compiler Platform SDK is not automatically selected as part of the Visual Studio extension development workload. You must select it as an optional component. 1- Run  Visual Studio Installer 2- Select  Modify 3- Check the  Visual Studio extension development  workload. 4-Open the  Visual Studio extension development  node in the summary tree. 5-Check the box for  .NET Compiler Platform SDK . You'll find it last under the optional components.

Installation instructions

Installation instructions

Installation instructions Optionally, you'll also want the  DGML editor  to display graphs in the visualizer: 1- Open the  Individual components  node in the summary tree. 2-Check the box for  DGML editor

Installation instructions Select DGML editor

Installation instructions Install using the Visual Studio Installer - Individual components tab 1-Run  Visual Studio Installer 2-Select  Modify 3-Select the  Individual components  tab 4-Check the box for  .NET Compiler Platform SDK . You'll find it at the top under the  Compilers, build tools, and runtimes  section. Optionally, you'll also want the  DGML editor  to display graphs in the visualizer : 1-Check the box for  DGML editor . You'll find it under the  Code tools  section.

Installation instructions

Choghakhor Wetland

Compiler Pipline First , the parse phase tokenizes and parses source text into syntax that follows the language grammar Second , the declaration phase analyzes source and imported metadata to form named symbols. Next , the bind phase matches identifiers in the code to symbols. Finally , the emit phase emits an assembly with all the information built up by the compiler.

API layers The .NET compiler SDK consists of two main layers of APIs: compiler APIs and workspaces APIs.

Compiler APIs The compiler layer contains the object models that correspond to information exposed at each phase of the compiler pipeline, both syntactic and semantic. The compiler layer also contains an immutable snapshot of a single invocation of a compiler, including assembly references, compiler options, and source code files. There are two distinct APIs that represent the C# language and the Visual Basic language.

Workspaces APIs It assists you in organizing all the information about the projects in a solution into single object model, offering you direct access to the compiler layer object models without needing to parse files, configure options, or manage project-to-project dependencies.

Workspaces APIs In addition, the Workspaces layer surfaces a set of APIs used when implementing code analysis and refactoring tools that function within a host environment like the Visual Studio IDE.

Parz Lordeghan Forest Park

Work with syntax syntax tree   serve two important purposes: 1-To allow tools - such as an IDE, add-ins, code analysis tools, and refactorings - to see and process the syntactic structure of source code in a user’s project. 2-To enable tools - such as refactorings and an IDE - to create, modify, and rearrange source code in a natural manner without having use direct text edits.

Syntax trees Syntax trees have three key attributes. 1- The first attribute is that syntax trees hold all the source information in full fidelity. 2- This enables the second attribute of syntax trees. A syntax tree obtained from the parser can produce the exact text it was parsed from. 3- The third attribute of syntax trees is that they are immutable and thread-safe. This means that after a tree is obtained, it is a snapshot of the current state of the code, and never changes.

Syntax trees

Syntax trees - nodes These nodes represent syntactic constructs such as declarations, statements, clauses, and expressions. Each category of syntax nodes is represented by a separate class derived from Microsoft.CodeAnalysis.SyntaxNode All syntax nodes are non-terminal nodes in the syntax tree. each node has a parent node that can be accessed through the SyntaxNode . Parent property.

Syntax trees - nodes Each node has a SyntaxNode.ChildNodes() method, which returns a list of child nodes in sequential order based on their position in the source text. Each node also has methods to examine Descendants, such as DescendantNodes , DescendantTokens , or DescendantTrivia , … each syntax node subclass exposes all the same children through strongly typed properties. For example, a BinaryExpressionSyntax node class has three additional properties specific to binary operators: Left , OperatorToken , and Right .

Syntax trees - tokens Syntax tokens are the terminals of the language grammar, representing the smallest syntactic fragments of the code. there is only one structure for all kinds of tokens with a mix of properties that have meaning depending on the kind of token that is being represented. the literal token has a Value property that tells you the exact decoded integer value. The ValueText property tells you the same information as the Value property; however this property is always typed as String.

Syntax trees - trivia Syntax trivia represent the parts of the source text that are largely insignificant for normal understanding of the code, such as white space, comments, and preprocessor directives. The single Microsoft.CodeAnalysis.Syntax Trivia type is used to describe all kinds of trivia. You can access trivia by inspecting a token’s SyntaxToken.LeadingTrivia or SyntaxToken.TrailingTrivia collections. Unlike syntax nodes and tokens, syntax trivia do not have parents.

Syntax trees - Spans A TextSpan object is the beginning position and a count of characters, both represented as integers Each node has two TextSpan properties: Span and FullSpan . The Span property is the text span from the start of the first token in the node’s sub-tree to the end of the last token. This span does not include any leading or trailing trivia. The FullSpan property is the text span that includes the node’s normal span, plus the span of any leading or trailing trivia.

Syntax trees - Kinds Each node, token, or trivia has a SyntaxNode.RawKind property, of type System.Int32, that identifies the exact syntax element represented. Each language, C# or Visual Basic, has a single SyntaxKind enumeration ( Microsoft.CodeAnalysis.CSharp.SyntaxKind and Microsoft.CodeAnalysis.VisualBasic.SyntaxKind , respectively) that lists all the possible nodes, tokens, and trivia elements in the grammar. The RawKind property allows for easy disambiguation of syntax node types that share the same node class.

Syntax trees - Errors When the parser encounters code that does not conform to the defined syntax of the language, it uses one of two techniques to create a syntax tree. 1- First, if the parser expects a particular kind of token but does not find it, it may insert a missing token into the syntax tree in the location that the token was expected. 2- Second, the parser may skip tokens until it finds one where it can continue parsing.

Work with syntax

Lordegan Miniature Waterfall

Work with semantics You can use it to discover the following: The symbols referenced at a specific location in source. The resultant type of any expression. All diagnostics, which are errors and warnings. How variables flow in and out of regions of source. The answers to more speculative questions.

Work with semantics - Compilation A compilation is a representation of everything needed to compile a C# or Visual Basic program, which includes all the assembly references, compiler options, and source files. The compilation contains a variety of methods that help you find and relate the symbols that have either been declared in the source code or imported as metadata from an assembly Similar to syntax trees, compilations are immutable

Work with semantics - Symbols A symbol represents a distinct element declared by the source code or imported from an assembly as metadata. A variety of methods and properties on the  Compilation  type help you find symbols Symbols also contain additional information that the compiler determines from the source or metadata, such as other referenced symbols.

Work with semantics - Symbols Each kind of symbol is represented by a separate interface derived from  Isymbol . Symbols present a common representation of namespaces, types, and members, between source code and metadata. Symbols are similar in concept to the CLR type system as represented by the  System.Reflection  API.

Work with a workspace - Workspace The Workspaces layer is the starting point for doing code analysis and refactoring over entire solutions. A workspace is an active representation of your solution as a collection of projects, each with a collection of documents. The  Workspace  provides access to the current model of the solution. You can also create stand-alone workspaces that are disconnected from the host environment or used in an application that has no host environment.

Work with a workspace - Solutions, projects, documents A solution is an immutable model of the projects and documents. you can modify solutions by constructing new instances based on existing solutions and specific changes. A project is a part of the overall immutable solution model. It represents all the source code documents, parse and compilation options, … A document is also a part of the overall immutable solution model. A document represents a single source file.

Work with a workspace - Solutions, projects, documents

Karun Dam 3

Get started with syntax analysis Create a new C#  Stand-Alone Code Analysis Tool  project: In Visual Studio, choose  File  >  New  >  Project  to display the New Project dialog. Under  Visual C#  >  Extensibility , choose  Stand-Alone Code Analysis Tool . Name your project " SyntaxTreeManualTraversal " and click OK.

syntax analysis const string programText = @" using System; using System.Text; namespace HelloWorld { class Program { static void Main(string[] args) { Console.WriteLine(""Hello, World!""); } } }";

syntax analysis - syntax tree Next, add the following code to build the syntax tree for the code text in the programText constant. Add the following line to your Main method: SyntaxTree tree = CSharpSyntaxTree .ParseText(programText); CompilationUnitSyntax root = tree .GetCompilationUnitRoot(); Those two lines create the tree and retrieve the root node of that tree. You can now examine the nodes in the tree. 

syntax analysis – root property Add these lines to your Main method to display some of the properties of the root node in the tree: WriteLine ($"The tree is a {root.Kind()} node."); WriteLine ($"The tree has {root.Members.Count} elements in it."); WriteLine ($"The tree has {root.Usings.Count} using statements. They are:"); foreach (UsingDirectiveSyntax element in root.Usings) WriteLine($"\t{element.Name}");

syntax analysis - APIs In this example, you're analyzing code you know to explore the APIs. Add the following code to examine the first member of the root node: MemberDeclarationSyntax firstMember = root.Members[0]; WriteLine($" The first member is a {firstMember.Kind()}."); var helloWorldDeclaration =(NamespaceDeclarationSyntax)firstMember;

syntax analysis – node types Add the following code to examine what nodes are declared inside the HelloWorld namespace: WriteLine ($"There are {helloWorldDeclaration.Members.Count} members declared in this namespace."); WriteLine ($"The first member is a {helloWorldDeclaration.Members[0].Kind()}.");

syntax analysis - Main method Add the following code to find the Main method, and cast it to a var programDeclaration = (ClassDeclarationSyntax)helloWorldDeclaration.Members[0]; WriteLine ($"There are {programDeclaration.Members.Count} members declared in the {programDeclaration.Identifier} class."); WriteLine ($"The first member is a {programDeclaration.Members[0].Kind()}."); var mainDeclaration = (MethodDeclarationSyntax)programDeclaration.Members[0];

syntax analysis - method declaration node WriteLine ($"The return type of the {mainDeclaration.Identifier} method is {mainDeclaration.ReturnType}."); WriteLine ($"The method has {mainDeclaration.ParameterList.Parameters.Count} parameters."); foreach ( ParameterSyntax item in mainDeclaration.ParameterList.Parameters ) WriteLine($"The type of the {item.Identifier} parameter is {item.Type}."); WriteLine($"The body text of the {mainDeclaration.Identifier} method follows:"); WriteLine(mainDeclaration.Body.ToFullString()); var argsParameter = mainDeclaration.ParameterList.Parameters[0];

syntax analysis - Run the program The tree is a CompilationUnit node. The tree has 1 elements in it. The tree has 4 using statements. They are: System System.Collections System.Linq System.Text The first member is a NamespaceDeclaration. There are 1 members declared in this namespace. The first member is a ClassDeclaration.

syntax analysis - Run the program There are 1 members declared in the Program class. The first member is a MethodDeclaration. The return type of the Main method is void. The method has 1 parameters. The type of the args parameter is string[]. The body text of the Main method follows: { Console.WriteLine("Hello, World!"); }

Karun Dam 4

Query methods you can also explore the syntax tree using the query methods defined on  Microsoft.CodeAnalysis.SyntaxNode . You can use these methods with LINQ to quickly find things in a tree. The SyntaxNode  has query methods as such DescendantNodes ,  AncestorsAndSelf  and  ChildNodes .  

Query methods You can use these query methods to find the argument to the Main method as an alternative to navigating the tree. var firstParameters = from methodDeclaration in root.DescendantNodes().OfType<MethodDeclarationSyntax> where methodDeclaration.Identifier.ValueText == "Main" select methodDeclaration.ParameterList.Parameters.First(); var argsParameter2 = firstParameters.Single(); WriteLine(argsParameter == argsParameter2);

Lake Clare

Querying symbols - semantic analysis Add the text for the Hello World program as a constant in your Program class: next page

Querying symbols - semantic analysis const string programText = @“ using System; using System.Collections.Generic; using System.Text; namespace HelloWorld {class Program {static void Main(string[] args) {Console.WriteLine(""Hello, World!"");} } } ";

Querying symbols - semantic analysis Next, add the following code to build the syntax tree for the code text in the programText constant. Add the following line to your Main method: SyntaxTree tree = CSharpSyntaxTree.ParseText(programText); CompilationUnitSyntax root = tree.GetCompilationUnitRoot(); In the code above, build a  CSharpCompilation  from the tree you already created.

Querying symbols - semantic analysis Add the following line to your Main method to create a compilation of your syntax tree, including the reference to the appropriate assembly: var compilation = CSharpCompilation .Create("HelloWorld") .AddReferences(MetadataReference.CreateFromFile typeof ( string ).Assembly.Location)) .AddSyntaxTrees(tree);

semantic analysis - Querying symbols The  CSharpCompilation.AddReferences  method adds references to the compilation. The  MetadataReference.CreateFromFile  method loads an assembly as a reference.

Querying the semantic model A  SemanticModel  can answer questions like "What names are in scope at this location?", "What members are accessible from this method?", "What variables are used in this block of text?", and "What does this name/expression refer to?" Add this statement to create the semantic model:   SemanticModel model = compilation.GetSemanticModel(tree);

semantic analysis - Binding a name Add these two lines to your Main method to create the semantic model and retrieve the symbol for the first using statement: // Use the syntax tree to find "using System;" UsingDirectiveSyntax usingSystem = root.Usings[0]; NameSyntax systemName = usingSystem.Name; // Use the semantic model for symbol information: SymbolInfo nameInfo = model.GetSymbolInfo(systemName);

semantic analysis - Binding a name Add these two lines to your Main method to create the semantic model and retrieve the symbol for the first using statement: // Use the syntax tree to find "using System;" UsingDirectiveSyntax usingSystem = root.Usings[0]; NameSyntax systemName = usingSystem.Name; // Use the semantic model for symbol information: SymbolInfo nameInfo = model.GetSymbolInfo(systemName);

semantic analysis - Binding a name The preceding code also illustrates that you use the syntax model to find the structure of the code; you use the semantic model to understand its meaning. From the  SymbolInfo  object you can obtain the Microsoft.CodeAnalysis.ISymbol   sing the  SymbolInfo.Symbol  property.

semantic analysis - Binding a name Add the following code to your Main method. It retrieves the symbol for the System namespace and then displays all the child namespaces declared in the System namespace: var systemSymbol = (INamespaceSymbol)nameInfo.Symbol; foreach (INamespaceSymbol ns in systemSymbol.GetNamespaceMembers()) { Console.WriteLine(ns); }

semantic analysis - Binding a name Run the program and you should see the following output: System.Collections System.Configuration System.Deployment System.Diagnostics System.Globalization System.IO System.Numerics ……………………… Press any key to continue . . .

semantic analysis - Binding a name There are other expressions in a C# program that can be bound that aren't names. // Use the syntax model to find the literal string: LiteralExpressionSyntax helloWorldString = root.DescendantNodes() .OfType<LiteralExpressionSyntax>() .Single(); // Use the semantic model for type information: TypeInfo literalInfo = model.GetTypeInfo(helloWorldString);

semantic analysis - Binding a name Add a declaration that assigns this property to a local variable: var stringTypeSymbol =(INamedTypeSymbol)literalInfo.Type;

semantic analysis - Binding a name To finish this tutorial, let's build a LINQ query that creates a sequence of all the public methods declared on the string type that return a string. The source for this query is the sequence of all members declared on the string type. var allMembers = stringTypeSymbol.GetMembers();

semantic analysis - Binding a name var methods = allMembers.OfType<IMethodSymbol>(); Next, add another filter to return only those methods that are public and return a string: var publicStringReturningMethods = methods.Where(m => m.ReturnType.Equals(stringTypeSymbol) && m.DeclaredAccessibility == Accessibility.Public);

semantic analysis - Binding a name Select only the name property, and only distinct names by removing any overloads: var distinctMethods = publicStringReturningMethods.Select(m =>m.Name).Distinct();

semantic analysis - Binding a name You can also build the full query using the LINQ query syntax, and then display all the method names in the console: foreach ( string name in ( from method in stringTypeSymbol .GetMembers().OfType<IMethodSymbol>() where method.ReturnType. Equals (stringTypeSymbol) && method.DeclaredAccessibility == Accessibility.Public select method.Name).Distinct()) { Console.WriteLine(name); }

Edit Syntax tree var workspace = new AdhocWorkspace(); var msBuild = MSBuildWorkspace.Create(); var project = workspace.AddProject(ProjectInfo.Create(ProjectId.CreateNewId (), VersionStamp.Create(), "NewProject" , "projName" , LanguageNames.CSharp)); Section1.c1.document1 = workspace.AddDocument(project.Id, "NewFile.cs" , SourceText.From(content)); DocumentEditor editor = DocumentEditor.CreateAsync(Section1.c1.document1).Result; int Count = Section1.c1.document1.GetSyntaxRootAsync().Result.DescendantNodes().OfType <IfStatementSyntax>().Count(); editor.ReplaceNode( node , newNode); Section1.c1.document1 = editor.GetChangedDocument(); editor= DocumentEditor.CreateAsync(Section1.c1.document1).Result;