1 /////////////////////////////////////////////////////////////////////////// 2 // Copyright (c) 2013 Academy of Motion Picture Arts and Sciences 3 // ("A.M.P.A.S."). Portions contributed by others as indicated. 4 // All rights reserved. 5 // 6 // A worldwide, royalty-free, non-exclusive right to copy, modify, create 7 // derivatives, and use, in source and binary forms, is hereby granted, 8 // subject to acceptance of this license. Performance of any of the 9 // aforementioned acts indicates acceptance to be bound by the following 10 // terms and conditions: 11 // 12 // * Copies of source code, in whole or in part, must retain the 13 // above copyright notice, this list of conditions and the 14 // Disclaimer of Warranty. 15 // 16 // * Use in binary form must retain the above copyright notice, 17 // this list of conditions and the Disclaimer of Warranty in the 18 // documentation and/or other materials provided with the distribution. 19 // 20 // * Nothing in this license shall be deemed to grant any rights to 21 // trademarks, copyrights, patents, trade secrets or any other 22 // intellectual property of A.M.P.A.S. or any contributors, except 23 // as expressly stated herein. 24 // 25 // * Neither the name "A.M.P.A.S." nor the name of any other 26 // contributors to this software may be used to endorse or promote 27 // products derivative of or based on this software without express 28 // prior written permission of A.M.P.A.S. or the contributors, as 29 // appropriate. 30 // 31 // This license shall be construed pursuant to the laws of the State of 32 // California, and any disputes related thereto shall be subject to the 33 // jurisdiction of the courts therein. 34 // 35 // Disclaimer of Warranty: THIS SOFTWARE IS PROVIDED BY A.M.P.A.S. AND 36 // CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, 37 // BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS 38 // FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT ARE DISCLAIMED. IN NO 39 // EVENT SHALL A.M.P.A.S., OR ANY CONTRIBUTORS OR DISTRIBUTORS, BE LIABLE 40 // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, RESITUTIONARY, 41 // OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 42 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 43 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 44 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 45 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 46 // THE POSSIBILITY OF SUCH DAMAGE. 47 // 48 // WITHOUT LIMITING THE GENERALITY OF THE FOREGOING, THE ACADEMY 49 // SPECIFICALLY DISCLAIMS ANY REPRESENTATIONS OR WARRANTIES WHATSOEVER 50 // RELATED TO PATENT OR OTHER INTELLECTUAL PROPERTY RIGHTS IN THE ACADEMY 51 // COLOR ENCODING SYSTEM, OR APPLICATIONS THEREOF, HELD BY PARTIES OTHER 52 // THAN A.M.P.A.S., WHETHER DISCLOSED OR UNDISCLOSED. 53 /////////////////////////////////////////////////////////////////////////// 54 55 56 #ifndef INCLUDED_CTL_L_CONTEXT_H 57 #define INCLUDED_CTL_L_CONTEXT_H 58 59 //----------------------------------------------------------------------------- 60 // 61 // class LContext 62 // 63 // An LContext ("load-time context") keeps track of information 64 // that is important while a CTL module is being loaded, such 65 // as a pointer to the module that is being constructed or whether 66 // any errors have been encountered. (In order to give good 67 // feedback to the user, module loading continues even if errors 68 // are found, but a module with errors is unloaded and discarded 69 // immediately after loading.) 70 // 71 // The LContext also serves as a factory for syntax tree nodes 72 // and type objects. 73 // 74 //----------------------------------------------------------------------------- 75 76 #include <CtlType.h> 77 #include <CtlSyntaxTree.h> 78 #include <CtlErrors.h> 79 #include <string> 80 #include <set> 81 82 namespace Ctl { 83 84 class Module; 85 class SymbolTable; 86 87 88 class LContext 89 { 90 public: 91 92 virtual ~LContext (); 93 94 //---------------------------------------------------------- 95 // Access to the file that contains the module's source code 96 //---------------------------------------------------------- 97 file()98 std::istream & file () {return _file;} 99 const std::string & fileName () const; 100 101 //------------------------------------ 102 // Access to the module we are loading 103 //------------------------------------ 104 module()105 Module * module () {return _module;} module()106 const Module * module () const {return _module;} 107 108 109 //--------------------------- 110 // Access to the symbol table 111 //--------------------------- 112 symtab()113 SymbolTable & symtab () {return _symtab;} symtab()114 const SymbolTable & symtab () const {return _symtab;} 115 116 117 //--------------------------------------------------------- 118 // Error counter, used to keep track of whether errors were 119 // encountered while attempting to load the module 120 //--------------------------------------------------------- 121 122 void foundError (int lineNumber, Error e); 123 void declareError (int lineNumber, Error e); 124 bool errorDeclared (int lineNumber, Error e); 125 void catchErrors (); 126 int numErrors () const; 127 void printDeclaredErrors() const; 128 129 130 //-------------------------------------------------------- 131 // Methods called by the parser to generate data addresses 132 //-------------------------------------------------------- 133 134 virtual void newStackFrame () = 0; 135 136 virtual AddrPtr parameterAddr (const DataTypePtr ¶meterType) = 0; 137 virtual AddrPtr returnValueAddr (const DataTypePtr &returnType) = 0; 138 virtual AddrPtr autoVariableAddr (const DataTypePtr &variableType) = 0; 139 140 141 //----------------------------------------------- 142 // Factory for syntax tree nodes and type objects 143 //----------------------------------------------- 144 145 virtual ModuleNodePtr newModuleNode 146 (int lineNumber, 147 const StatementNodePtr &constants, 148 const FunctionNodePtr &functions) 149 const = 0; 150 151 virtual FunctionNodePtr newFunctionNode 152 (int lineNumber, 153 const std::string &name, 154 const SymbolInfoPtr &info, 155 const StatementNodePtr &body) 156 const = 0; 157 158 virtual VariableNodePtr newVariableNode 159 (int lineNumber, 160 const std::string &name, 161 const SymbolInfoPtr &info, 162 const ExprNodePtr &initialValue, 163 bool assignInitialValue) 164 const = 0; 165 166 virtual AssignmentNodePtr newAssignmentNode 167 (int lineNumber, 168 const ExprNodePtr &lhs, 169 const ExprNodePtr &rhs) 170 const = 0; 171 172 virtual ExprStatementNodePtr newExprStatementNode 173 (int lineNumber, 174 const ExprNodePtr &expr) 175 const = 0; 176 177 virtual IfNodePtr newIfNode 178 (int lineNumber, 179 const ExprNodePtr &condition, 180 const StatementNodePtr &truePath, 181 const StatementNodePtr &falsePath) 182 const = 0; 183 184 virtual ReturnNodePtr newReturnNode 185 (int lineNumber, 186 const SymbolInfoPtr &info, 187 const ExprNodePtr &returnedValue) 188 const = 0; 189 190 virtual WhileNodePtr newWhileNode 191 (int lineNumber, 192 const ExprNodePtr &condition, 193 const StatementNodePtr &loopBody) 194 const = 0; 195 196 virtual BinaryOpNodePtr newBinaryOpNode 197 (int lineNumber, 198 Token op, 199 const ExprNodePtr &leftOperand, 200 const ExprNodePtr &rightOperand) 201 const = 0; 202 203 virtual UnaryOpNodePtr newUnaryOpNode 204 (int lineNumber, 205 Token op, 206 const ExprNodePtr &operand) 207 const = 0; 208 209 virtual ArrayIndexNodePtr newArrayIndexNode 210 (int lineNumber, 211 const ExprNodePtr &array, 212 const ExprNodePtr &index) 213 const = 0; 214 215 virtual SizeNodePtr newSizeNode 216 (int lineNumber, 217 const ExprNodePtr &obj) 218 const = 0; 219 220 virtual MemberNodePtr newMemberNode 221 (int lineNumber, 222 const ExprNodePtr &obj, 223 const std::string &member) 224 const = 0; 225 226 virtual NameNodePtr newNameNode 227 (int lineNumber, 228 const std::string &name, 229 const SymbolInfoPtr &info) 230 const = 0; 231 232 virtual BoolLiteralNodePtr newBoolLiteralNode 233 (int lineNumber, bool value) const = 0; 234 235 virtual IntLiteralNodePtr newIntLiteralNode 236 (int lineNumber, int value) const = 0; 237 238 virtual UIntLiteralNodePtr newUIntLiteralNode 239 (int lineNumber, unsigned value) const = 0; 240 241 virtual HalfLiteralNodePtr newHalfLiteralNode 242 (int lineNumber, half value) const = 0; 243 244 virtual FloatLiteralNodePtr newFloatLiteralNode 245 (int lineNumber, float value) const = 0; 246 247 virtual StringLiteralNodePtr newStringLiteralNode 248 (int lineNumber, 249 const std::string &value) 250 const = 0; 251 252 virtual CallNodePtr newCallNode 253 (int lineNumber, 254 const NameNodePtr &function, 255 const ExprNodeVector &arguments) 256 const = 0; 257 258 virtual ValueNodePtr newValueNode 259 (int lineNumber, 260 const ExprNodeVector &elements) 261 const = 0; 262 263 virtual VoidTypePtr newVoidType () const = 0; 264 265 virtual BoolTypePtr newBoolType () const = 0; 266 267 virtual IntTypePtr newIntType () const = 0; 268 269 virtual UIntTypePtr newUIntType () const = 0; 270 271 virtual HalfTypePtr newHalfType () const = 0; 272 273 virtual FloatTypePtr newFloatType () const = 0; 274 275 virtual StringTypePtr newStringType () const = 0; 276 277 enum ArrayTypeUsage { PARAMETER, NON_PARAMETER }; 278 virtual ArrayTypePtr newArrayType 279 (const DataTypePtr &baseType, 280 int size, 281 ArrayTypeUsage usage = NON_PARAMETER) = 0; 282 283 // Create a multidimensional array 284 virtual ArrayTypePtr newArrayType 285 (const DataTypePtr &baseType, 286 SizeVector sizes, 287 ArrayTypeUsage usage = NON_PARAMETER); 288 289 virtual StructTypePtr newStructType 290 (const std::string &name, 291 const MemberVector &members) const = 0; 292 293 virtual FunctionTypePtr newFunctionType 294 (const DataTypePtr &returnType, 295 bool returnVarying, 296 const ParamVector ¶meters) const = 0; 297 protected: 298 299 LContext (std::istream &file, 300 Module *module, 301 SymbolTable &symtab); 302 303 private: 304 305 std::istream & _file; 306 Module * _module; 307 SymbolTable & _symtab; 308 309 std::set<LineError> _lineErrors; 310 std::set<LineError> _declErrors; 311 312 }; 313 314 315 } // namespace Ctl 316 317 #endif 318