IP Library Granted Patent US 9,542,166
Granted Patent B2
US 9,542,166 · App. 13/677,000 · Granted Jan 10, 2017

System and method for inferring immutability of program variables

Inventors: Brian Goetz (Williston, VT); Alexander Buckley (Cupertino, CA); Daniel Smith (Spanish Fork, UT); Maurizio Cimadamore (Dublin, IE)
Assignee: ORACLE INTERNATIONAL CORPORATION
G06F8/41G06F8/30G06F8/447G06F11/3672
View Patent ↗
Loading inventors, assignments & file history…
Monitor This Case
Get email alerts when status or documents change.
Order Certified Copies
Most orders are placed with the USPTO same day — all within 24 business hours.
Order via The Patent Place →
Pre-filled with this patent's details
Quick Facts
Patent No.
US 9,542,166
App. No.
13/677,000
Granted
Jan 10, 2017
Kind
B2
Abstract

In accordance with various embodiments, systems and methods are provided which facilitate inferring immutability of variables. A compiler analyzes local variables within source code to determine whether they are immutable. In particular embodiments the compiler examines locations where each variable is assigned to determine whether the variable was definitely unassigned before the assignment. Because the compiler can infer whether a local variable is immutable, it is possible for the programmer to avoid using a keyword to expressly declare the local variable as immutable. Inferring immutability of variables, thus, maintains correctness of the compiled code while reducing the burden on the programmer.

Claims (34)

1. A computer-implemented method for compiling source code using a compiler operating on one or more microprocessor, the method comprising:

receiving said source code wherein said source code includes a plurality of local variables wherein each of said plurality of local variables is expressed in said source code without the use of a keyword which expressly declares said each of said plurality of local variables as being immutable;

identifying with the compiler each of said plurality of local variables in said source code;

for each local variable of the plurality of said plurality of local variables:

identifying one or more locations in the source code where said each local variable is assigned;

if said local variable has an initializer, inferring that said local variable is immutable if said local variable never occurs as an operand of a pre-increment, pre-decrement, post-increment, or post-decrement operator in said source code and never occurs as a left-hand operator of an assignment operator in said source code; and

if said local variable has no initializer, inferring that said local variable is immutable if said local variable never occurs as an operand of a pre-increment, pre-decrement, post-increment, or post-decrement operator in said source code, and at any location is said source code where said local variable occurs as a left-hand operator of an assignment operator said local variable is definitely unassigned before being assigned; and

compiling said source code with the compiler to generate bytecode, wherein the compiler treats, during compilation, all of said plurality of local variables inferred as immutable in the same manner as a local variable expressly declared to be immutable.

2. The method of claim 1 , further comprising:

throwing an error during said compiling for each of said plurality of local variables unless said each of the plurality of local variables is inferred to be immutable or is not required to be immutable.

3. The method of claim 1 , wherein identifying each of said plurality of local variables in the source code comprises identifying each of said plurality of local variables in the source code wherein said each of the plurality of local variables is at least one of a method parameter, constructor parameter, lambda parameter, and exception parameter.

4. A non-transitory computer readable storage medium including instructions stored thereon for compiling source code, which instructions, when executed by a computer, cause the computer to perform steps comprising:

receiving said source code wherein said source code includes a plurality of local variables wherein each of said plurality of local variables is expressed in said source code without the use of a keyword which expressly declares said each of said plurality of local variables as being immutable;

identifying with the compiler each of said plurality of local variables in said source code;

for each local variable of the plurality of said plurality of local variables:

identifying one or more locations in the source code where said each local variable is assigned;

if said local variable has an initializer, inferring that said local variable is immutable if said local variable never occurs as an operand of a pre-increment, pre-decrement, post-increment, or post-decrement operator in said source code and never occurs as a left-hand operator of an assignment operator in said source code; and

if said local variable has no initializer, inferring that said local variable is immutable if said local variable never occurs as an operand of a pre-increment, pre-decrement, post-increment, or post-decrement operator in said source code, and at any location is said source code where said local variable occurs as a left-hand operator of an assignment operator said local variable is definitely unassigned before being assigned; and

compiling said source code with the compiler to generate bytecode, wherein the compiler treats, during compilation, all of said plurality of local variables inferred as immutable in the same manner as a local variable expressly declared to be immutable.

5. The non-transitory computer readable storage medium of claim 4 , wherein the instructions stored thereon, when executed by a computer, cause the computer to perform further steps comprising:

throwing an error for each identified local variable of said plurality of local variables unless each identified local variable is inferred to be immutable or the identified local variable is not required to be final.

6. The non-transitory computer readable storage medium of claim 4 , wherein identifying each of said plurality of local variables in the source code comprises:

identifying each of said plurality of local variables in the source code wherein the local variable is at least one of a method parameter, constructor parameter, lambda parameter, and exception parameter.

7. A system for compiling source code, the system comprising:

a computer system comprising one or more microprocessor and memory;

a compiler operating on said computer system, wherein said compiler is configured to receive said source code wherein said source code includes a plurality of local variables wherein each of said plurality of local variables is expressed in said source code without the use of a keyword which expressly declares said each of said plurality of local variables as being immutable;

a variable analyzer component of said compiler, wherein said variable analyzer is configured to identify each of said plurality of local variables in said source code, and for each local variable of the plurality of said plurality of local variables:

identify one or more locations in the source code where said each local variable is assigned;

if said local variable has an initializer, infer that said local variable is immutable if said local variable never occurs as an operand of a pre-increment, pre-decrement, post-increment, or post-decrement operator in said source code and never occurs as a left-hand operator of an assignment operator in said source code, and

if said local variable has no initializer, infer that said local variable is immutable if said local variable never occurs as an operand of a pre-increment, pre-decrement, post-increment, or post-decrement operator in said source code, and at any location is said source code where said local variable occurs as a left-hand operator of an assignment operator said local variable is definitely unassigned before being assigned; and

wherein said compiler is further configured to compile said source code to generate bytecode, wherein the compiler treats, during compilation, all of said plurality of local variables inferred as immutable in the same manner as a local variable expressly declared to be immutable.

8. The system of claim 7 , wherein:

the compiler comprises an error handler which throws an error during compilation of said source code for each local variable of said plurality of local variables which is not identified as immutable by the variable analyzer unless the local variable is not required to be final.

9. The system of claim 7 , wherein each of the plurality of local variables include one or more of method parameters, constructor parameters, lambda parameters, and exception parameters.

Assignments (1)
ASSIGNMENT OF ASSIGNOR'S INTEREST Recorded Nov 19, 2012
From: BUCKLEY, ALEXANDER; CIMADAMORE, MAURIZIO; GOETZ, BRIAN; SMITH, DANIEL
To: ORACLE INTERNATIONAL CORPORATION
Reel/Frame 029319/0213 →
Continuity (2)
Provisional Application 61720314 · Oct 30, 2012
Related Publication 20140123118A1 · May 1, 2014