Names, Binding, Scope, Type Checking
Total Page:16
File Type:pdf, Size:1020Kb
CSC 311 SURVEY OF PROGRAMMING LANGUAGES Declarations and Types- The concept of types, Declaration models (binding, visibility, scope, and lifetime), Overview of type-checking LECTURE EIGHT DISCLAIMER The contents of this document are intended for leaning purposes at the undergraduate level. The materials are from different sources including the internet and the contributors do not in any way claim authorship or ownership of them. -DECLARATIONS AND TYPES -DECLARATION MODELS(CHARACTERISTICS OF VARIABLES) In computer programming, a declaration is a language construct that specifies properties of an identifier: It declares what a word (identifier) "means". Declarations are commonly used for functions, variables, constants, and classes, but can also be used for other entities such as type definitions. Beyond the name (the identifier itself) and the kind of entity (function, variable, etc.), declarations typically specify the data type (for variables and constants), or the type signature (for functions); types may also include dimensions, such as arrays. A declaration is used to announce the existence of the entity to the compiler; this is important in those strongly typed languages that require functions, variables, and constants, and their types to be specified with a declaration before use, and is used in forward declaration. The term "declaration" is frequently contrasted with the term "definition", but meaning and usage varies significantly between languages. Declarations are particularly prominent in languages SUCH AS ALGOL, BCPL family, most prominently C and C++, and also Pascal. Java uses the term "declaration", though Java does not have separate declarations and definitions. Declaration vs. definition A basic dichotomy is whether a declaration contains a definition or not: for example, whether a declaration of a constant or variable specifies the value of the constant (respectively, initial value of a variable), or only its type; and similarly whether a declaration of a function specifies the body (implementation) of the function, or only its type. Not all languages make this distinction: in many languages, declarations always include a definition, and may be referred to as either "declarations" or "definitions", depending on the language. However, these concepts are distinguished in languages that require declaration before use (for which forward declarations are used), and in languages where interface and implementation are separated: the interface contains declarations, the implementation contains definitions. In informal usage, a "declaration" refers only to a pure declaration (types only, no value or body), while a "definition" refers to a declaration that includes a value or body. However, in formal usage (in language specifications), "declaration" includes both of these senses, with finer distinctions by language: in C and C++, a declaration of a function that does not include a body is called a function prototype, while a declaration of a function that does include a body is called a "function definition". By contrast in Java declarations always include the body, and the word "definition" has no technical meaning in Java. Here are some examples of declarations that are not definitions, in C: extern char example1; extern int example2; void example3(void); Here are some examples of declarations that are definitions, again in C: char example1; /* Outside of a function definition it will be initialized to zero. */ int example2 = 5; void example3(void) { /* definition between braces */ } TYPE Sizes of fundamental Types are: Type Storage char, unsigned char, signed 1 byte char short, unsigned short 2 bytes int, unsigned int 4 bytes long, unsigned long 4 bytes float 4 bytes double 8 bytes long double 8 bytes The C data types fall into general categories. The "integral types" include char, int, short, long, signed, unsigned, and enum. The "floating types" include float, double, and long double. The "arithmetic types" include all floating and integral types. SUMMARY A declaration specifies the interpretation and attributes of a set of identifiers. A definition of an identifier is a declaration for that identifier that: for an object [variable or constant], causes storage to be reserved for that object; for a function, includes the function body; for an enumeration constant, is the (only) declaration of the identifier; for a typedef name, is the first (or only) declaration of the identifier." Declaration Models • Names, variables • Binding • Scope - Visibility • Constants. Variable initialization Names • A name is a handle on an entity in a program. We refer to something by a name if we want to create, use, change, destroy it. • Declaration, use, lifetime, scope of names: these properties are a major consideration in programming languages. What needs a name? • constants, variables, • operators, • statement labels, • types, • procedures, functions, methods, • modules, programs, • files, disks, • commands, menu items, • computers, networks, user (login name). Names and identifiers • A name is denoted by an identifier. • Typical identifiers are built of letters, digits and underscores, but some languages allow "funny" identifiers as well. Prolog, Scheme and Perl are in this category. Variables • Variables are abstractions for memory cells of the computer. Variables attributes are name, address, value, type, lifetime, scope For example, if we write int x; we decide what will be the name and type of x. • The place of this declaration in the program decides where and how long x is available (scope, lifetime). Its address is determined when its program unit is executing, Finally, using x in statements decides what is its current value. Variables • A problem in most programming languages: the same name can be reused in different contexts and denote different entities. For example: void a() { int b; /* ... */ } float b() { char a; /* ... */ } • Different types, addresses and values may be associated with such occurrences of a name. Each occurrence has a different lifetime (when and for how long is an entity created?), and a different scope (where can the name be used?). Values of variables • The concept of value is more general: l-value (l for left) and r-value (r for right) are the source and target of an assignment. An example: x = y; • l-value is the address of x, r-value is the value of y. This becomes complicated for array elements that denote addresses which must be evaluated: T[i*2+1] = y; • This address depends on the current value of i. Undefined variables In some programming languages, an implicit declaration is provided the first time such a variable is encountered at compile time. In other languages, such a usage is considered to be an error, which may resulting in a diagnostic message. Some languages have started out with the implicit declaration behavior, but as they matured they provided an option to disable it (e.g. Perl's "use strict" , Visual Basic's use "Option Explicit"). How to Declare Variables in Java To reference a variable in a Java program, you must first declare it. To declare a variable, follow these four steps. Open your text editor and create a new file. Type in the following Java statements: Two variables are declared. The first variable is of type int. A value is established in a separate Java statement. The second variable is of type String and is declared with an initial value. 2. Save your file as DeclareVariablesInJava.java. 3. Now run your Java program Binding • An informal presentation • Binding times • Bindings of a variable • Lifetime Binding Binding is not formally defined. Intuitively, we can say that it is (that is, binding) an attribute with an entity. Examples of attributes are name, type, value. Binding occurs at various times in the life of a program. Three periods are usually considered: compile time (actually, translation time, because binding happens both in compilers and interpreters); load time (preparing object code for execution, pulling in the necessary code for built-in operations or operations defined in libraries of modules); run time (between starting the program's execution and its termination). Static and dynamic binding We also distinguish two kinds of binding, depending on its duration: static binding is permanent during the life of the program; dynamic binding is in force during some part of the program's life. Assigning properties to variables • variable name • compile time – described in declarations • variable address • load time or run time (e.g. C), • run time (e.g. Smalltalk) – this is usually done implicitly • variable type • compile time (e.g. Java), • run time (e.g. Scheme) – described in declarations, if bound at compile time Assigning properties... (2) • variable value • run time, • load time (initialization) – specified in statements, mainly assignment • variable lifetime • compile time – described in declarations • variable scope • compile time – expressed by placement of declarations Lifetime • Allocation of memory for an entity happens at load time or at run time. Two classes of variables are distinguished: • Static variables • Dynamic variables Static variables • Memory for static variables is allocated once, before the program begins execution. • Fortran IV was an important language with such an allocation policy for all variables. This was inflexible, but it also was conceptually simple and inexpensive. Recursion was not possible. Dynamic variables • Allocation is done after the program has started. • Two possibilities of dynamic allocation.