
1 // 2 // Copyright (c) 2006, Brian Frank and Andy Frank 3 // Licensed under the Academic Free License version 3.0 4 // 5 // History: 6 // 18 May 06 Brian Frank Creation 7 // 8 9 ** 10 ** CompilerSupport provides lots of convenience methods for classes 11 ** used during the compiler pipeline. 12 ** 13 class CompilerSupport 14 { 15 16 ////////////////////////////////////////////////////////////////////////// 17 // Construction 18 ////////////////////////////////////////////////////////////////////////// 19 20 ** 21 ** Constructor takes the associated Compiler 22 ** 23 new make(Compiler compiler) 24 { 25 this.compiler = compiler 26 } 27 28 ////////////////////////////////////////////////////////////////////////// 29 // Convenience 30 ////////////////////////////////////////////////////////////////////////// 31 32 ** 33 ** Convenience for compiler.ns 34 ** 35 Namespace ns() 36 { 37 return compiler.ns 38 } 39 40 ** 41 ** Convenience for compiler.pod 42 ** 43 PodDef pod() 44 { 45 return compiler.pod 46 } 47 48 ** 49 ** Convenience for compiler.pod.units 50 ** 51 CompilationUnit[] units() 52 { 53 return compiler.pod.units 54 } 55 56 ** 57 ** Convenience for compiler.types 58 ** 59 TypeDef[] types() 60 { 61 return compiler.types 62 } 63 64 ** 65 ** Convenience for compiler.log 66 ** 67 CompilerLog log() 68 { 69 return compiler.log 70 } 71 72 ////////////////////////////////////////////////////////////////////////// 73 // Utils 74 ////////////////////////////////////////////////////////////////////////// 75 76 ** 77 ** Add a synthetic type 78 ** 79 Void addTypeDef(TypeDef t) 80 { 81 t.unit.types.add(t) 82 pod.typeDefs[t.name] = t 83 compiler.types.add(t) 84 } 85 86 ////////////////////////////////////////////////////////////////////////// 87 // Errors 88 ////////////////////////////////////////////////////////////////////////// 89 90 ** 91 ** Create, log, and return a CompilerErr. 92 ** 93 virtual CompilerErr err(Str msg, Location loc) 94 { 95 return errReport(CompilerErr.make(msg, loc)) 96 } 97 98 ** 99 ** Log, store, and return the specified CompilerErr. 100 ** 101 CompilerErr errReport(CompilerErr e) 102 { 103 if (compiler != null) 104 { 105 compiler.log.compilerErr(e) 106 compiler.errors.add(e) 107 } 108 return e 109 } 110 111 ** 112 ** If any errors are accumulated, then throw the last one 113 ** 114 Void bombIfErr() 115 { 116 if (!compiler.errors.isEmpty) 117 throw compiler.errors.last 118 } 119 120 ////////////////////////////////////////////////////////////////////////// 121 // Fields 122 ////////////////////////////////////////////////////////////////////////// 123 124 Compiler compiler 125 126 }