Document not found! Please try again

Integrating Ada 2005 into Visual Studio 2005 - ACM Digital Library

4 downloads 0 Views 563KB Size Report
Nov 12, 2006 - Ada was the first language to include mixed-language pragmas as part of its specification, allowing it to easily interface with other programming ...
Integrating Ada 2005 into Visual Studio 2005 Martin C. Carlisle

J.A. “Drew” Hamilton, Jr.

Department of Computer Science 2354 Fairchild Drive, Suite 6G101 U.S. Air Force Academy, CO 80840-6234

Computer Science & Software Engineering 107 Dunstan Hall Auburn University, AL 36849

[email protected]

[email protected] enthusiastic group of early users, especially in educational and research institutions, often has broad influence on adoption of programming languages.[2]”

ABSTRACT In this paper, we describe the integration of an Ada 2005 compiler into Microsoft’s 2005 Visual Studio and its implications for the Ada community and beyond.

A#, MGNAT, JGNAT, Ada 2005, Visual Studio 2005

While many in the Ada community view Microsoft as the focus of evil in the modern world of computing, there is no denying the major impact of Microsoft is making in software engineering. Regarding the .NET Framework, Eric Braude writes that “Microsoft wanted to allow developers to create interoperable components using their choice of source language.[4]” More importantly, the .NET Framework provides a practical means for widespread software reuse. Reusing software components is a textbook software engineering best practice. Developers reuse components written by others, combining them in unique ways to create new software products. For this reason, the development of A# is a significant milestone in broadening the use and the reuse of Ada 2005.

1. INTRODUCTION

3. A#

Categories and Subject Descriptors D.2.6 [Programming Environments]: Programming Environments – graphical environments, integrated environments, programmer workbench.

General Terms Languages

Keywords

Ada is well known for supporting good software engineering practices and for interfacing cleanly with other languages—these features have only gotten better with Ada 2005. The A# project is an open-source implementation of Ada 2005 for Microsoft’s .NET framework.

Throughout its history, complaints about Ada have been directed and misdirected against real and perceived lack of tools and environments. The integration of A# into the .NET environment and the further integration of A# into Microsoft’s Visual Studio 2005 are significant milestones in the usability of Ada 2005. Ada was the first language to include mixed-language pragmas as part of its specification, allowing it to easily interface with other programming languages. A# is a modification of Ada that seeks to create a fully-interoperable environment for an Ada programmer using .NET [1]. Ada programmers can use libraries written by other .NET programmers programming in other languages, and also share their libraries with programmers using other languages [1].

3.1 Overview Using A#, programmers can combine Ada code with reusable .NET components, including modules written in C#, as well as legacy component object model (COM) components and Win32 Dynamically Linked Libraries (DLLs). This allows leveraging both the software engineering advantages of Ada and the large amount of reusable libraries written for .NET. Additionally, A# targets portable digital assistants (PDAs) and other mobile and embedded devices.

2. PROGRAMMING LANGUAGES AND DEVELOPMENT ENVIRONMENTS

The A# project (http://asharp.martincarlisle.com) seeks to alleviate perceived shortages of Ada components by providing Ada developers access to the reusable components available from the .NET Framework. By providing an open-source compilation environment for Ada on the .NET framework, A# gives software developers the opportunity to leverage the large amount of reusable .NET classes while also being able to write code in a language that more strongly supports good software engineering practices.

The 1995 National Research Council report “Ada and Beyond” stated: “In decisions affecting adoption of programming languages, non-technical factors often dominate specific technical features. compilers and tools for a wide variety of computing environments, as well as the availability of texts and related training materials. In addition, grass-roots advocacy by an Copyright 2006 Association for Computing Machinery. ACM acknowledges that this contribution was authored or co-authored by an employee, contractor or affiliate of the U.S. Government. As such, the Government retains a nonexclusive, royalty-free right to publish or reproduce this article, or to allow others to do so, for Government purposes only. SIGAda’06, November 12–16, 2006, Albuquerque, New Mexico, USA. Copyright 2006 ACM 1-59593-563-0/06/0011...$5.00.

3.2 Compiling Ada 2005 in the .NET Environment One of the key design goals for .NET was supporting multiple different programming languages [3]. Microsoft provides technical support to language developers and has published a list

15

of 27 languages that compile to .NET (not including the 4 distributed with Visual Studio) [5].

Consider the following C# class:

The .NET Common Intermediate Language (CIL) bears a strong resemblance to Java bytecode. Therefore, JGNAT [6], an open source Ada compiler which allowed Ada code to run on the Java Virtual Machine, was used as a starting point for the compiler. The backend was rewritten to emit CIL instead of Java bytecode. Also, the Ada standard library packages were rewritten to call .NET routines instead of those from the Java platform. JGNAT is no longer maintained by Ada Core Technologies, so the front end of that compiler does not contain any Ada 2005 features. Therefore, the latest GNAT compiler (with some modifications) is used for the front end [7]. GNAT is an open-source Ada 2005 compiler distributed under the Free Software Foundation General Public License. GNAT runs on many different platforms, but not .NET.

{

namespace SIGAda2006 public class Class1:Superclass,MyInterface { public color my_color; private string s; public Class1(string x) {…} public void package() {…} public static color get_color() {…} private int get_value() {…} } }

Msil2ada parses the compiled DLL containing this class and generates the following Ada specification (corresponding to only public attributes and methods):

To make using the compiler easier, we previously modified AdaGIDE [8], a freely available development environment for Ada, so that the A# compiler, MGNAT, can be selected simply by pushing the target button and then selecting the .NET radio button. In this paper, we describe how A# has been integrated into Visual Studio .NET 2005. Once the integration installer has been run, A# shows on the list of installed products on the Visual Studio splash screen, as shown in Figure 1.

with SIGAda2006.Superclass; with SIGAda2006.MyInterface; with SIGAda2006.color; limited with MSSyst.String; package SIGAda2006.Class1 is type Typ; type Ref is access all Typ'Class; type Typ is new SIGAda2006.Superclass.Typ and SIGAda2006.MyInterface.Typ with record my_color : SIGAda2006.color.Valuetype; pragma Import(MSIL,my_color,"my_color"); end record; function new_Class1(This : Ref := null; x:access MSSyst.String.Typ) return Ref; function get_color return SIGAda2006.color.Valuetype; procedure package_k(This : access Typ); private

3.3 Using .NET Classes in Ada

pragma Convention(MSIL,Typ);

Whenever you mix programming languages, it is necessary to have a language binding which enables communication between

pragma MSIL_Constructor(new_Class1); pragma Import(MSIL,get_color,"get_color");

Figure 1. Visual Studio Splash Screen including A#.

pragma Import(MSIL,package_k,"package");

components written in the different languages. These bindings may be either written by hand, or automatically generated. Win32Ada [10] is an example of a handwritten binding to the Microsoft Win32 Application Program Interface (API). The problem with manually writing such a binding is that it is incredibly tedious, and also quickly becomes stale. As the Microsoft Win32 API developed, Win32Ada was not kept up to date.

end SIGAda2006.Class1; pragma Import(MSIL,SIGAda2006.Class1,".ver 1:0:2161:15913", "[SIGAda2006]SIGAda2006.Class1");

In the Ada code above, the compiler directive pragma Import specifies that an item is being imported from a different programming language. Several new Ada 2005 features are used in this binding, making it easier to read than similar bindings written in Ada 95. First, Ada 2005 adds interfaces, which are styled after those in Java and C#. In the definition of Class1, the “and” shows how to specify that a tagged type implements an interface. Oddly, in Ada 2005 only child classes are allowed to implement interfaces. This poses no problems in mapping the C#

A# instead automatically generates bindings via the msil2ada tool. Msil2ada is written in a combination of Ada and C# and uses the .NET reflection classes to enumerate all of the classes, methods and attributes of a .NET DLL, then generates corresponding Ada specifications.

16

Since it is expected that strings will be commonly passed between the languages, A# provides a unary “+” operator to perform this conversion.

types, as all types in C# are derived from System.Object (which does not implement any interfaces). Second, the new “limited with” clause in combination with new anonymous access types makes it easy to map mutually dependent C# classes. A limited with clause is added for each dependent class (as is seen for MSSyst.String).

Csharp_String : MSSyst.String.Ref := +”hello world”;

When calling a C# method that takes a string as a parameter, the compiler will automatically insert the conversion for you:

Reserved words in C# and Ada differ, and C# is case-sensitive while Ada is not, so the pragma Import is used to map C# names to Ada names. Note that since “package” is a keyword in Ada, “_k” is added to its Ada identifier. The namespace System from C# is renamed to MSSyst for similar reasons.

C1: SIGAda2006.Class1.Ref := new_Class1(x => ”hello world”);

Version 2 of the .NET framework adds generics. Using the generics built in to the framework to implement Ada generics reduced code size and made generic Ada constructs visible to other .NET languages.

A# was the first Ada compiler to introduce the object.method syntax which has now become part of the Ada 2005 standard. This means that programmers using both C# and A# can use the same object-oriented syntax for method calls in both languages. In Ada 95, a call to the “package_k” method would have appeared as:

The 2.0 release of .NET includes generic classes and methods. A sample generic class looks like the following in C#: public class Dictionary { public void Add(TKey key, TValue value);

SIGAda2006.Class1.package_k(This => Class1_Ptr);

}

In A# and Ada 2005, this can now be written as:

In A#, msil2ada generates the following:

Class1_Ptr.package_k;

generic

This is not only shorter, but simpler, as the programmer does not need to worry about what package a class was declared in to call a method on it. This is particularly helpful for non-dispatching methods (those declared with ‘Class), as they may be in packages where superclasses were declared.

type TKey is private; type TValue is private; package Dictionary is type Typ is tagged...

On the Java Virtual Machine, there were only base types (such as integer and float) and class references. The Microsoft .NET platform allows for the creation of types that stored on the stack are passed by value (hence the name Valuetype) instead of by heap reference. To resolve this, we have added the reserved word “Valuetype”. The get_color method returns something of type crosstalk.color.Valuetype. If a type is so named, then the compiler will generate code for it in according to the .NET calling conventions for Valuetypes. Since there are no pointers for these type, a full “with” is needed for the crosstalk.color package instead of the “limited with” used for other dependencies.

procedure Add(This : access Typ; key : TKey; value : TValue); end Dictionary;

To instantiate it, you need to do a normal instantiation of the generic, but also specify an import: package MyDictionary is new Dictionary( MSSyst.String.Ref, Integer); pragma Import(MSIL, MyDictionary);

The C# “enum” is another example of a Valuetype. Although, as previously mentioned, it differs from an Ada enumeration type, A# currently maps it to an Ada enumeration. However, since C# “enum” types do not support determining a successor or predecessor, these mapped types can not use Ada enumeration attributes (such as ‘Pos or ‘Succ). Unlike Ada enumeration types, .NET enumerations can have multiple names corresponding to the same value. In these cases a named constant is declared for each additional name. In certain cases, enum values can be combined to create values that have no name (e.g., in the FontStyle enumeration, Bold and Italic are listed—they can be added together to create a Bold Italic style, although this is not listed in the enumeration). When the type allows this, we provide a function “+” for performing such combinations. The C# enum differs so significantly from an Ada enumeration that it would be more accurately mapped to a named integer type (e.g. “type FontStyle is new Integer”). This more precise mapping may be accomplished in a later version.

The pragma Import indicates that the instantiation of the generic does not come from Ada, but instead from an external library. Generic methods are currently not supported. MSIL2Ada does not automatically generate specifications for generic child classes with generic parents.

4. VISUAL STUDIO 2005 Microsoft’s Visual Studio 2005 is a major, mainstream development environment. The full integration of A# into Visual Studio expands Ada 2005 access to developers outside of the Ada community. Adding A# to the Visual Studio environment is surprisingly easy (particularly if the 1st author is on your faculty). Detailed instructions are available at: http://asharp.martincarlisle.com/VisualStudioIntegration.htm

Another key difference between C# and Ada data types is strings. In C#, strings are stored in 16-bit Unicode, while the Ada basic string is 8-bit International Standards Organization (ISO) Latin-1.

Installing A# into Visual Studio is a three step process:

17

1.

Download and unzip mgnat.

2.

Download and run the asharp installer (specifying the folder where you unzipped MGNAT).

3.

Download and run the Visual Studio Integration Installer.

code editor as shown in Figure 4. The Intellisense box will automatically suggest Ada reserved words (e.g. exit, for, procedure), as well as identifiers previously found in the program (e.g. x, y, List_Node). Other advanced editor features such as block commenting and uncommenting, outlining, searching and word completion, et cetera.

The downloads are currently hosted using SourceForge. A link to the download site is found at http://asharp.martincarlisle.com. The source code for msil2ada is also available at this site, but it is not required for using the software. Once Visual Studio is started, it is simple to create an Ada for .NET (A#) project as shown in Figure 2 below.

Figure 2. Selecting A# for a new project.

Figure 4. Intellisense support for A# in Visual Studio. The application templates use GNAT project files to control the build process (e.g. applicationProject.gpr). Novice users should not need to modify anything in these files; advanced users can use this file to specify search folders, compilation flags, etc. Building and running an A# project is done exactly as you would a C# project, and error messages and warnings appear in the same window. Double-clicking on one of these errors will bring the cursor directly to the line containing the error.

4.1 RAPTOR: A Major A# Project A significant example of using A# is RAPTOR [11]. RAPTOR, the Rapid Algorithmic Prototyping Tool for Ordered Reasoning, was designed specifically to address the shortcomings of syntactic difficulties and non-visual environments. RAPTOR allows students to create algorithms by combining basic flowchart symbols. Students can then run their algorithms in the environment, either step-by-step or in continuous play mode. The environment visually displays the location of the currently executing flowchart symbol, as well as the contents of all variables. Also, RAPTOR provides a simple graphics library, based on AdaGraph [9]. Not only can the students create algorithms visually, but also the solutions can be visualized as shown in Figure 5.

Figure 3. Ada 2005 source code in Visual Studio 2005. As can be seen in Figure 3, using A# in Visual Studio 2005 is straightforward. For someone who has not written any Ada code in many years, it was very easy to immediately start writing, compiling and executing Ada programs. Visual Studio has many useful features, auto-indent, formatting and intellisense. Intellisense is a Microsoft trademark, it is a drop down list that appears when you type a recognized word into your

18

generation of standalone executables and procedures with parameters. More information on RAPTOR as well as papers and the program itself is available for downloading at: http://raptor.martincarlisle.com/.

4.2 Debugging in A# The A# compiler generates debugging information which allows use of the debugger in the Visual Studio environment. The debugger is a robust debugger, though as yet, there are no specific help files included for A# in the Visual Studio environment. In Figure 6, we can see a simple loop with multiple break points set in it. In the lower right there is a fully functional and convenient command window and in the lower left we can see the representations of the local variables. The debugger allows setting breakpoints, single stepping, viewing local variables (including complex data structures), and traversing the call stack.

5. CONCLUSIONS A# has demonstrated the viability and usefulness of combining Ada with other .NET languages, as well as providing interfacing to legacy Win32 and COM libraries. This allows developers to gain the advantages of Ada’s strong typing, while also leveraging the vast number of libraries available with .NET and the GUI builder from Visual Studio. Furthermore, A# provides an easy mechanism for getting Ada code running on embedded and mobile devices via the .NET compact framework (e.g. Windows CE, Pocket PC, Smartphone 2003).

Figure 5. An example of a RAPTOR flowchart. RAPTOR provides a simple environment for students to experiment with developing algorithms. Instructors can customize the environment and facilitate more interesting exercises by adding to the built-in procedures. The latest version of RAPTOR supports generation of Ada and C# stub code,

Figure 6. A# Program utilizing the debugger.

19

[4] Braude, E. Software Design, John Wiley and Sons, 2004, p.429.

Since Visual Studio 2005 allows extensions to be written in .NET languages (Visual Studio 2003 required the extension to be written using C++ COM objects), the integration code itself could be written in A#. Finally, while A# has been maintained as an academic project (even though it is in use by defense contractors), it would be preferable to perform technology transfer to the private sector, which has greater resources to develop and maintain this product.

[5] “About Languages: Welcome to the .NET Language Developers Group.” Gotdotnet.com. October 2004. Microsoft. 29 Nov 2005 . [6] Comar, Cyrille, Gary Dismukes, and Franco Gasperoni. “Targeting GNAT to the Java Virtual Machine.” Proceedings of the Tri-Ada 97 Conference. [St Louis MO] Nov 9 – 13, 1997.

In the meantime, Ada 2005 has been successfully integrated into a mainstream IDE – this can only assist in the acceptance and usage of Ada.

[7] “The Libre Site for Free Software Developers.” Ada Core Technologies. 29 Nov 2005

6. ACKNOWLEDGEMENT Sincere thanks to Phil Taylor and Allen Denver of Microsoft for their technical assistance on this project.

[8] Carlisle, Martin. “AdaGIDE Home Page.” 21 May 2006 .

7. REFERENCES

[9] vanDijk, J. AdaGraph. Online. Internet. Available: http://users.ncrvnet.nl/gmvdijk/adagraph.html.

[1] Carlisle, M.C., Sward, R.E., and Humphries, J.W., Weaving Ada 95 into the .Net Environment, SIGAda 2002, Houston, TX, December 8-12, 2002.

[10] Taft, S. Tucker. “Win32Ada.” 22 June 1999. Averstar and Labtek. 29 Nov 2005 < http://archive.adaic.com/tools /bindings/win32ada/win32ada.html>.

[2] Ada and Beyond, Software Policies for the Department of Defense, National Research Council, National Academy Press, Washington, D.C., 1997.

[11] Carlisle, Martin. “RAPTOR Home Page.” 7 June 2006 .

[3] Carlisle, M.C. Ada 2005 on .NET and Mobile and Embedded Devices, to appear in: Crosstalk: The Journal of Defense Software Engineering, Hill AFB, UT, 2006.

20

Suggest Documents