System and method for inferring immutability of program variables
First Claim
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.
1 Assignment
0 Petitions
Accused Products
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.
30 Citations
9 Claims
-
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. - View Dependent Claims (2, 3)
-
-
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. - View Dependent Claims (5, 6)
-
-
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. - View Dependent Claims (8, 9)
-
Specification