DOC PREVIEW
UW-Madison CS 536 - Packages and Import

This preview shows page 1-2-19-20 out of 20 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 20 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 20 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 20 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 20 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 20 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

358CS 536 Spring 2007©Packages and ImportsJava allows packages which groupclass and interface definitions intonamed units.A package requires a symbol tableto access members. Thus areferencejava.util.Vectorlocates the package java.util(typically using a CLASSPATH) andlooks up Vector within it.Java supports import statementsthat modify symbol table lookuprules.A single class import, likeimport java.util.Vector;brings the name Vector into thecurrent symbol table (unless a359CS 536 Spring 2007©definition of Vector is alreadypresent).An “import on demand” likeimport java.util.*;will lookup identifiers in thenamed packages after explicituser declarations have beenchecked.360CS 536 Spring 2007©Classfiles and Object FilesClass files (“.class” files, producedby Java compilers) and object files(“.o” files, produced by C and C++compilers) contain internalsymbol tables.When a field or method of a Javaclass is accessed, the JVM usesthe classfile’s internal symboltable to access the symbol’s valueand verify that type rules arerespected.When a C or C++ object file islinked, the object file’s internalsymbol table is used to determinewhat external names arereferenced, and what internallydefined names will be exported.361CS 536 Spring 2007©C, C++ and Java all allow users torequest that a more completesymbol table be generated fordebugging purposes. This makesinternal names (like local variable)visible so that a debugger candisplay source level informationwhile debugging.362CS 536 Spring 2007©OverloadingA number of programminglanguages, including Java andC++, allow method andsubprogram names to beoverloaded.This means several methods orsubprograms may share the samename, as long as they differ in thenumber or types of parametersthey accept. For example,class C { int x; public static int sum(int v1, int v2) { return v1 + v2; } public int sum(int v3) { return x + v3; }}363CS 536 Spring 2007©For overloaded identifiers thesymbol table must return a list ofvalid definitions of the identifier.Semantic analysis (type checking)then decides which definition touse.In the above example, whilechecking(new C()).sum(10);both definitions of sum arereturned when it is looked up.Since one argument is provided,the definition that uses oneparameter is selected andchecked.A few languages (like Ada) allowoverloading to be disambiguatedon the basis of a method’s resulttype. Algorithms that do thisanalysis are known, but are fairlycomplex.364CS 536 Spring 2007©Overloaded OperatorsA few languages, like C++, allowoperators to be overloaded.This means users may add newdefinitions for existing operators,though they may not create newoperators or alter existingprecedence and associativityrules.(Such changes would forcechanges to the scanner or parser.)For example,class complex{float re, im;complex operator+(complex d){complex ans;ans.re = d.re+re;ans.im = d.im+im;return ans;} }complex c,d; c=c+d;365CS 536 Spring 2007©During type checking of anoperator, all visible definitions ofthe operator (including predefineddefinitions) are gathered andexamined.Only one definition shouldsuccessfully pass type checks.Thus in the above example, theremay be many definitions of +, butonly one is defined to takecomplex operands.366CS 536 Spring 2007©Contextual ResolutionOverloading allows multipledefinitions of the same kind ofobject (method, procedure oroperator) to co-exist.Programming languages alsosometimes allow reuse of thesame name in defining differentkinds of objects. Resolution is bycontext of use.For example, in Java, a class namemay be used for both the classand its constructor. Hence we seeC cvar = new C(10);In Pascal, the name of a functionis also used for its return value.Java allows rather extensive reuseof an identifier, with the sameidentifier potentially denoting aclass (type), a class constructor, a367CS 536 Spring 2007©package name, a method and afield.For example,class C {double v;C(double f) {v=f;}}class D {int C;double C() {return 1.0;}C cval = new C(C+C());}At type-checking time weexamine all potential definitionsand use that definition that isconsistent with the context ofuse. Hence new C() must be aconstructor, +C() must be afunction call, etc.368CS 536 Spring 2007©Allowing multiple definitions toco-exist certainly makes typechecking more complicated thanin other languages.Whether such reuse benefitsprogrammers is unclear; itcertainly violates Java’s “keep itsimple” philosophy.369CS 536 Spring 2007©Type and Kind Information inCSXIn CSX symbol table entries and inAST nodes for expressions, it isuseful to store type and kindinformation.This information is created andtested during type checking. Infact, most of type checkinginvolves deciding whether thetype and kind values for thecurrent construct and itscomponents are valid.Possible values for type include:• Integer (int)• Boolean (bool)• Character (char)• String370CS 536 Spring 2007©• VoidVoid is used to represent objects thathave no declared type (e.g., a label orprocedure).• ErrorError is used to represent objectsthat should have a type, but don’t(because of type errors).Error typessuppress further type checking,preventing cascaded error messages.• UnknownUnknown is used as an initial value,before the type of an object isdetermined.371CS 536 Spring 2007©Possible values for kindinclude:• Var (a local variable or field thatmay be assigned to)• Value (a value that may be readbut not changed)• Array• ScalarParm (a by-value scalarparameter)• ArrayParm (a by-reference arrayparameter)• Method (a procedure or function)• Label (on a while loop)372CS 536 Spring 2007©Most combinations of type andkind represent something in CSX.Hence type==Boolean andkind==Value is a bool constantor expression.type==Void and kind==Methodis a procedure (a method thatreturns no value).Type checking procedure andfunction declarations and callsrequires some care.When a method is declared, youshould build a linked list of(type,kind) pairs, one for eachdeclared parameter.When a call is type checked youshould build a second linked listof (type,kind) pairs for theactual parameters of the call.373CS 536 Spring 2007©You compare the lengths of the listof formal and actual parameters tocheck that the correct number ofparameters has been passed.You then compare correspondingformal and actual parameter pairsto check if each individual actualparameter correctly matches itscorresponding formal parameter.For


View Full Document

UW-Madison CS 536 - Packages and Import

Download Packages and Import
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Packages and Import and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Packages and Import 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?