| File: | src/java/javacode.c |
| Warning: | line 1306, column 2 Value stored to 'aClss' is never read |
Press '?' to see keyboard shortcuts
Keyboard shortcuts:
| 1 | #include "javacode.h" |
| 2 | #include "javaobj.h" |
| 3 | #include "sexpr.h" |
| 4 | #include "util.h" |
| 5 | |
| 6 | enum jc_clss_enum { |
| 7 | JCO_CLSS_START, |
| 8 | JCO_CLSS_String = JCO_CLSS_START, |
| 9 | JCO_CLSS_Character, |
| 10 | JCO_CLSS_Integer, |
| 11 | JCO_CLSS_Float, |
| 12 | JCO_CLSS_Double, |
| 13 | JCO_CLSS_Keyword, |
| 14 | JCO_CLSS_Id, |
| 15 | JCO_CLSS_CommaSeq, |
| 16 | JCO_CLSS_SpaceSeq, |
| 17 | JCO_CLSS_NLSeq, |
| 18 | JCO_CLSS_Seq, |
| 19 | JCO_CLSS_Parens, |
| 20 | JCO_CLSS_Braces, |
| 21 | JCO_CLSS_SqBrackets, |
| 22 | JCO_CLSS_ABrackets, |
| 23 | JCO_CLSS_ImportedId, |
| 24 | JCO_CLSS_ImportedStatic, |
| 25 | JCO_CLSS_Annotation, |
| 26 | JCO_CLSS_Class, |
| 27 | JCO_CLSS_JavaDoc, |
| 28 | JCO_CLSS_Comment, |
| 29 | JCO_CLSS_Method, |
| 30 | JCO_CLSS_Declaration, |
| 31 | JCO_CLSS_Statement, |
| 32 | JCO_CLSS_File, |
| 33 | |
| 34 | JCO_CLSS_If, |
| 35 | JCO_CLSS_While, |
| 36 | JCO_CLSS_Switch, |
| 37 | JCO_CLSS_Case, |
| 38 | JCO_CLSS_Block, |
| 39 | JCO_CLSS_Try, |
| 40 | JCO_CLSS_Catch, |
| 41 | JCO_CLSS_Finally, |
| 42 | |
| 43 | JCO_CLSS_ArrRef, |
| 44 | JCO_CLSS_MemRef, |
| 45 | JCO_CLSS_Cast, |
| 46 | |
| 47 | JCO_CLSS_Apply, |
| 48 | JCO_CLSS_Construct, |
| 49 | |
| 50 | JCO_CLSS_Not, |
| 51 | JCO_CLSS_LogAnd, |
| 52 | JCO_CLSS_LogOr, |
| 53 | JCO_CLSS_And, |
| 54 | JCO_CLSS_Or, |
| 55 | JCO_CLSS_XOr, |
| 56 | JCO_CLSS_Equals, |
| 57 | JCO_CLSS_NEquals, |
| 58 | JCO_CLSS_Assign, |
| 59 | JCO_CLSS_Plus, |
| 60 | JCO_CLSS_Minus, |
| 61 | JCO_CLSS_Times, |
| 62 | JCO_CLSS_Divide, |
| 63 | JCO_CLSS_Modulo, |
| 64 | JCO_CLSS_LT, |
| 65 | JCO_CLSS_LE, |
| 66 | JCO_CLSS_GT, |
| 67 | JCO_CLSS_GE, |
| 68 | JCO_CLSS_Negate, |
| 69 | JCO_CLSS_ShiftUp, |
| 70 | JCO_CLSS_ShiftDn, |
| 71 | |
| 72 | JCO_CLSS_Conditional, |
| 73 | JCO_CLSSS_END |
| 74 | }; |
| 75 | |
| 76 | enum JavaKeywordTag { |
| 77 | JK_Abstract, |
| 78 | JK_Assert, |
| 79 | JK_Boolean, |
| 80 | JK_Break, |
| 81 | JK_Byte, |
| 82 | JK_Case, |
| 83 | JK_Catch, |
| 84 | JK_Char, |
| 85 | JK_Class, |
| 86 | JK_Const, |
| 87 | JK_Continue, |
| 88 | JK_Default, |
| 89 | JK_Do, |
| 90 | JK_Double, |
| 91 | JK_Else, |
| 92 | JK_Enum, |
| 93 | JK_Extends, |
| 94 | JK_Final, |
| 95 | JK_Finally, |
| 96 | JK_Float, |
| 97 | JK_For, |
| 98 | JK_Goto, |
| 99 | JK_If, |
| 100 | JK_Implements, |
| 101 | JK_Import, |
| 102 | JK_Instanceof, |
| 103 | JK_Int, |
| 104 | JK_Interface, |
| 105 | JK_Long, |
| 106 | JK_Native, |
| 107 | JK_New, |
| 108 | JK_Package, |
| 109 | JK_Private, |
| 110 | JK_Protected, |
| 111 | JK_Public, |
| 112 | JK_Return, |
| 113 | JK_Short, |
| 114 | JK_Static, |
| 115 | JK_Strictfp, |
| 116 | JK_Super, |
| 117 | JK_Switch, |
| 118 | JK_Synchronized, |
| 119 | JK_This, |
| 120 | JK_Throw, |
| 121 | JK_Throws, |
| 122 | JK_Transient, |
| 123 | JK_Try, |
| 124 | JK_Void, |
| 125 | JK_Volatile, |
| 126 | JK_While, |
| 127 | JK_False, |
| 128 | JK_Null, |
| 129 | JK_True, |
| 130 | JK_END |
| 131 | }; |
| 132 | |
| 133 | |
| 134 | typedef struct javaKeyword { |
| 135 | int id; |
| 136 | String text; |
| 137 | } *JavaKeyword; |
| 138 | |
| 139 | struct javaKeyword jkKeywords[] = { |
| 140 | { JK_Abstract, "abstract" }, |
| 141 | { JK_Assert, "assert" }, |
| 142 | { JK_Boolean, "boolean" }, |
| 143 | { JK_Break, "break" }, |
| 144 | { JK_Byte, "byte" }, |
| 145 | { JK_Case, "case" }, |
| 146 | { JK_Catch, "catch" }, |
| 147 | { JK_Char, "char" }, |
| 148 | { JK_Class, "class" }, |
| 149 | { JK_Const, "const" }, |
| 150 | { JK_Continue, "continue" }, |
| 151 | { JK_Default, "default" }, |
| 152 | { JK_Do, "do" }, |
| 153 | { JK_Double, "double" }, |
| 154 | { JK_Else, "else" }, |
| 155 | { JK_Enum, "enum" }, |
| 156 | { JK_Extends, "extends" }, |
| 157 | { JK_Final, "final" }, |
| 158 | { JK_Finally, "finally" }, |
| 159 | { JK_Float, "float" }, |
| 160 | { JK_For, "for" }, |
| 161 | { JK_Goto, "goto" }, |
| 162 | { JK_If, "if" }, |
| 163 | { JK_Implements, "implements" }, |
| 164 | { JK_Import, "import" }, |
| 165 | { JK_Instanceof, "instanceof" }, |
| 166 | { JK_Int, "int" }, |
| 167 | { JK_Interface, "interface" }, |
| 168 | { JK_Long, "long" }, |
| 169 | { JK_Native, "native" }, |
| 170 | { JK_New, "new" }, |
| 171 | { JK_Package, "package" }, |
| 172 | { JK_Private, "private" }, |
| 173 | { JK_Protected, "protected" }, |
| 174 | { JK_Public, "public" }, |
| 175 | { JK_Return, "return" }, |
| 176 | { JK_Short, "short" }, |
| 177 | { JK_Static, "static" }, |
| 178 | { JK_Strictfp, "strictfp" }, |
| 179 | { JK_Super, "super" }, |
| 180 | { JK_Switch, "switch" }, |
| 181 | { JK_Synchronized, "synchronized" }, |
| 182 | { JK_This, "this" }, |
| 183 | { JK_Throw, "throw" }, |
| 184 | { JK_Throws, "throws" }, |
| 185 | { JK_Transient, "transient" }, |
| 186 | { JK_Try, "try" }, |
| 187 | { JK_Void, "void" }, |
| 188 | { JK_Volatile, "volatile" }, |
| 189 | { JK_While, "while" }, |
| 190 | { JK_False, "false" }, |
| 191 | { JK_Null, "null" }, |
| 192 | { JK_True, "true" }, |
| 193 | }; |
| 194 | |
| 195 | typedef Enum(jc_clss_enum)enum jc_clss_enum JcClassId; |
| 196 | |
| 197 | localstatic JWriteFn jcAnnotationPrint; |
| 198 | localstatic JWriteFn jcApplyPrint; |
| 199 | localstatic JWriteFn jcARefPrint; |
| 200 | localstatic JWriteFn jcBinOpPrint; |
| 201 | localstatic JWriteFn jcBlockHdrPrint; |
| 202 | localstatic JWriteFn jcBlockKeywordPrint; |
| 203 | localstatic JWriteFn jcBlockPrint; |
| 204 | localstatic JWriteFn jcCasePrint; |
| 205 | localstatic JWriteFn jcCastPrint; |
| 206 | localstatic JWriteFn jcClassPrint; |
| 207 | localstatic JWriteFn jcCommentPrint; |
| 208 | localstatic JWriteFn jcCondPrint; |
| 209 | localstatic JWriteFn jcConstructPrint; |
| 210 | localstatic JWriteFn jcDeclarationPrint; |
| 211 | localstatic JWriteFn jcIdPrint; |
| 212 | localstatic JWriteFn jcImportPrint; |
| 213 | localstatic JWriteFn jcIntegerPrint; |
| 214 | localstatic JWriteFn jcJavaDocPrint; |
| 215 | localstatic JWriteFn jcKeywordPrint; |
| 216 | localstatic JWriteFn jcMethodPrint; |
| 217 | localstatic JWriteFn jcParenPrint; |
| 218 | localstatic JWriteFn jcSequencePrint; |
| 219 | localstatic JWriteFn jcStatementPrint; |
| 220 | localstatic JWriteFn jcFilePrint; |
| 221 | localstatic JWriteFn jcStringPrint; |
| 222 | localstatic JWriteFn jcUnaryOpPrint; |
| 223 | |
| 224 | localstatic JSExprFn jcCommentSExpr; |
| 225 | localstatic JSExprFn jcIdSExpr; |
| 226 | localstatic JSExprFn jcImportSExpr; |
| 227 | localstatic JSExprFn jcIntegerSExpr; |
| 228 | localstatic JSExprFn jcKeywordSExpr; |
| 229 | localstatic JSExprFn jcStringSExpr; |
| 230 | |
| 231 | localstatic JavaCodeList jc0CreateModifiers(int modifiers); |
| 232 | localstatic void jc0PrintWithParens(JavaCodePContext ctxt, JavaCodeClass oclss, JavaCode arg); |
| 233 | localstatic Bool jc0NeedsParens(JavaCodeClass c1, JavaCodeClass c2); |
| 234 | localstatic String jc0EscapeString(String s, Bool terminal); |
| 235 | localstatic Bool jcBlockHdrIndent(JavaCode code); |
| 236 | localstatic JavaCode jcBinaryOp(JavaCodeClass c, JavaCode lhs, JavaCode rhs); |
| 237 | |
| 238 | /* |
| 239 | Operator Description Associativity |
| 240 | 15. () Parentheses (grouping) left-to-right |
| 241 | 14. ++ -- Unary postincrement/postdecrement right-to-left |
| 242 | 13 ++ -- Unary preincrement/predecrement right-to-left |
| 243 | + - Unary plus/minus |
| 244 | ! ~ Unary logical negation/bitwise complement |
| 245 | (type) Unary cast (change type) |
| 246 | 12. * / % Multiplication/division/modulus left-to-right |
| 247 | 11. + - Addition/subtraction left-to-right |
| 248 | 10. << >> Bitwise shift left, Bitwise shift right left-to-right |
| 249 | 9. < <= Relational less than/less than or equal to left-to-right |
| 250 | > >= Relational greater than/greater than or equal to |
| 251 | instanceof Type comparison |
| 252 | 8 == != Relational is equal to/is not equal to left-to-right |
| 253 | 7 & Bitwise AND left-to-right |
| 254 | 6 ^ Bitwise exclusive OR left-to-right |
| 255 | 5. | Bitwise inclusive OR left-to-right |
| 256 | 4. && Logical AND left-to-right |
| 257 | 3. || Logical OR left-to-right |
| 258 | 2. ?: Ternary conditional right-to-left |
| 259 | 1. = Assignment right-to-left |
| 260 | += -= Addition/subtraction assignment |
| 261 | *= /= Multiplication/division assignment |
| 262 | %= &= Modulus/bitwise AND assignment |
| 263 | ^= |= Bitwise exclusive/inclusive OR assignment |
| 264 | <<= >>= Bitwise shift left/right assignment |
| 265 | |
| 266 | */ |
| 267 | static struct jclss jcClss[] = { |
| 268 | { JCO_CLSS_String, jcStringPrint, jcStringSExpr, "string", "\""}, |
| 269 | { JCO_CLSS_Character, jcStringPrint, jcStringSExpr, "char", "'"}, |
| 270 | { JCO_CLSS_Integer, jcIntegerPrint, jcIntegerSExpr, "integer"}, |
| 271 | { JCO_CLSS_Float, jcIntegerPrint, jcIntegerSExpr, "float"}, |
| 272 | { JCO_CLSS_Double, jcIntegerPrint, jcIntegerSExpr, "double"}, |
| 273 | { JCO_CLSS_Keyword, jcKeywordPrint, jcKeywordSExpr, "keyword"}, |
| 274 | { JCO_CLSS_Id, jcIdPrint, jcIdSExpr, "id"}, |
| 275 | |
| 276 | { JCO_CLSS_CommaSeq, jcSequencePrint,jcNodeSExpr, "commaseq", ", "}, |
| 277 | { JCO_CLSS_SpaceSeq, jcSequencePrint,jcNodeSExpr, "spaceseq", " "}, |
| 278 | { JCO_CLSS_NLSeq, jcSequencePrint,jcNodeSExpr, "nlseq", "\n"}, |
| 279 | { JCO_CLSS_Seq, jcSequencePrint,jcNodeSExpr, "seq", ""}, |
| 280 | { JCO_CLSS_Parens, jcParenPrint, jcNodeSExpr, "paren", "()", 15, JCO_NONE }, |
| 281 | { JCO_CLSS_Braces, jcParenPrint, jcNodeSExpr, "braces", "{}", 15, JCO_NONE }, |
| 282 | { JCO_CLSS_SqBrackets, jcParenPrint, jcNodeSExpr, "sqbracket", "[]", 15, JCO_NONE }, |
| 283 | { JCO_CLSS_ABrackets, jcParenPrint, jcNodeSExpr, "angle", "<>" }, |
| 284 | { JCO_CLSS_ImportedId, jcImportPrint, jcImportSExpr,"importid", 0}, |
| 285 | { JCO_CLSS_ImportedStatic, |
| 286 | jcImportPrint, jcImportSExpr, "static-importid", 0}, |
| 287 | { JCO_CLSS_Annotation, jcAnnotationPrint, jcNodeSExpr, "annotation", 0}, |
| 288 | { JCO_CLSS_Class, jcClassPrint, jcNodeSExpr, "class", 0}, |
| 289 | { JCO_CLSS_JavaDoc, jcJavaDocPrint, jcCommentSExpr, "javadoc", 0}, |
| 290 | { JCO_CLSS_Comment, jcCommentPrint, jcCommentSExpr, "comment", 0}, |
| 291 | { JCO_CLSS_Method, jcMethodPrint, jcNodeSExpr, "method", 0}, |
| 292 | { JCO_CLSS_Declaration,jcDeclarationPrint, jcNodeSExpr,"declaration", 0}, |
| 293 | { JCO_CLSS_Statement, jcStatementPrint, jcNodeSExpr, "statement", 0}, |
| 294 | { JCO_CLSS_File, jcFilePrint, jcNodeSExpr, "file", 0}, |
| 295 | |
| 296 | { JCO_CLSS_If, jcBlockHdrPrint, jcNodeSExpr, "if", "if"}, |
| 297 | { JCO_CLSS_While, jcBlockHdrPrint, jcNodeSExpr, "while", "while"}, |
| 298 | { JCO_CLSS_Switch, jcBlockHdrPrint, jcNodeSExpr, "switch", "switch"}, |
| 299 | { JCO_CLSS_Case, jcCasePrint, jcNodeSExpr, "case", "case"}, |
| 300 | { JCO_CLSS_Block, jcBlockPrint, jcNodeSExpr, "block"}, |
| 301 | { JCO_CLSS_Try, jcBlockKeywordPrint, jcNodeSExpr, "try", "try"}, |
| 302 | { JCO_CLSS_Catch, jcBlockHdrPrint, jcNodeSExpr, "catch", "catch"}, |
| 303 | { JCO_CLSS_Finally, jcBlockKeywordPrint, jcNodeSExpr, "finally", "finally"}, |
| 304 | |
| 305 | { JCO_CLSS_ArrRef, jcARefPrint, jcNodeSExpr, "arrayref", 0, 20, JCO_NONE}, |
| 306 | { JCO_CLSS_MemRef, jcBinOpPrint, jcNodeSExpr, "memref", ".", 20, JCO_NONE}, |
| 307 | { JCO_CLSS_Cast, jcCastPrint, jcNodeSExpr, "cast", " ", 16, JCO_NONE }, |
| 308 | |
| 309 | { JCO_CLSS_Apply, jcApplyPrint, jcNodeSExpr, "apply", 0, 20, JCO_NONE}, |
| 310 | { JCO_CLSS_Construct, jcConstructPrint, jcNodeSExpr, "new", 0, 20, JCO_NONE}, |
| 311 | |
| 312 | { JCO_CLSS_Not, jcUnaryOpPrint,jcNodeSExpr, "not", "!", 13,JCO_LR }, |
| 313 | { JCO_CLSS_LogAnd, jcBinOpPrint, jcNodeSExpr, "and", " && ", 4, JCO_LR }, |
| 314 | { JCO_CLSS_LogOr, jcBinOpPrint, jcNodeSExpr, "or", " || ", 4, JCO_LR }, |
| 315 | { JCO_CLSS_And, jcBinOpPrint, jcNodeSExpr, "and", " & ", 7, JCO_LR }, |
| 316 | { JCO_CLSS_Or, jcBinOpPrint, jcNodeSExpr, "or", " | ", 7, JCO_LR }, |
| 317 | { JCO_CLSS_XOr, jcBinOpPrint, jcNodeSExpr, "xor", " ^ ", 7, JCO_LR }, |
| 318 | { JCO_CLSS_Equals, jcBinOpPrint, jcNodeSExpr, "equal", " == ", 8, JCO_LR }, |
| 319 | { JCO_CLSS_NEquals, jcBinOpPrint, jcNodeSExpr, "nequal", " != ", 8, JCO_LR }, |
| 320 | { JCO_CLSS_Assign, jcBinOpPrint, jcNodeSExpr, "assign", " = ", 1, JCO_RL }, |
| 321 | { JCO_CLSS_Plus, jcBinOpPrint, jcNodeSExpr, "plus", " + ", 11,JCO_LR }, |
| 322 | { JCO_CLSS_Minus, jcBinOpPrint, jcNodeSExpr, "minus", " - ", 11,JCO_LR }, |
| 323 | { JCO_CLSS_Times, jcBinOpPrint, jcNodeSExpr, "times", "*", 12,JCO_LR }, |
| 324 | { JCO_CLSS_Divide, jcBinOpPrint, jcNodeSExpr, "divide", "/", 12,JCO_LR }, |
| 325 | { JCO_CLSS_Modulo, jcBinOpPrint, jcNodeSExpr, "modulo", "%", 12,JCO_LR }, |
| 326 | { JCO_CLSS_LT, jcBinOpPrint, jcNodeSExpr, "lt", " < ", 9, JCO_LR }, |
| 327 | { JCO_CLSS_LE, jcBinOpPrint, jcNodeSExpr, "le", " <= ", 9, JCO_LR }, |
| 328 | { JCO_CLSS_GT, jcBinOpPrint, jcNodeSExpr, "gt", " > ", 9, JCO_LR }, |
| 329 | { JCO_CLSS_GE, jcBinOpPrint, jcNodeSExpr, "ge", " >= ", 9, JCO_LR }, |
| 330 | { JCO_CLSS_Negate, jcUnaryOpPrint,jcNodeSExpr, "negate", "-", 13,JCO_LR }, |
| 331 | { JCO_CLSS_ShiftUp, jcBinOpPrint, jcNodeSExpr, "shiftup", "<<", 10,JCO_LR }, |
| 332 | { JCO_CLSS_ShiftDn, jcBinOpPrint, jcNodeSExpr, "shiftdn", ">>", 10,JCO_LR }, |
| 333 | |
| 334 | { JCO_CLSS_Conditional,jcCondPrint,jcNodeSExpr, "cond", 0, 2, JCO_RL }, |
| 335 | }; |
| 336 | |
| 337 | void |
| 338 | jcInit() |
| 339 | { |
| 340 | int i; |
| 341 | for (i=0; i<JK_END; i++) { |
| 342 | if (jkKeywords[i].id != i) |
| 343 | bug("Java is a mess"); |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | |
| 348 | |
| 349 | localstatic JavaCodeClass jc0ClassObj(JcClassId); |
| 350 | |
| 351 | JavaCode |
| 352 | jcClass(int modifiers, String comment, |
| 353 | JavaCodeList annotations, |
| 354 | JavaCode id, JavaCode superclass, |
| 355 | JavaCodeList extendList, JavaCodeList body) |
| 356 | { |
| 357 | JavaCodeList jcmods = jc0CreateModifiers(modifiers); |
| 358 | |
| 359 | JavaCode clss = jcoNew(jc0ClassObj(JCO_CLSS_Class), |
| 360 | 6, |
| 361 | jcNLSeq(annotations), |
| 362 | jcSpaceSeq(jcmods), |
| 363 | id, superclass, |
| 364 | extendList == listNil(JavaCode)((JavaCodeList) 0) ? NULL((void*)0) : jcCommaSeq(extendList), |
| 365 | jcNLSeq(body)); |
| 366 | if (comment == NULL((void*)0)) |
| 367 | return clss; |
| 368 | |
| 369 | return jcDocumented(comment, clss); |
| 370 | } |
| 371 | |
| 372 | void |
| 373 | jcClassPrint(JavaCodePContext ctxt, JavaCode clss) |
| 374 | { |
| 375 | JavaCode annotations = jcoArgv(clss)[0]; |
| 376 | JavaCode modifiers = jcoArgv(clss)[1]; |
| 377 | JavaCode id = jcoArgv(clss)[2]; |
| 378 | JavaCode superclass = jcoArgv(clss)[3]; |
| 379 | JavaCode implList = jcoArgv(clss)[4]; |
| 380 | JavaCode body = jcoArgv(clss)[5]; |
| 381 | |
| 382 | jcoWrite(ctxt, annotations); |
| 383 | jcoPContextWrite(ctxt, "\n"); |
| 384 | if (modifiers != NULL((void*)0) && jcoArgc(modifiers) > 0) { |
| 385 | jcoWrite(ctxt, modifiers); |
| 386 | jcoPContextWrite(ctxt, " "); |
| 387 | } |
| 388 | jcoPContextWrite(ctxt, "class "); |
| 389 | jcoWrite(ctxt, id); |
| 390 | if (superclass != NULL((void*)0)) { |
| 391 | jcoPContextWrite(ctxt, " extends "); |
| 392 | jcoWrite(ctxt, superclass); |
| 393 | } |
| 394 | if (implList != NULL((void*)0)) { |
| 395 | jcoPContextWrite(ctxt, " implements "); |
| 396 | jcoWrite(ctxt, implList); |
| 397 | } |
| 398 | jcoPContextWrite(ctxt, " {"); |
| 399 | jcoPContextNewlineIndent(ctxt); |
| 400 | jcoWrite(ctxt, body); |
| 401 | jcoPContextNewlineUnindent(ctxt); |
| 402 | jcoPContextWrite(ctxt, "}"); |
| 403 | } |
| 404 | |
| 405 | /* |
| 406 | * :: Methods (actually, could be 'member' instead) |
| 407 | */ |
| 408 | JavaCode |
| 409 | jcMethod(int modifiers, String comment, |
| 410 | JavaCode retnType, |
| 411 | JavaCode id, JavaCodeList genArgs, |
| 412 | JavaCodeList args, |
| 413 | JavaCodeList exns, JavaCode body) |
| 414 | { |
| 415 | JavaCode meth = jcoNew(jc0ClassObj(JCO_CLSS_Method), |
| 416 | 2, |
| 417 | jcDeclaration(modifiers, retnType, |
| 418 | id, listNil(JavaCode)((JavaCodeList) 0), |
| 419 | jcParens(jcCommaSeq(args)), exns), |
| 420 | body); |
| 421 | return meth; |
| 422 | } |
| 423 | |
| 424 | JavaCode |
| 425 | jcConstructor(int modifiers, String comment, |
| 426 | JavaCode id, JavaCodeList genArgs, |
| 427 | JavaCodeList args, |
| 428 | JavaCodeList exns, JavaCode body) |
| 429 | { |
| 430 | JavaCode meth = jcoNew(jc0ClassObj(JCO_CLSS_Method), |
| 431 | 2, |
| 432 | jcDeclaration(modifiers, jcSpaceSeqV(0), |
| 433 | id, listNil(JavaCode)((JavaCodeList) 0), |
| 434 | jcParens(jcCommaSeq(args)), exns), |
| 435 | body); |
| 436 | return meth; |
| 437 | } |
| 438 | |
| 439 | localstatic void |
| 440 | jcMethodPrint(JavaCodePContext ctxt, JavaCode code) |
| 441 | { |
| 442 | JavaCode decl = jcoArgv(code)[0]; |
| 443 | JavaCode body = jcoArgv(code)[1]; |
| 444 | |
| 445 | jcoWrite(ctxt, decl); |
| 446 | jcoPContextWrite(ctxt, " {"); |
| 447 | jcoPContextNewlineIndent(ctxt); |
| 448 | jcoWrite(ctxt, body); |
| 449 | jcoPContextNewlineUnindent(ctxt); |
| 450 | jcoPContextWrite(ctxt, "}"); |
| 451 | jcoPContextNewline(ctxt); |
| 452 | } |
| 453 | |
| 454 | |
| 455 | /* |
| 456 | * :: Declarations |
| 457 | */ |
| 458 | |
| 459 | JavaCode |
| 460 | jcDeclaration(int modifiers, |
| 461 | JavaCode retnType, |
| 462 | JavaCode id, JavaCodeList genArgs, |
| 463 | JavaCode args, |
| 464 | JavaCodeList exns) |
| 465 | { |
| 466 | JavaCodeList l = listNil(JavaCode)((JavaCodeList) 0); |
| 467 | JavaCodeList jcmods = jc0CreateModifiers(modifiers); |
| 468 | |
| 469 | l = listCons(JavaCode)(JavaCode_listPointer->Cons)(jcSpaceSeq(jcmods), l); |
| 470 | l = listCons(JavaCode)(JavaCode_listPointer->Cons)(retnType, l); |
| 471 | l = listCons(JavaCode)(JavaCode_listPointer->Cons)(id, l); |
| 472 | if (args != NULL((void*)0)) { |
| 473 | l = listCons(JavaCode)(JavaCode_listPointer->Cons)(args, l); |
| 474 | l = listCons(JavaCode)(JavaCode_listPointer->Cons)(jcCommaSeq(exns), l); |
| 475 | } |
| 476 | l = listNReverse(JavaCode)(JavaCode_listPointer->NReverse)(l); |
| 477 | return jcoNewFrList(jc0ClassObj(JCO_CLSS_Declaration), l); |
| 478 | } |
| 479 | |
| 480 | JavaCode |
| 481 | jcMemberDecl(int modifiers, JavaCode type, JavaCode id) |
| 482 | { |
| 483 | return jcDeclaration(modifiers, type, id, 0, 0, 0); |
| 484 | } |
| 485 | |
| 486 | JavaCode |
| 487 | jcParamDecl(int modifiers, JavaCode type, JavaCode id) |
| 488 | { |
| 489 | return jcDeclaration(modifiers, type, id, 0, 0, 0); |
| 490 | } |
| 491 | |
| 492 | JavaCode |
| 493 | jcLocalDecl(int modifiers, JavaCode type, JavaCode id) |
| 494 | { |
| 495 | return jcDeclaration(modifiers, type, id, 0, 0, 0); |
| 496 | } |
| 497 | |
| 498 | JavaCode |
| 499 | jcInitialisation(int modifiers, JavaCode type, JavaCode id, JavaCode value) |
| 500 | { |
| 501 | return jcParamDecl(modifiers, type, jcAssign(id, value)); |
| 502 | } |
| 503 | |
| 504 | |
| 505 | localstatic void |
| 506 | jcDeclarationPrint(JavaCodePContext ctxt, JavaCode code) |
| 507 | { |
| 508 | JavaCode mods = jcoArgv(code)[0]; |
| 509 | JavaCode retn = jcoArgv(code)[1]; |
| 510 | JavaCode name = jcoArgv(code)[2]; |
| 511 | |
| 512 | if (!jcoIsEmpty(mods)) { |
| 513 | jcoWrite(ctxt, mods); |
| 514 | jcoPContextWrite(ctxt, " "); |
| 515 | } |
| 516 | if (!jcoIsEmpty(retn)) { |
| 517 | jcoWrite(ctxt, retn); |
| 518 | jcoPContextWrite(ctxt, " "); |
| 519 | } |
| 520 | jcoWrite(ctxt, name); |
| 521 | if (jcoArgc(code) > 3) { |
| 522 | JavaCode args = jcoArgv(code)[3]; |
| 523 | JavaCode exns = jcoArgv(code)[4]; |
| 524 | jcoWrite(ctxt, args); |
| 525 | if (jcoArgc(exns) > 0) { |
| 526 | jcoPContextWrite(ctxt, " throws "); |
| 527 | jcoWrite(ctxt, exns); |
| 528 | } |
| 529 | |
| 530 | } |
| 531 | |
| 532 | } |
| 533 | |
| 534 | /* |
| 535 | * :: Annotations |
| 536 | */ |
| 537 | |
| 538 | JavaCode |
| 539 | jcAnnotation(JavaCode annotationClass, JavaCodeList arguments) |
| 540 | { |
| 541 | return jcoNew(jc0ClassObj(JCO_CLSS_Annotation), |
| 542 | 2, annotationClass, |
| 543 | jcParens(jcCommaSeq(arguments))); |
| 544 | } |
| 545 | |
| 546 | localstatic void |
| 547 | jcAnnotationPrint(JavaCodePContext ctxt, JavaCode code) |
| 548 | { |
| 549 | jcoPContextWrite(ctxt, "@"); |
| 550 | jcoWrite(ctxt, jcoArgv(code)[0]); |
| 551 | jcoWrite(ctxt, jcoArgv(code)[1]); |
| 552 | } |
| 553 | |
| 554 | /* |
| 555 | * :: Function application |
| 556 | */ |
| 557 | |
| 558 | JavaCode |
| 559 | jcApply(JavaCode c, JavaCodeList args) |
| 560 | { |
| 561 | return jcoNew(jc0ClassObj(JCO_CLSS_Apply), 2, c, jcParens(jcCommaSeq(args))); |
| 562 | } |
| 563 | |
| 564 | JavaCode |
| 565 | jcApplyV(JavaCode c, int n, ...) |
| 566 | { |
| 567 | va_list argp; |
| 568 | JavaCode jc; |
| 569 | va_start(argp, n)__builtin_va_start(argp, n); |
| 570 | jc = jcApplyP(c, n, argp); |
| 571 | va_end(argp)__builtin_va_end(argp); |
| 572 | return jc; |
| 573 | } |
| 574 | |
| 575 | JavaCode |
| 576 | jcApplyP(JavaCode c, int n, va_list argp) |
| 577 | { |
| 578 | return jcoNew(jc0ClassObj(JCO_CLSS_Apply), |
| 579 | 2, c, jcParens(jcCommaSeqP(n, argp))); |
| 580 | } |
| 581 | |
| 582 | JavaCode |
| 583 | jcApplyMethod(JavaCode c, JavaCode id, JavaCodeList args) |
| 584 | { |
| 585 | return jcApply(jcMemRef(c, id), args); |
| 586 | } |
| 587 | |
| 588 | JavaCode |
| 589 | jcApplyMethodV(JavaCode c, JavaCode id, int n, ...) |
| 590 | { |
| 591 | va_list argp; |
| 592 | JavaCode jc; |
| 593 | |
| 594 | va_start(argp, n)__builtin_va_start(argp, n); |
| 595 | |
| 596 | jc = jcApplyP(jcMemRef(c, id), n, argp); |
| 597 | va_end(argp)__builtin_va_end(argp); |
| 598 | return jc; |
| 599 | } |
| 600 | |
| 601 | localstatic void |
| 602 | jcApplyPrint(JavaCodePContext ctxt, JavaCode code) |
| 603 | { |
| 604 | jcoWrite(ctxt, jcoArgv(code)[0]); |
| 605 | jcoWrite(ctxt, jcoArgv(code)[1]); |
| 606 | } |
| 607 | |
| 608 | JavaCode |
| 609 | jcGenericMethodName(JavaCode methodName, JavaCodeList genArgs) |
| 610 | { |
| 611 | return jcSeqV(2, jcABrackets(jcCommaSeq(genArgs))); |
| 612 | } |
| 613 | |
| 614 | JavaCode |
| 615 | jcGenericMethodNameV(JavaCode methodName, int n, ...) |
| 616 | { |
| 617 | JavaCode jc; |
| 618 | va_list argp; |
| 619 | va_start(argp, n)__builtin_va_start(argp, n); |
| 620 | jc = jcSeqV(2, jcABrackets(jcCommaSeqP(n, argp)), methodName); |
| 621 | va_end(argp)__builtin_va_end(argp); |
| 622 | return jc; |
| 623 | } |
| 624 | |
| 625 | |
| 626 | /* |
| 627 | * :: Parens |
| 628 | */ |
| 629 | JavaCode |
| 630 | jcParens(JavaCode args) |
| 631 | { |
| 632 | JavaCode jco = jcoNew(jc0ClassObj(JCO_CLSS_Parens), 1, args); |
| 633 | return jco; |
| 634 | } |
| 635 | |
| 636 | JavaCode |
| 637 | jcBraces(JavaCode args) |
| 638 | { |
| 639 | JavaCode jco = jcoNew(jc0ClassObj(JCO_CLSS_Braces), 1, args); |
| 640 | return jco; |
| 641 | } |
| 642 | |
| 643 | JavaCode |
| 644 | jcSqBrackets(JavaCode args) |
| 645 | { |
| 646 | JavaCode jco = jcoNew(jc0ClassObj(JCO_CLSS_SqBrackets), 1, args); |
| 647 | return jco; |
| 648 | } |
| 649 | |
| 650 | JavaCode |
| 651 | jcABrackets(JavaCode args) |
| 652 | { |
| 653 | JavaCode jco = jcoNew(jc0ClassObj(JCO_CLSS_ABrackets), 1, args); |
| 654 | return jco; |
| 655 | } |
| 656 | |
| 657 | localstatic void |
| 658 | jcParenPrint(JavaCodePContext ctxt, JavaCode code) |
| 659 | { |
| 660 | char s[2] = " "; |
| 661 | String txt = jcoClass(code)(((code)->hdr).clss)->txt; |
| 662 | s[0]=txt[0]; |
| 663 | jcoPContextWrite(ctxt, s); |
| 664 | jcoWrite(ctxt, jcoArgv(code)[0]); |
| 665 | s[0]=txt[1]; |
| 666 | jcoPContextWrite(ctxt, s); |
| 667 | } |
| 668 | |
| 669 | |
| 670 | /* |
| 671 | * :: Comments |
| 672 | */ |
| 673 | JavaCode |
| 674 | jcDocumented(String comment, JavaCode code) |
| 675 | { |
| 676 | JavaCode doc = jcoNewLiteral(jc0ClassObj(JCO_CLSS_JavaDoc), comment); |
| 677 | return jcNLSeqV(2, doc, code); |
| 678 | } |
| 679 | |
| 680 | JavaCode |
| 681 | jcCommented(String comment, JavaCode code) |
| 682 | { |
| 683 | return jcSpaceSeqV(2, jcComment(comment), code); |
| 684 | } |
| 685 | |
| 686 | JavaCode |
| 687 | jcComment(String comment) |
| 688 | { |
| 689 | JavaCode jc = jcoNewLiteral(jc0ClassObj(JCO_CLSS_Comment), comment); |
| 690 | return jc; |
| 691 | } |
| 692 | |
| 693 | localstatic void |
| 694 | jcJavaDocPrint(JavaCodePContext ctxt, JavaCode code) |
| 695 | { |
| 696 | String s = strReplace(jcoLiteral(code), "\n", "\n * "); |
| 697 | jcoPContextWrite(ctxt, "/**\n * "); |
| 698 | jcoPContextWrite(ctxt, s); |
| 699 | jcoPContextWrite(ctxt, "\n */"); |
| 700 | strFree(s); |
| 701 | } |
| 702 | |
| 703 | |
| 704 | localstatic void |
| 705 | jcCommentPrint(JavaCodePContext ctxt, JavaCode code) |
| 706 | { |
| 707 | String s = strReplace(jcoLiteral(code), "\n", "\n *"); |
| 708 | jcoPContextWrite(ctxt, "/* "); |
| 709 | jcoPContextWrite(ctxt, s); |
| 710 | jcoPContextWrite(ctxt, "*/"); |
| 711 | strFree(s); |
| 712 | } |
| 713 | |
| 714 | localstatic SExpr |
| 715 | jcCommentSExpr(JavaCode code) |
| 716 | { |
| 717 | SExpr h = sxiFrSymbol(symIntern(jcoClass(code)->name)symProbe((((code)->hdr).clss)->name, 1 | 2)); |
| 718 | String s = jc0EscapeString(jcoLiteral(code), false((int) 0)); |
| 719 | SExpr sx = sxiFrString(s); |
| 720 | strFree(s); |
| 721 | return sxiList(2, h, sx); |
| 722 | } |
| 723 | |
| 724 | /* |
| 725 | * :: Imports |
| 726 | */ |
| 727 | |
| 728 | JavaCode |
| 729 | jcImportedIdFrString(String str) |
| 730 | { |
| 731 | String p = strLastIndexOf(str, '.'); |
| 732 | if (p == NULL((void*)0)) { |
| 733 | return jcId(strCopy(str)); |
| 734 | } |
| 735 | else { |
| 736 | String pkg = strnCopy(str, p - str); |
| 737 | String id = strCopy(p+1); |
| 738 | return jcImportedId(pkg, id); |
| 739 | } |
| 740 | } |
| 741 | |
| 742 | JavaCode |
| 743 | jcImportedId(String pkg, String name) |
| 744 | { |
| 745 | return jcoNewImport(jc0ClassObj(JCO_CLSS_ImportedId), pkg, listNil(String)((StringList) 0), name, false((int) 0)); |
| 746 | } |
| 747 | |
| 748 | |
| 749 | String |
| 750 | jcImportedIdName(JavaCode id) |
| 751 | { |
| 752 | return jcoImportId(id); |
| 753 | } |
| 754 | |
| 755 | String |
| 756 | jcImportedIdPkg(JavaCode id) |
| 757 | { |
| 758 | return jcoImportPkg(id); |
| 759 | } |
| 760 | |
| 761 | |
| 762 | JavaCode |
| 763 | jcImportedStaticId(String pkg, String clss, String name) |
| 764 | { |
| 765 | assert(strLastIndexOf(clss, '.') == NULL)do { if (!(strLastIndexOf(clss, '.') == ((void*)0))) _do_assert (("strLastIndexOf(clss, '.') == NULL"),"java/javacode.c",765) ; } while (0); |
| 766 | assert(strLastIndexOf(name, '.') == NULL)do { if (!(strLastIndexOf(name, '.') == ((void*)0))) _do_assert (("strLastIndexOf(name, '.') == NULL"),"java/javacode.c",766) ; } while (0); |
| 767 | |
| 768 | return jcoNewImport(jc0ClassObj(JCO_CLSS_ImportedStatic), pkg, |
| 769 | listSingleton(String)(String_listPointer->Singleton)(clss), name, false((int) 0)); |
| 770 | } |
| 771 | |
| 772 | JavaCode |
| 773 | jcImportedStaticIdFrString(String str) |
| 774 | { |
| 775 | String p = strLastIndexOf(str, '.'); |
| 776 | String id = strCopy(p+1); |
| 777 | String pkgClss = strnCopy(str, p - str); |
| 778 | String dclss = strLastIndexOf(pkgClss, '.'); |
| 779 | String clss = strCopy(dclss+1); |
| 780 | String pkg = strnCopy(pkgClss, dclss - pkgClss); |
| 781 | |
| 782 | return jcImportedStaticId(pkg, clss, id); |
| 783 | } |
| 784 | |
| 785 | String |
| 786 | jcImportedStaticIdClass(JavaCode importedId) |
| 787 | { |
| 788 | return car(jcoImportPath(importedId))((jcoImportPath(importedId))->first); |
| 789 | } |
| 790 | |
| 791 | String |
| 792 | jcImportedStaticIdPkg(JavaCode importedId) |
| 793 | { |
| 794 | return jcoImportPkg(importedId); |
| 795 | } |
| 796 | |
| 797 | String |
| 798 | jcImportedStaticIdName(JavaCode importedId) |
| 799 | { |
| 800 | return jcImportedIdName(importedId); |
| 801 | } |
| 802 | |
| 803 | localstatic void |
| 804 | jcImportPrint(JavaCodePContext ctxt, JavaCode code) |
| 805 | { |
| 806 | if (jcoImportIsImported(code)) |
| 807 | jcoPContextWrite(ctxt, jcoImportId(code)); |
| 808 | else { |
| 809 | if (strlen(jcoImportPkg(code)) != 0) { |
| 810 | jcoPContextWrite(ctxt, jcoImportPkg(code)); |
| 811 | jcoPContextWrite(ctxt, "."); |
| 812 | } |
| 813 | jcoPContextWrite(ctxt, jcoImportId(code)); |
| 814 | } |
| 815 | } |
| 816 | |
| 817 | localstatic SExpr |
| 818 | jcImportSExpr(JavaCode code) |
| 819 | { |
| 820 | SExpr sym = sxiFrSymbol(symIntern(jcoClass(code)->name)symProbe((((code)->hdr).clss)->name, 1 | 2)); |
| 821 | if (jcoImportPkg(code) == NULL((void*)0)) { |
| 822 | return sxiList(2, sym, sxiFrString(jcoImportId(code))); |
| 823 | } |
| 824 | return sxiList(3, sym, |
| 825 | sxiFrString(jcoImportPkg(code)), |
| 826 | sxiFrString(jcoImportId(code))); |
| 827 | } |
| 828 | |
| 829 | |
| 830 | /* |
| 831 | * :: String literals |
| 832 | */ |
| 833 | |
| 834 | |
| 835 | JavaCode |
| 836 | jcLiteralString(String s) |
| 837 | { |
| 838 | return jcoNewLiteral(jc0ClassObj(JCO_CLSS_String), |
| 839 | jc0EscapeString(s, false((int) 0))); |
| 840 | } |
| 841 | |
| 842 | JavaCode |
| 843 | jcLiteralStringWithTerminalChar(String s) |
| 844 | { |
| 845 | return jcoNewLiteral(jc0ClassObj(JCO_CLSS_String), |
| 846 | jc0EscapeString(s, true1)); |
| 847 | } |
| 848 | |
| 849 | JavaCode |
| 850 | jcLiteralChar(String s) |
| 851 | { |
| 852 | String t; |
| 853 | if (s[0] == '\0') |
| 854 | t = strCopy("\\0"); |
| 855 | else if (s[0] == '\'') |
| 856 | t = strCopy("\\'"); |
| 857 | else if (s[0] == '"') |
| 858 | t = strCopy("\\\""); |
| 859 | else if (s[0] == '\n') |
| 860 | t = strCopy("\\n"); |
| 861 | else if (s[0] == '\t') |
| 862 | t = strCopy("\\t"); |
| 863 | else if (s[0] == '\\') |
| 864 | t = strCopy("\\\\"); |
| 865 | else if (s[0] == -1) |
| 866 | t = strCopy("\\1"); |
| 867 | else |
| 868 | t = strCopy(s); |
| 869 | |
| 870 | return jcoNewLiteral(jc0ClassObj(JCO_CLSS_Character), t); |
| 871 | } |
| 872 | |
| 873 | localstatic void |
| 874 | jcStringPrint(JavaCodePContext ctxt, JavaCode code) |
| 875 | { |
| 876 | JavaCodeClass thisClss = jcoClass(code)(((code)->hdr).clss); |
| 877 | jcoPContextWrite(ctxt, thisClss->txt); |
| 878 | jcoPContextWrite(ctxt, jcoLiteral(code)); |
| 879 | jcoPContextWrite(ctxt, thisClss->txt); |
| 880 | } |
| 881 | |
| 882 | localstatic SExpr |
| 883 | jcStringSExpr(JavaCode code) |
| 884 | { |
| 885 | String s = jcoLiteral(code); |
| 886 | SExpr sx; |
| 887 | s = jc0EscapeString(s, false((int) 0)); |
| 888 | sx = sxiFrString(s); |
| 889 | strFree(s); |
| 890 | return sx; |
| 891 | } |
| 892 | |
| 893 | /* |
| 894 | * :: Numeric literals |
| 895 | */ |
| 896 | |
| 897 | |
| 898 | JavaCode |
| 899 | jcLiteralInteger(AInt i) |
| 900 | { |
| 901 | String s = strPrintf("%d", i); |
| 902 | return jcoNewLiteral(jc0ClassObj(JCO_CLSS_Integer), s); |
| 903 | } |
| 904 | |
| 905 | JavaCode |
| 906 | jcLiteralIntegerFrString(String s) |
| 907 | { |
| 908 | return jcoNewLiteral(jc0ClassObj(JCO_CLSS_Integer), s); |
| 909 | } |
| 910 | |
| 911 | JavaCode |
| 912 | jcLiteralFloatFrString(String s) |
| 913 | { |
| 914 | return jcoNewLiteral(jc0ClassObj(JCO_CLSS_Float), s); |
| 915 | } |
| 916 | |
| 917 | void |
| 918 | jcIntegerPrint(JavaCodePContext ctxt, JavaCode code) |
| 919 | { |
| 920 | jcoPContextWrite(ctxt, jcoLiteral(code)); |
| 921 | } |
| 922 | |
| 923 | localstatic SExpr |
| 924 | jcIntegerSExpr(JavaCode code) |
| 925 | { |
| 926 | int i = atoi(jcoLiteral(code)); |
| 927 | return sxiFrInteger(i); |
| 928 | } |
| 929 | |
| 930 | /* |
| 931 | * :: Keywords |
| 932 | */ |
| 933 | |
| 934 | |
| 935 | JavaCode |
| 936 | jcKeyword(Symbol sym) |
| 937 | { |
| 938 | return jcoNewToken(jc0ClassObj(JCO_CLSS_Keyword), sym); |
| 939 | } |
| 940 | |
| 941 | JavaCode |
| 942 | jcReturn(JavaCode c) |
| 943 | { |
| 944 | return jcSpaceSeqV(2, jcKeyword(symInternConst("return")symProbe("return", 1)), c); |
| 945 | } |
| 946 | |
| 947 | JavaCode |
| 948 | jcReturnVoid() |
| 949 | { |
| 950 | return jcKeyword(symInternConst("return")symProbe("return", 1)); |
| 951 | } |
| 952 | |
| 953 | |
| 954 | JavaCode |
| 955 | jcNull() |
| 956 | { |
| 957 | return jcKeyword(symInternConst("null")symProbe("null", 1)); |
| 958 | } |
| 959 | |
| 960 | JavaCode |
| 961 | jcTrue(String name) |
| 962 | { |
| 963 | return jcKeyword(symInternConst("true")symProbe("true", 1)); |
| 964 | } |
| 965 | JavaCode |
| 966 | jcFalse(String name) |
| 967 | { |
| 968 | return jcKeyword(symInternConst("false")symProbe("false", 1)); |
| 969 | } |
| 970 | |
| 971 | JavaCode |
| 972 | jcThis(String name) |
| 973 | { |
| 974 | return jcKeyword(symInternConst("this")symProbe("this", 1)); |
| 975 | } |
| 976 | |
| 977 | localstatic SExpr |
| 978 | jcKeywordSExpr(JavaCode code) |
| 979 | { |
| 980 | return sxiFrSymbol(jcoToken(code)); |
| 981 | } |
| 982 | |
| 983 | localstatic void |
| 984 | jcKeywordPrint(JavaCodePContext ctxt, JavaCode code) |
| 985 | { |
| 986 | jcoPContextWrite(ctxt, symString(jcoToken(code))((jcoToken(code))->str)); |
| 987 | } |
| 988 | |
| 989 | /* |
| 990 | * :: Ids |
| 991 | */ |
| 992 | |
| 993 | JavaCode |
| 994 | jcId(String name) |
| 995 | { |
| 996 | return jcoNewLiteral(jc0ClassObj(JCO_CLSS_Id), name); |
| 997 | } |
| 998 | |
| 999 | String |
| 1000 | jcIdName(JavaCode id) |
| 1001 | { |
| 1002 | return jcoLiteral(id); |
| 1003 | } |
| 1004 | |
| 1005 | localstatic SExpr |
| 1006 | jcIdSExpr(JavaCode code) |
| 1007 | { |
| 1008 | return sxiFrString(jcoLiteral(code)); |
| 1009 | } |
| 1010 | |
| 1011 | void |
| 1012 | jcIdPrint(JavaCodePContext ctxt, JavaCode code) |
| 1013 | { |
| 1014 | String name = jcoLiteral(code); |
| 1015 | jcoPContextWrite(ctxt, name); |
| 1016 | } |
| 1017 | |
| 1018 | |
| 1019 | /* |
| 1020 | * :: Generic id |
| 1021 | */ |
| 1022 | JavaCode |
| 1023 | jcGenericId(JavaCode root, JavaCodeList genArgs) |
| 1024 | { |
| 1025 | return jcSeqV(2, root, jcABrackets(jcCommaSeq(genArgs))); |
| 1026 | } |
| 1027 | |
| 1028 | |
| 1029 | /* |
| 1030 | * :: Constructor Call |
| 1031 | */ |
| 1032 | JavaCode |
| 1033 | jcConstructBase(JavaCode t, JavaCode body) |
| 1034 | { |
| 1035 | return jcoNew(jc0ClassObj(JCO_CLSS_Construct), 2, t, body); |
| 1036 | } |
| 1037 | |
| 1038 | JavaCode |
| 1039 | jcConstruct(JavaCode t, JavaCodeList args) |
| 1040 | { |
| 1041 | return jcConstructBase(jcApply(t, args), 0); |
| 1042 | } |
| 1043 | |
| 1044 | JavaCode |
| 1045 | jcConstructSubclass(JavaCode t, JavaCodeList args, JavaCode body) |
| 1046 | { |
| 1047 | return jcConstructBase(jcApply(t, args), body); |
| 1048 | } |
| 1049 | |
| 1050 | JavaCode |
| 1051 | jcConstructV(JavaCode t, int n, ...) |
| 1052 | { |
| 1053 | JavaCode jc; |
| 1054 | va_list argp; |
| 1055 | |
| 1056 | va_start(argp, n)__builtin_va_start(argp, n); |
| 1057 | jc = jcConstructBase(jcApplyP(t, n, argp), 0); |
| 1058 | va_end(argp)__builtin_va_end(argp); |
| 1059 | return jc; |
| 1060 | } |
| 1061 | |
| 1062 | localstatic void |
| 1063 | jcConstructPrint(JavaCodePContext ctxt, JavaCode t) |
| 1064 | { |
| 1065 | jcoPContextWrite(ctxt, "new "); |
| 1066 | jcoWrite(ctxt, jcoArgv(t)[0]); |
| 1067 | if (jcoArgv(t)[1] != 0) { |
| 1068 | jcoPContextWrite(ctxt, " { "); |
| 1069 | jcoPContextNewlineIndent(ctxt); |
| 1070 | jcoWrite(ctxt, jcoArgv(t)[1]); |
| 1071 | jcoPContextNewlineUnindent(ctxt); |
| 1072 | jcoPContextWrite(ctxt, "}"); |
| 1073 | } |
| 1074 | } |
| 1075 | |
| 1076 | /* |
| 1077 | * :: Arrays |
| 1078 | */ |
| 1079 | |
| 1080 | JavaCode |
| 1081 | jcArrayOf(JavaCode type) |
| 1082 | { |
| 1083 | return jcSpaceSeqV(2, type, jcSqBrackets(jcSpaceSeqV(0))); |
| 1084 | } |
| 1085 | |
| 1086 | JavaCode |
| 1087 | jcNAry(JavaCode type) |
| 1088 | { |
| 1089 | return jcSpaceSeqV(2, type, |
| 1090 | jcKeyword(symInternConst("...")symProbe("...", 1))); |
| 1091 | } |
| 1092 | |
| 1093 | JavaCode |
| 1094 | jcArrayNew(JavaCode t, JavaCode sz) |
| 1095 | { |
| 1096 | return jcConstructBase(jcArrayRef(t, sz), 0); |
| 1097 | } |
| 1098 | |
| 1099 | JavaCode |
| 1100 | jcArrayRef(JavaCode arr, JavaCode idx) |
| 1101 | { |
| 1102 | return jcoNew(jc0ClassObj(JCO_CLSS_ArrRef), 2, arr, idx); |
| 1103 | } |
| 1104 | |
| 1105 | void jcARefPrint(JavaCodePContext ctxt, JavaCode code) |
| 1106 | { |
| 1107 | JavaCodeClass thisClss = jcoClass(code)(((code)->hdr).clss); |
| 1108 | |
| 1109 | jc0PrintWithParens(ctxt, thisClss, jcoArgv(code)[0]); |
| 1110 | jcoPContextWrite(ctxt, "["); |
| 1111 | jcoWrite(ctxt, jcoArgv(code)[1]); |
| 1112 | jcoPContextWrite(ctxt, "]"); |
| 1113 | } |
| 1114 | |
| 1115 | /* |
| 1116 | * :: Binary operations |
| 1117 | */ |
| 1118 | |
| 1119 | JavaCode |
| 1120 | jcAssign(JavaCode lhs, JavaCode rhs) |
| 1121 | { |
| 1122 | return jcBinaryOp(jc0ClassObj(JCO_CLSS_Assign), lhs, rhs); |
| 1123 | } |
| 1124 | |
| 1125 | JavaCode |
| 1126 | jcCast(JavaCode lhs, JavaCode rhs) |
| 1127 | { |
| 1128 | return jcBinaryOp(jc0ClassObj(JCO_CLSS_Cast), lhs, rhs); |
| 1129 | } |
| 1130 | |
| 1131 | JavaCode |
| 1132 | jcMemRef(JavaCode lhs, JavaCode rhs) |
| 1133 | { |
| 1134 | return jcBinaryOp(jc0ClassObj(JCO_CLSS_MemRef), lhs, rhs); |
| 1135 | } |
| 1136 | |
| 1137 | |
| 1138 | |
| 1139 | /* |
| 1140 | * :: Operations |
| 1141 | */ |
| 1142 | typedef JavaCode (*JcOpBuilder)(JavaCodeList l); |
| 1143 | struct jcOpInfo { |
| 1144 | JcOperation op; |
| 1145 | JcOpBuilder builder; |
| 1146 | JcClassId clsId; |
| 1147 | }; |
| 1148 | typedef struct jcOpInfo *JcOpInfo; |
| 1149 | |
| 1150 | localstatic JavaCode jcOpNot(JavaCodeList l); |
| 1151 | localstatic JavaCode jcOpNegate(JavaCodeList l); |
| 1152 | localstatic JavaCode jcOpTimesPlus(JavaCodeList args); |
| 1153 | |
| 1154 | localstatic JcOpInfo jc0OpInfo(JcOperation op); |
| 1155 | |
| 1156 | |
| 1157 | struct jcOpInfo JcOpInfoTable[] = { |
| 1158 | { JCO_OP_Not, jcOpNot}, |
| 1159 | { JCO_OP_LogAnd, 0, JCO_CLSS_LogAnd}, |
| 1160 | { JCO_OP_LogOr, 0, JCO_CLSS_LogOr}, |
| 1161 | { JCO_OP_And, 0, JCO_CLSS_And}, |
| 1162 | { JCO_OP_Or, 0, JCO_CLSS_Or}, |
| 1163 | { JCO_OP_XOr, 0, JCO_CLSS_XOr}, |
| 1164 | { JCO_OP_Equals, 0, JCO_CLSS_Equals}, |
| 1165 | { JCO_OP_NEquals, 0, JCO_CLSS_NEquals}, |
| 1166 | { JCO_OP_Assign, 0, JCO_CLSS_Assign}, |
| 1167 | { JCO_OP_Plus, 0, JCO_CLSS_Plus}, |
| 1168 | { JCO_OP_Minus, 0, JCO_CLSS_Minus}, |
| 1169 | { JCO_OP_Times, 0, JCO_CLSS_Times}, |
| 1170 | { JCO_OP_Divide, 0, JCO_CLSS_Divide}, |
| 1171 | { JCO_OP_Modulo, 0, JCO_CLSS_Modulo}, |
| 1172 | { JCO_OP_LT, 0, JCO_CLSS_LT}, |
| 1173 | { JCO_OP_LE, 0, JCO_CLSS_LE}, |
| 1174 | { JCO_OP_GT, 0, JCO_CLSS_GT}, |
| 1175 | { JCO_OP_GE, 0, JCO_CLSS_GE}, |
| 1176 | { JCO_OP_Negate, jcOpNegate}, |
| 1177 | { JCO_OP_TimesPlus, jcOpTimesPlus}, |
| 1178 | { JCO_OP_ShiftUp, 0, JCO_CLSS_ShiftUp}, |
| 1179 | { JCO_OP_ShiftDn, 0, JCO_CLSS_ShiftDn}, |
| 1180 | }; |
| 1181 | |
| 1182 | JavaCode |
| 1183 | jcOp(JcOperation op, JavaCodeList args) |
| 1184 | { |
| 1185 | JcOpInfo inf = jc0OpInfo(op); |
| 1186 | if (inf->builder == 0) |
| 1187 | return jcBinaryOp(jc0ClassObj(inf->clsId), car(args)((args)->first), car(cdr(args))((((args)->rest))->first)); |
| 1188 | else |
| 1189 | return inf->builder(args); |
| 1190 | } |
| 1191 | |
| 1192 | JavaCode |
| 1193 | jcBinOp(JcOperation op, JavaCode e1, JavaCode e2) |
| 1194 | { |
| 1195 | JcOpInfo inf = jc0OpInfo(op); |
| 1196 | |
| 1197 | if (inf->builder == 0) |
| 1198 | return jcBinaryOp(jc0ClassObj(inf->clsId), e1, e2); |
| 1199 | else |
| 1200 | return inf->builder(listList(JavaCode)(JavaCode_listPointer->List)(2, e1, e2)); |
| 1201 | } |
| 1202 | |
| 1203 | |
| 1204 | localstatic JavaCode |
| 1205 | jcOpNot(JavaCodeList l) |
| 1206 | { |
| 1207 | return jcNot(car(l)((l)->first)); |
| 1208 | } |
| 1209 | |
| 1210 | localstatic JavaCode |
| 1211 | jcOpNegate(JavaCodeList l) |
| 1212 | { |
| 1213 | return jcNegate(car(l)((l)->first)); |
| 1214 | } |
| 1215 | |
| 1216 | localstatic JavaCode |
| 1217 | jcOpTimesPlus(JavaCodeList args) |
| 1218 | { |
| 1219 | JavaCode a1 = car(args)((args)->first); |
| 1220 | JavaCode a2 = car(cdr(args))((((args)->rest))->first); |
| 1221 | JavaCode a3 = car(cdr(cdr(args)))((((((args)->rest))->rest))->first); |
| 1222 | |
| 1223 | return jcBinaryOp(jc0ClassObj(JCO_CLSS_Plus), |
| 1224 | jcBinaryOp(jc0ClassObj(JCO_CLSS_Times), a1, a2), |
| 1225 | a3); |
| 1226 | } |
| 1227 | |
| 1228 | |
| 1229 | localstatic JcOpInfo |
| 1230 | jc0OpInfo(JcOperation op) |
| 1231 | { |
| 1232 | JcOpInfo inf = &JcOpInfoTable[op]; |
| 1233 | |
| 1234 | assert(inf->op == op)do { if (!(inf->op == op)) _do_assert(("inf->op == op") ,"java/javacode.c",1234); } while (0); |
| 1235 | return inf; |
| 1236 | } |
| 1237 | |
| 1238 | /* |
| 1239 | * :: Binary operations |
| 1240 | */ |
| 1241 | |
| 1242 | JavaCode |
| 1243 | jcBinaryOp(JavaCodeClass c, JavaCode lhs, JavaCode rhs) |
| 1244 | { |
| 1245 | JavaCode r = jcoNew(c, 2, lhs, rhs); |
| 1246 | return r; |
| 1247 | } |
| 1248 | |
| 1249 | localstatic void |
| 1250 | jcBinOpPrint(JavaCodePContext ctxt, JavaCode code) |
| 1251 | { |
| 1252 | JavaCodeClass thisClss = jcoClass(code)(((code)->hdr).clss); |
| 1253 | JavaCode lhs = jcoArgv(code)[0]; |
| 1254 | JavaCode rhs = jcoArgv(code)[1]; |
| 1255 | |
| 1256 | jc0PrintWithParens(ctxt, thisClss, lhs); |
| 1257 | jcoPContextWrite(ctxt, thisClss->txt); |
| 1258 | jc0PrintWithParens(ctxt, thisClss, rhs); |
| 1259 | } |
| 1260 | |
| 1261 | localstatic void |
| 1262 | jc0PrintWithParens(JavaCodePContext ctxt, JavaCodeClass oClss, JavaCode arg) |
| 1263 | { |
| 1264 | JavaCodeClass aClss = jcoClass(arg)(((arg)->hdr).clss); |
| 1265 | if (jc0NeedsParens(oClss, aClss)) { |
| 1266 | jcoPContextWrite(ctxt, "("); |
| 1267 | jcoWrite(ctxt, arg); |
| 1268 | jcoPContextWrite(ctxt, ")"); |
| 1269 | } |
| 1270 | else { |
| 1271 | jcoWrite(ctxt, arg); |
| 1272 | } |
| 1273 | } |
| 1274 | |
| 1275 | localstatic Bool |
| 1276 | jc0NeedsParens(JavaCodeClass c1, JavaCodeClass c2) |
| 1277 | { |
| 1278 | if (c2->prec == 0) |
| 1279 | return false((int) 0); |
| 1280 | return c1->prec > c2->prec; |
| 1281 | } |
| 1282 | |
| 1283 | /* |
| 1284 | * :: Unary operations |
| 1285 | */ |
| 1286 | |
| 1287 | JavaCode |
| 1288 | jcNot(JavaCode arg) |
| 1289 | { |
| 1290 | return jcoNew(jc0ClassObj(JCO_CLSS_Not), 1, arg); |
| 1291 | } |
| 1292 | |
| 1293 | JavaCode |
| 1294 | jcNegate(JavaCode arg) |
| 1295 | { |
| 1296 | return jcoNew(jc0ClassObj(JCO_CLSS_Negate), 1, arg); |
| 1297 | } |
| 1298 | |
| 1299 | |
| 1300 | localstatic void |
| 1301 | jcUnaryOpPrint(JavaCodePContext ctxt, JavaCode code) |
| 1302 | { |
| 1303 | JavaCodeClass thisClss, aClss; |
| 1304 | JavaCode arg = jcoArgv(code)[0]; |
| 1305 | thisClss = jcoClass(code)(((code)->hdr).clss); |
| 1306 | aClss = jcoClass(arg)(((arg)->hdr).clss); |
Value stored to 'aClss' is never read | |
| 1307 | |
| 1308 | jcoPContextWrite(ctxt, thisClss->txt); |
| 1309 | jc0PrintWithParens(ctxt, thisClss, arg); |
| 1310 | |
| 1311 | } |
| 1312 | |
| 1313 | localstatic void |
| 1314 | jcCastPrint(JavaCodePContext ctxt, JavaCode code) |
| 1315 | { |
| 1316 | JavaCodeClass thisClss, aClss; |
| 1317 | JavaCode arg1 = jcoArgv(code)[0]; |
| 1318 | JavaCode arg2 = jcoArgv(code)[1]; |
| 1319 | |
| 1320 | thisClss = jcoClass(code)(((code)->hdr).clss); |
| 1321 | aClss = jcoClass(code)(((code)->hdr).clss); |
| 1322 | |
| 1323 | jcoPContextWrite(ctxt, "("); |
| 1324 | jcoWrite(ctxt, arg1); |
| 1325 | jcoPContextWrite(ctxt, ")"); |
| 1326 | |
| 1327 | jc0PrintWithParens(ctxt, thisClss, arg2); |
| 1328 | } |
| 1329 | |
| 1330 | /* |
| 1331 | * :: Ternary operators |
| 1332 | * Well, there's only ?:. |
| 1333 | */ |
| 1334 | |
| 1335 | JavaCode |
| 1336 | jcConditional(JavaCode test, JavaCode truePart, JavaCode falsePart) |
| 1337 | { |
| 1338 | return jcoNew(jc0ClassObj(JCO_CLSS_Conditional), 3, test, truePart, falsePart); |
| 1339 | } |
| 1340 | |
| 1341 | localstatic void |
| 1342 | jcCondPrint(JavaCodePContext ctxt, JavaCode code) |
| 1343 | { |
| 1344 | JavaCodeClass thisClss; |
| 1345 | JavaCode arg1 = jcoArgv(code)[0]; |
| 1346 | JavaCode arg2 = jcoArgv(code)[1]; |
| 1347 | JavaCode arg3 = jcoArgv(code)[2]; |
| 1348 | |
| 1349 | thisClss = jcoClass(code)(((code)->hdr).clss); |
| 1350 | jc0PrintWithParens(ctxt, thisClss, arg1); |
| 1351 | jcoPContextWrite(ctxt, " ? "); |
| 1352 | jc0PrintWithParens(ctxt, thisClss, arg2); |
| 1353 | jcoPContextWrite(ctxt, " : "); |
| 1354 | jc0PrintWithParens(ctxt, thisClss, arg3); |
| 1355 | } |
| 1356 | |
| 1357 | /* |
| 1358 | * :: Statements |
| 1359 | */ |
| 1360 | JavaCode |
| 1361 | jcStatement(JavaCode stmt) |
| 1362 | { |
| 1363 | return jcoNew(jc0ClassObj(JCO_CLSS_Statement), 1, stmt); |
| 1364 | } |
| 1365 | |
| 1366 | localstatic void |
| 1367 | jcStatementPrint(JavaCodePContext ctxt, JavaCode code) |
| 1368 | { |
| 1369 | jcoWrite(ctxt, jcoArgv(code)[0]); |
| 1370 | jcoPContextWrite(ctxt, ";"); |
| 1371 | } |
| 1372 | |
| 1373 | /* |
| 1374 | * :: Sequences |
| 1375 | */ |
| 1376 | |
| 1377 | JavaCode |
| 1378 | jcCommaSeq(JavaCodeList lst) |
| 1379 | { |
| 1380 | return jcoNewFrList(jc0ClassObj(JCO_CLSS_CommaSeq), lst); |
| 1381 | } |
| 1382 | |
| 1383 | JavaCode |
| 1384 | jcCommaSeqP(int n, va_list argp) |
| 1385 | { |
| 1386 | return jcoNewP(jc0ClassObj(JCO_CLSS_CommaSeq), n, argp); |
| 1387 | } |
| 1388 | |
| 1389 | JavaCode |
| 1390 | jcSeq(JavaCodeList lst) |
| 1391 | { |
| 1392 | return jcoNewFrList(jc0ClassObj(JCO_CLSS_Seq), lst); |
| 1393 | } |
| 1394 | |
| 1395 | JavaCode |
| 1396 | jcSeqV(int n, ...) |
| 1397 | { |
| 1398 | va_list argp; |
| 1399 | JavaCode jc; |
| 1400 | va_start(argp, n)__builtin_va_start(argp, n); |
| 1401 | jc = jcoNewP(jc0ClassObj(JCO_CLSS_Seq), n, argp); |
| 1402 | va_end(argp)__builtin_va_end(argp); |
| 1403 | return jc; |
| 1404 | } |
| 1405 | |
| 1406 | JavaCode |
| 1407 | jcNLSeq(JavaCodeList lst) |
| 1408 | { |
| 1409 | return jcoNewFrList(jc0ClassObj(JCO_CLSS_NLSeq), lst); |
| 1410 | } |
| 1411 | |
| 1412 | JavaCode |
| 1413 | jcSpaceSeq(JavaCodeList lst) |
| 1414 | { |
| 1415 | return jcoNewFrList(jc0ClassObj(JCO_CLSS_SpaceSeq), lst); |
| 1416 | } |
| 1417 | |
| 1418 | JavaCode |
| 1419 | jcSpaceSeqV(int n, ...) |
| 1420 | { |
| 1421 | va_list argp; |
| 1422 | JavaCode jc; |
| 1423 | va_start(argp, n)__builtin_va_start(argp, n); |
| 1424 | jc = jcoNewP(jc0ClassObj(JCO_CLSS_SpaceSeq), n, argp); |
| 1425 | va_end(argp)__builtin_va_end(argp); |
| 1426 | return jc; |
| 1427 | } |
| 1428 | |
| 1429 | JavaCode |
| 1430 | jcNLSeqV(int n, ...) |
| 1431 | { |
| 1432 | va_list argp; |
| 1433 | JavaCode jc; |
| 1434 | va_start(argp, n)__builtin_va_start(argp, n); |
| 1435 | jc = jcoNewP(jc0ClassObj(JCO_CLSS_NLSeq), n, argp); |
| 1436 | va_end(argp)__builtin_va_end(argp); |
| 1437 | return jc; |
| 1438 | } |
| 1439 | |
| 1440 | localstatic void |
| 1441 | jcSequencePrint(JavaCodePContext ctxt, JavaCode code) |
| 1442 | { |
| 1443 | char *theSep = jcoClass(code)(((code)->hdr).clss)->txt; |
| 1444 | char *sep = 0; |
| 1445 | int i=0; |
| 1446 | for (i=0; i<jcoArgc(code); i++) { |
| 1447 | if (sep != 0) |
| 1448 | jcoPContextWrite(ctxt, sep); |
| 1449 | jcoWrite(ctxt, jcoArgv(code)[i]); |
| 1450 | sep = theSep; |
| 1451 | } |
| 1452 | } |
| 1453 | |
| 1454 | JavaCode |
| 1455 | jcBlockNoNL(JavaCode body) |
| 1456 | { |
| 1457 | return jcoNew(jc0ClassObj(JCO_CLSS_Braces), 1, body); |
| 1458 | } |
| 1459 | |
| 1460 | JavaCode |
| 1461 | jcBlock(JavaCode body) |
| 1462 | { |
| 1463 | return jcoNew(jc0ClassObj(JCO_CLSS_Block), 1, body); |
| 1464 | } |
| 1465 | |
| 1466 | localstatic void |
| 1467 | jcBlockPrint(JavaCodePContext ctxt, JavaCode code) |
| 1468 | { |
| 1469 | jcoPContextWrite(ctxt, "{"); |
| 1470 | jcoPContextNewlineIndent(ctxt); |
| 1471 | jcoWrite(ctxt, jcoArgv(code)[0]); |
| 1472 | jcoPContextNewlineUnindent(ctxt); |
| 1473 | jcoPContextWrite(ctxt, "}"); |
| 1474 | |
| 1475 | } |
| 1476 | |
| 1477 | JavaCode |
| 1478 | jcBreak(JavaCode label) |
| 1479 | { |
| 1480 | if (label == 0) |
| 1481 | return jcKeyword(symInternConst("break")symProbe("break", 1)); |
| 1482 | else |
| 1483 | return jcSpaceSeqV(2, jcKeyword(symInternConst("break")symProbe("break", 1)), label); |
| 1484 | } |
| 1485 | |
| 1486 | JavaCode |
| 1487 | jcContinue(JavaCode label) |
| 1488 | { |
| 1489 | if (label == 0) |
| 1490 | return jcKeyword(symInternConst("continue")symProbe("continue", 1)); |
| 1491 | else |
| 1492 | return jcSpaceSeqV(2, jcKeyword(symInternConst("continue")symProbe("continue", 1)), label); |
| 1493 | } |
| 1494 | |
| 1495 | JavaCode |
| 1496 | jcCaseLabel(JavaCode label) |
| 1497 | { |
| 1498 | return jcoNew(jc0ClassObj(JCO_CLSS_Case), 1, label); |
| 1499 | } |
| 1500 | |
| 1501 | localstatic void |
| 1502 | jcCasePrint(JavaCodePContext ctxt, JavaCode code) |
| 1503 | { |
| 1504 | jcoPContextWrite(ctxt, "case "); |
| 1505 | jcoWrite(ctxt, jcoArgv(code)[0]); |
| 1506 | jcoPContextWrite(ctxt, ": "); |
| 1507 | } |
| 1508 | |
| 1509 | JavaCode |
| 1510 | jcTryCatch(JavaCode body, JavaCode catch, JavaCode finally) |
| 1511 | { |
| 1512 | return jcTry(body, listSingleton(JavaCode)(JavaCode_listPointer->Singleton)(catch), finally); |
| 1513 | } |
| 1514 | |
| 1515 | JavaCode |
| 1516 | jcTry(JavaCode body, JavaCodeList catches, JavaCode finally) |
| 1517 | { |
| 1518 | JavaCodeList lst = listNil(JavaCode)((JavaCodeList) 0); |
| 1519 | lst = listCons(JavaCode)(JavaCode_listPointer->Cons)(jcoNew(jc0ClassObj(JCO_CLSS_Try), 1, body), lst); |
| 1520 | lst = listNConcat(JavaCode)(JavaCode_listPointer->NConcat)(lst, catches); |
| 1521 | if (finally != NULL((void*)0)) { |
| 1522 | lst = listNConcat(JavaCode)(JavaCode_listPointer->NConcat)(lst, |
| 1523 | listSingleton(JavaCode)(JavaCode_listPointer->Singleton)(jcoNew(jc0ClassObj(JCO_CLSS_Finally), |
| 1524 | 1, finally))); |
| 1525 | } |
| 1526 | return jcNLSeq(lst); |
| 1527 | } |
| 1528 | |
| 1529 | JavaCode |
| 1530 | jcCatch(JavaCode decl, JavaCode body) |
| 1531 | { |
| 1532 | return jcoNew(jc0ClassObj(JCO_CLSS_Catch), 2, decl, body); |
| 1533 | } |
| 1534 | |
| 1535 | /* |
| 1536 | * :: Import, Package |
| 1537 | */ |
| 1538 | |
| 1539 | JavaCode |
| 1540 | jcImport(JavaCode arg) |
| 1541 | { |
| 1542 | return jcSpaceSeqV(2, jcKeyword(symInternConst("import")symProbe("import", 1)), arg); |
| 1543 | } |
| 1544 | |
| 1545 | JavaCode |
| 1546 | jcPackage(JavaCode arg) |
| 1547 | { |
| 1548 | return jcSpaceSeqV(2, jcKeyword(symInternConst("package")symProbe("package", 1)), arg); |
| 1549 | } |
| 1550 | |
| 1551 | /* |
| 1552 | * :: Throw, catch |
| 1553 | */ |
| 1554 | |
| 1555 | JavaCode |
| 1556 | jcThrow(JavaCode arg) |
| 1557 | { |
| 1558 | return jcSpaceSeqV(2, jcKeyword(symInternConst("throw")symProbe("throw", 1)), arg); |
| 1559 | } |
| 1560 | |
| 1561 | /* |
| 1562 | * :: If, Switch, While |
| 1563 | */ |
| 1564 | |
| 1565 | JavaCode |
| 1566 | jcIf(JavaCode test, JavaCode stmt) |
| 1567 | { |
| 1568 | return jcoNew(jc0ClassObj(JCO_CLSS_If), 2, test, stmt); |
| 1569 | } |
| 1570 | |
| 1571 | JavaCode |
| 1572 | jcSwitch(JavaCode test, JavaCodeList bodyList) |
| 1573 | { |
| 1574 | JavaCode block = jcBraces(jcNLSeq(bodyList)); |
| 1575 | return jcoNew(jc0ClassObj(JCO_CLSS_Switch), 2, test, block); |
| 1576 | } |
| 1577 | |
| 1578 | JavaCode |
| 1579 | jcWhile(JavaCode test, JavaCode stmt) |
| 1580 | { |
| 1581 | return jcoNew(jc0ClassObj(JCO_CLSS_While), 2, test, stmt); |
| 1582 | } |
| 1583 | |
| 1584 | localstatic void |
| 1585 | jcBlockHdrPrint(JavaCodePContext ctxt, JavaCode code) |
| 1586 | { |
| 1587 | Bool needsIndent; |
| 1588 | char *key = (char *) jcoClass(code)(((code)->hdr).clss)->txt; |
| 1589 | jcoPContextWrite(ctxt, key); |
| 1590 | jcoPContextWrite(ctxt, " ("); |
| 1591 | jcoWrite(ctxt, jcoArgv(code)[0]); |
| 1592 | jcoPContextWrite(ctxt, ") "); |
| 1593 | |
| 1594 | needsIndent = jcBlockHdrIndent(jcoArgv(code)[1]); |
| 1595 | if (needsIndent) |
| 1596 | jcoPContextNewlineIndent(ctxt); |
| 1597 | jcoWrite(ctxt, jcoArgv(code)[1]); |
| 1598 | if (needsIndent) |
| 1599 | jcoPContextNewlineUnindent(ctxt); |
| 1600 | |
| 1601 | } |
| 1602 | |
| 1603 | localstatic void |
| 1604 | jcBlockKeywordPrint(JavaCodePContext ctxt, JavaCode code) |
| 1605 | { |
| 1606 | Bool needsIndent; |
| 1607 | char *key = (char *) jcoClass(code)(((code)->hdr).clss)->txt; |
| 1608 | jcoPContextWrite(ctxt, key); |
| 1609 | jcoPContextWrite(ctxt, " "); |
| 1610 | |
| 1611 | needsIndent = jcBlockHdrIndent(jcoArgv(code)[0]); |
| 1612 | if (needsIndent) |
| 1613 | jcoPContextNewlineIndent(ctxt); |
| 1614 | jcoWrite(ctxt, jcoArgv(code)[0]); |
| 1615 | if (needsIndent) |
| 1616 | jcoPContextNewlineUnindent(ctxt); |
| 1617 | |
| 1618 | } |
| 1619 | |
| 1620 | localstatic Bool |
| 1621 | jcBlockHdrIndent(JavaCode code) |
| 1622 | { |
| 1623 | return jcoClass(code)(((code)->hdr).clss)->id != JCO_CLSS_Braces |
| 1624 | && jcoClass(code)(((code)->hdr).clss)->id != JCO_CLSS_Block; |
| 1625 | } |
| 1626 | |
| 1627 | /* |
| 1628 | * :: File |
| 1629 | */ |
| 1630 | |
| 1631 | JavaCode |
| 1632 | jcFile(JavaCode pkg, JavaCode name, JavaCodeList imports, JavaCode body) |
| 1633 | { |
| 1634 | JavaCodeList whole; |
| 1635 | JavaCode file; |
| 1636 | |
| 1637 | whole = listNil(JavaCode)((JavaCodeList) 0); |
| 1638 | if (pkg != NULL((void*)0)) { |
| 1639 | whole = listSingleton(JavaCode)(JavaCode_listPointer->Singleton)(jcStatement(jcPackage(jcoCopy(pkg)))); |
| 1640 | } |
| 1641 | whole = listNConcat(JavaCode)(JavaCode_listPointer->NConcat)(whole, imports); |
| 1642 | whole = listNConcat(JavaCode)(JavaCode_listPointer->NConcat)(whole, listSingleton(JavaCode)(JavaCode_listPointer->Singleton)(body)); |
| 1643 | |
| 1644 | file = jcoNew(jc0ClassObj(JCO_CLSS_File), |
| 1645 | 3, name, jcoCopy(pkg), jcNLSeq(whole)); |
| 1646 | |
| 1647 | jcoFree(pkg); |
| 1648 | |
| 1649 | return file; |
| 1650 | } |
| 1651 | |
| 1652 | void |
| 1653 | jcFilePrint(JavaCodePContext ctxt, JavaCode code) |
| 1654 | { |
| 1655 | jcoWrite(ctxt, jcoArgv(code)[2]); |
| 1656 | } |
| 1657 | |
| 1658 | String |
| 1659 | jcFileClassName(JavaCode file) |
| 1660 | { |
| 1661 | return jcIdName(jcoArgv(file)[0]); |
| 1662 | } |
| 1663 | |
| 1664 | String |
| 1665 | jcFilePackageName(JavaCode file) |
| 1666 | { |
| 1667 | if (jcoArgv(file)[1] == NULL((void*)0)) { |
| 1668 | return ""; |
| 1669 | } |
| 1670 | else { |
| 1671 | return jcIdName(jcoArgv(file)[1]); |
| 1672 | } |
| 1673 | } |
| 1674 | |
| 1675 | /* |
| 1676 | * :: Generic operations |
| 1677 | */ |
| 1678 | SExpr |
| 1679 | jcNodeSExpr(JavaCode code) |
| 1680 | { |
| 1681 | Symbol sym = symIntern(jcoClass(code)->name)symProbe((((code)->hdr).clss)->name, 1 | 2); |
| 1682 | SExpr whole = sxiList(1, sxiFrSymbol(sym)); |
| 1683 | int i=0; |
| 1684 | |
| 1685 | for (i=0; i<jcoArgc(code); i++) { |
| 1686 | JavaCode jc = jcoArgv(code)[i]; |
| 1687 | SExpr sexpr = jc == NULL((void*)0) ? sxNil : jcoSExpr(jc); |
| 1688 | whole = sxCons(sexpr, whole); |
| 1689 | } |
| 1690 | return sxNReverse(whole); |
| 1691 | } |
| 1692 | |
| 1693 | extern void |
| 1694 | jcNodePrint(JavaCodePContext ctxt, JavaCode code) |
| 1695 | { |
| 1696 | jcoPContextWrite(ctxt, "<<node>>"); |
| 1697 | } |
| 1698 | |
| 1699 | |
| 1700 | void |
| 1701 | jcListPrint(JavaCodePContext ctxt, JavaCode code) |
| 1702 | { |
| 1703 | char *theSep = (char *) jcoClass(code)(((code)->hdr).clss)->txt; |
| 1704 | char *sep = ""; |
| 1705 | int i; |
| 1706 | for (i=0; i<jcoArgc(code); i++) { |
| 1707 | jcoPContextWrite(ctxt, sep); |
| 1708 | jcoWrite(ctxt, jcoArgv(code)[i]); |
| 1709 | sep = theSep; |
| 1710 | } |
| 1711 | } |
| 1712 | |
| 1713 | |
| 1714 | /* |
| 1715 | * :: Utils |
| 1716 | */ |
| 1717 | localstatic Symbol jc0ModifierSymbol(int idx); |
| 1718 | |
| 1719 | localstatic JavaCodeClass |
| 1720 | jc0ClassObj(JcClassId id) |
| 1721 | { |
| 1722 | JavaCodeClass clss = &jcClss[id]; |
| 1723 | assert(clss->id == id)do { if (!(clss->id == id)) _do_assert(("clss->id == id" ),"java/javacode.c",1723); } while (0); |
| 1724 | return clss; |
| 1725 | } |
| 1726 | |
| 1727 | struct jcModifierInfo { |
| 1728 | int mask; |
| 1729 | String txt; |
| 1730 | Symbol sym; |
| 1731 | }; |
| 1732 | |
| 1733 | |
| 1734 | static struct jcModifierInfo jcModifierList[] = { |
| 1735 | { JCO_MOD_Public, "public"}, |
| 1736 | { JCO_MOD_Private, "private"}, |
| 1737 | { JCO_MOD_Protected, "protected"}, |
| 1738 | { JCO_MOD_Static, "static"}, |
| 1739 | { JCO_MOD_Final, "final"}, |
| 1740 | { JCO_MOD_Transient, "transient"}, |
| 1741 | { JCO_MOD_Volatile, "volatile"}, |
| 1742 | }; |
| 1743 | |
| 1744 | localstatic JavaCodeList |
| 1745 | jc0CreateModifiers(int modifiers) |
| 1746 | { |
| 1747 | JavaCodeList l = listNil(JavaCode)((JavaCodeList) 0); |
| 1748 | int i=0, m; |
| 1749 | for (m=1; m< JCO_MOD_MAX; m=m<<1) { |
| 1750 | if (modifiers & m) |
| 1751 | l = listCons(JavaCode)(JavaCode_listPointer->Cons)(jcoNewToken(jc0ClassObj(JCO_CLSS_Keyword), |
| 1752 | jc0ModifierSymbol(i)), l); |
| 1753 | i++; |
| 1754 | } |
| 1755 | return listNReverse(JavaCode)(JavaCode_listPointer->NReverse)(l); |
| 1756 | } |
| 1757 | |
| 1758 | localstatic Symbol |
| 1759 | jc0ModifierSymbol(int idx) |
| 1760 | { |
| 1761 | struct jcModifierInfo *inf = &jcModifierList[idx]; |
| 1762 | if (inf->sym == NULL((void*)0)) |
| 1763 | inf->sym = symInternConst(inf->txt)symProbe(inf->txt, 1); |
| 1764 | return inf->sym; |
| 1765 | } |
| 1766 | |
| 1767 | |
| 1768 | localstatic Bool jc0ImportEq(JavaCode c1, JavaCode c2); |
| 1769 | localstatic void jc0CollectImports(Table tbl, Table nameTbl, JavaCode code); |
| 1770 | |
| 1771 | JavaCodeList |
| 1772 | jcCollectImports(JavaCode code) |
| 1773 | { |
| 1774 | Table tbl = tblNew((TblHashFun) jcoHash, (TblEqFun) jc0ImportEq); |
| 1775 | Table nameTbl = tblNew((TblHashFun) strHash, (TblEqFun) strEqual); |
| 1776 | TableIterator it; |
| 1777 | JavaCodeList resList = listNil(JavaCode)((JavaCodeList) 0); |
| 1778 | |
| 1779 | jc0CollectImports(tbl, nameTbl, code); |
| 1780 | |
| 1781 | for (tblITER(it, tbl)_tblITER(&(it), tbl); tblMORE(it)((it).curr <= (it).last); tblSTEP(it)((((it).link=(it).link->next))==0 ? _tblSTEP(&(it)) : 1 )) { |
| 1782 | JavaCode id = (JavaCode) tblKEY(it)((it).link->key); |
| 1783 | JavaCodeList codes = (JavaCodeList) tblELT(it)((it).link->elt); |
| 1784 | JavaCodeList tmp; |
| 1785 | JavaCode copy = jcImportedId(jcoImportPkg(id), jcoImportId(id)); |
| 1786 | JavaCodeList usages = (JavaCodeList) tblElt(nameTbl, jcoImportId(id), listNil(JavaCode)((JavaCodeList) 0)); |
| 1787 | if (cdr(usages)((usages)->rest) != listNil(JavaCode)((JavaCodeList) 0)) { |
| 1788 | continue; |
| 1789 | } |
| 1790 | else { |
| 1791 | resList = listCons(JavaCode)(JavaCode_listPointer->Cons)(copy, resList); |
| 1792 | tmp = codes; |
| 1793 | while (tmp != 0) { |
| 1794 | JavaCode imp = car(tmp)((tmp)->first); |
| 1795 | jcoImportSetImported(imp, true1); |
| 1796 | tmp = cdr(tmp)((tmp)->rest); |
| 1797 | } |
| 1798 | listFree(JavaCode)(JavaCode_listPointer->Free)(codes); |
| 1799 | } |
| 1800 | } |
| 1801 | return resList; |
| 1802 | } |
| 1803 | |
| 1804 | localstatic Bool |
| 1805 | jc0ImportEq(JavaCode c1, JavaCode c2) |
| 1806 | { |
| 1807 | assert(jcoIsImport(c1) && jcoIsImport(c2))do { if (!(((((c1)->hdr).tag) == JCO_IMPORT) && (( ((c2)->hdr).tag) == JCO_IMPORT))) _do_assert(("jcoIsImport(c1) && jcoIsImport(c2)" ),"java/javacode.c",1807); } while (0); |
| 1808 | |
| 1809 | if (strcmp(jcoImportPkg(c1), jcoImportPkg(c2)) != 0) |
| 1810 | return false((int) 0); |
| 1811 | if (strcmp(jcoImportId(c1), jcoImportId(c2)) != 0) |
| 1812 | return false((int) 0); |
| 1813 | |
| 1814 | return true1; |
| 1815 | } |
| 1816 | |
| 1817 | |
| 1818 | localstatic void |
| 1819 | jc0CollectImports(Table tbl, Table nameTbl, JavaCode code) |
| 1820 | { |
| 1821 | if (code == 0) |
| 1822 | return; |
| 1823 | if (jcoIsImport(code)((((code)->hdr).tag) == JCO_IMPORT)) { |
| 1824 | JavaCodeList l = (JavaCodeList) tblElt(tbl, code, listNil(JavaCode)((JavaCodeList) 0)); |
| 1825 | JavaCode key = code; |
| 1826 | l = listCons(JavaCode)(JavaCode_listPointer->Cons)(code, l); |
| 1827 | tblSetElt(tbl, key, l); |
| 1828 | |
| 1829 | String name = jcoImportId(code); |
| 1830 | JavaCodeList ids = (JavaCodeList) tblElt(nameTbl, name, listNil(JavaCode)((JavaCodeList) 0)); |
| 1831 | if (!listMember(JavaCode)(JavaCode_listPointer->Member)(ids, code, jc0ImportEq)) { |
| 1832 | tblSetElt(nameTbl, name, (TblElt) listCons(JavaCode)(JavaCode_listPointer->Cons)(code, ids)); |
| 1833 | } |
| 1834 | } |
| 1835 | if (jcoIsNode(code)((((code)->hdr).tag) == JCO_JAVA)) { |
| 1836 | int i=0; |
| 1837 | for (i=0; i<jcoArgc(code); i++) { |
| 1838 | jc0CollectImports(tbl, nameTbl, jcoArgv(code)[i]); |
| 1839 | } |
| 1840 | } |
| 1841 | } |
| 1842 | |
| 1843 | /* |
| 1844 | * Returns a newly allocated string with properly escaped characters. |
| 1845 | */ |
| 1846 | localstatic String |
| 1847 | jc0EscapeString(String s, Bool addTerminalChar) |
| 1848 | { |
| 1849 | Buffer buf; |
| 1850 | buf = bufNew(); |
| 1851 | while (*s != 0) { |
| 1852 | switch (*s) { |
| 1853 | case '\\': |
| 1854 | bufPuts(buf, "\\\\"); |
| 1855 | break; |
| 1856 | case '\n': |
| 1857 | bufPuts(buf, "\\\n"); |
| 1858 | break; |
| 1859 | case '\"': |
| 1860 | bufPuts(buf, "\\\""); |
| 1861 | break; |
| 1862 | default: |
| 1863 | bufPutc(buf, *s); |
| 1864 | break; |
| 1865 | } |
| 1866 | s++; |
| 1867 | } |
| 1868 | if (addTerminalChar) { |
| 1869 | bufPutc(buf, '\\'); |
| 1870 | bufPutc(buf, '0'); |
| 1871 | } |
| 1872 | |
| 1873 | return bufLiberate(buf); |
| 1874 | } |
| 1875 | |
| 1876 | /* |
| 1877 | * :: Names and so on |
| 1878 | */ |
| 1879 | |
| 1880 | localstatic Bool jcIsId(String word); |
| 1881 | |
| 1882 | Bool |
| 1883 | jcIsLegalClassName(String word) |
| 1884 | { |
| 1885 | int i; |
| 1886 | if (!jcIsId(word)) |
| 1887 | return false((int) 0); |
| 1888 | |
| 1889 | for (i=0; i<JK_END; i++) { |
| 1890 | if (strcmp(word, jkKeywords[i].text) == 0) |
| 1891 | return false((int) 0); |
| 1892 | } |
| 1893 | return true1; |
| 1894 | } |
| 1895 | |
| 1896 | localstatic Bool |
| 1897 | jcIsId(String word) |
| 1898 | { |
| 1899 | char *p = word; |
| 1900 | if (word[0] == '\0') |
| 1901 | return false((int) 0); |
| 1902 | if (!(word[0] == '_' || isalpha(word[0])((*__ctype_b_loc ())[(int) ((word[0]))] & (unsigned short int) _ISalpha))) |
| 1903 | return false((int) 0); |
| 1904 | while (*p != '\0') { |
| 1905 | if (!(*p == '_' || isalnum(*p)((*__ctype_b_loc ())[(int) ((*p))] & (unsigned short int) _ISalnum))) |
| 1906 | return false((int) 0); |
| 1907 | p++; |
| 1908 | } |
| 1909 | |
| 1910 | return true1; |
| 1911 | } |