Debian Codegraph @

src/core/chuck_compile.h

raw symbols SCIP
1 /*----------------------------------------------------------------------------
2 ChucK Strongly-timed Audio Programming Language
3 Compiler, Virtual Machine, and Synthesis Engine
4
5 Copyright (c) 2003 Ge Wang and Perry R. Cook. All rights reserved.
6 http://chuck.stanford.edu/
7 http://chuck.cs.princeton.edu/
8
9 This program is free software; you can redistribute it and/or modify
10 it under the dual-license terms of EITHER the MIT License OR the GNU
11 General Public License (the latter as published by the Free Software
12 Foundation; either version 2 of the License or, at your option, any
13 later version).
14
15 This program is distributed in the hope that it will be useful and/or
16 interesting, but WITHOUT ANY WARRANTY; without even the implied warranty
17 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 MIT Licence and/or the GNU General Public License for details.
19
20 You should have received a copy of the MIT License and the GNU General
21 Public License (GPL) along with this program; a copy of the GPL can also
22 be obtained by writing to the Free Software Foundation, Inc., 59 Temple
23 Place, Suite 330, Boston, MA 02111-1307 U.S.A.
24 -----------------------------------------------------------------------------*/
25
26 //-----------------------------------------------------------------------------
27 // file: chuck_compile.h
28 // desc: chuck compile system unifying parser, type checker, and emitter
29 //
30 // author: Ge Wang ([email protected] | [email protected])
31 // date: Autumn 2005 - original
32 // Autumn 2017 - major refactor (with Jack Atherton)
33 // Summer 2023 - major update to error reporting
34 // Autumn 2024 - import system (with Nick Shaheed)
35 //-----------------------------------------------------------------------------
36 #ifndef __CHUCK_COMPILE_H__
37 #define __CHUCK_COMPILE_H__
38
39 #include "chuck_def.h"
40 #include "chuck_parse.h"
41 #include "chuck_scan.h"
42 #include "chuck_type.h"
43 #include "chuck_emit.h"
44 #include "chuck_vm.h"
45 #include <list>
46 #include <set>
47 #include <string>
48
49
50
51
52 //-----------------------------------------------------------------------------
53 // forward reference
54 //-----------------------------------------------------------------------------
55 struct Chuck_DLL;
56 struct Chuck_CompileTarget;
57
58
59
60
61 //-----------------------------------------------------------------------------
62 // compilation state, e.g., for compile targets
63 //-----------------------------------------------------------------------------
64 enum te_CompileState
65 {
66 te_compile_inprogress,
67 te_compile_complete
68 };
69
70
71
72
73 //-----------------------------------------------------------------------------
74 // name: intList
75 // desc: a linked list of token positions by line | moved from errmsg
76 //-----------------------------------------------------------------------------
77 typedef struct intList { t_CKINT i; struct intList *rest; } *IntList;
78 // constructor
79 static IntList intList( t_CKINT i, IntList rest )
80 {
81 IntList l = (IntList)checked_malloc(sizeof *l);
82 l->i=i; l->rest=rest;
83 return l;
84 }
85
86
87
88
89 //-----------------------------------------------------------------------------
90 // name: struct ImportTargetNode
91 // desc: a specific import node, corresponding to a particular @import
92 //-----------------------------------------------------------------------------
93 struct ImportTargetNode
94 {
95 Chuck_CompileTarget * target; // actual target
96 t_CKINT where; // pos
97 t_CKINT line; // line
98
99 ImportTargetNode( Chuck_CompileTarget * t = NULL, t_CKINT wh = 0, t_CKINT ln = 0 )
100 : target( t ), where( wh ), line( ln ) { }
101 };
102
103
104
105
106 //-----------------------------------------------------------------------------
107 // name: struct Chuck_CompileTarget
108 // desc: an compilation target
109 //-----------------------------------------------------------------------------
110 struct Chuck_CompileTarget
111 {
112 public:
113 // constructor
114 Chuck_CompileTarget( te_HowMuch extent = te_do_all )
115 : state(te_compile_inprogress), howMuch(extent), isSystemImport(FALSE),
116 fd2parse(NULL), chugin(NULL), lineNum(1), tokPos(0),
117 AST(NULL), timestamp(0), the_chuck(NULL)
118 {
119 // initialize
120 the_linePos = intList( 0, NULL );
121 }
122
123 // destructor
124 virtual ~Chuck_CompileTarget()
125 { cleanup(); CK_SAFE_FREE( the_linePos ); }
126
127 // performs cleanup (e.g., close file desriptors and reclaims AST)
128 // post-parser prep for target to be archived in registry
129 void cleanup();
130 // clean up AST
131 void cleanupAST();
132
133 public:
134 // get filename
135 std::string getFilename() const { return filename; }
136 // set absolute path
137 void setAbsolutePath( const std::string & fullpath ) { absolutePath = fullpath; }
138 // get absolute path
139 std::string getAbsolutePath() const { return absolutePath; }
140 // hash key
141 std::string key() const { return absolutePath; }
142 // is chugin?
143 t_CKBOOL isChugin() const { return chugin != NULL; }
144
145 public:
146 // filename for reading from file
147 std::string filename;
148 // this should be unique; also key for import hash
149 std::string absolutePath;
150
151 public:
152 // state
153 te_CompileState state;
154 // all or import-only or no-import
155 te_HowMuch howMuch;
156 // is this a system or user import?
157 t_CKBOOL isSystemImport;
158
159 // file descriptor to parse
160 FILE * fd2parse;
161 // code literal (alternative to reading from file)
162 std::string codeLiteral;
163 // loaded chugin
164 Chuck_DLL * chugin;
165
166 // line number (should be at end of file; used for error reporting)
167 t_CKINT lineNum;
168 // token position (used for error reporting)
169 t_CKINT tokPos;
170 // file source info (for better error reporting)
171 CompileFileSource fileSource;
172 // pointer to abstract syntax tree
173 a_Program AST;
174 // for the current file
175 IntList the_linePos;
176
177 // targets this target depends on
178 std::vector<ImportTargetNode> dependencies;
179
180 // timestamp of target file when target was compiled
181 // used to detect and potentially warn of modified files
182 time_t timestamp;
183 // reference to ChucK instance
184 ChucK * the_chuck;
185 };
186
187
188
189
190 //-----------------------------------------------------------------------------
191 // name: struct Chuck_ImportRegistry
192 // desc: registry of import targets
193 //-----------------------------------------------------------------------------
194 struct Chuck_ImportRegistry
195 {
196 public:
197 // constructor
198 Chuck_ImportRegistry();
199 virtual ~Chuck_ImportRegistry() { shutdown(); }
200
201 public:
202 // clear in-progress list
203 void clearInProgress();
204 // clear all user-imported contents (in-progress and imported)
205 // NOTE: system-imports are not removed; chugin imports are all system-level
206 // NOTE: this is for when VM is cleared
207 void clearAllUserImports();
208
209 public:
210 // look up a compile target by path
211 Chuck_CompileTarget * lookup( const std::string & path, Chuck_CompileTarget * relativeTo = NULL );
212 // add a compile target to in-progress list
213 t_CKBOOL addInProgress( Chuck_CompileTarget * target );
214 // remove a compile target by path
215 t_CKBOOL remove( const std::string & path );
216 // move target from in progress to imported list
217 void commit( Chuck_CompileTarget * target );
218 // add a chugin
219 Chuck_CompileTarget * commit( Chuck_DLL * chugin );
220 // check if a same-name chugin is already imported, ignoring path
221 t_CKBOOL isChuginLoaded( const std::string & path );
222
223 protected:
224 // clear everything (system and user)
225 void shutdown();
226
227 protected:
228 // map of targets being compiled (not yet completed)
229 std::map<std::string, Chuck_CompileTarget *> m_inProgressCKFiles;
230 // map of successfully compiled (ck) targets
231 std::map<std::string, Chuck_CompileTarget *> m_importedTargets;
232 // map of successfully imported chugins
233 std::map<std::string, Chuck_DLL *> m_importedChugins;
234 };
235
236
237
238
239 //-----------------------------------------------------------------------------
240 // name: struct Chuck_Compiler
241 // desc: the sum of the components in compilation
242 //-----------------------------------------------------------------------------
243 struct Chuck_Compiler
244 {
245 protected: // data
246 // carrier
247 Chuck_Carrier * m_carrier;
248
249 public: // get protected data
250 // REFACTOR-2017: get associated, per-compiler environment
251 Chuck_Env * env() const { return m_carrier->env; }
252 // REFACTOR-2017: get associated, per-compiler VM
253 Chuck_VM * vm() const { return m_carrier->vm; }
254 // REFACTOR-2017: get associated, per-compiler carrier
255 Chuck_Carrier * carrier() const { return m_carrier; }
256 // get import registry | 1.5.4.0 (ge)
257 Chuck_ImportRegistry * imports() { return &m_importRegistry; }
258 // set carrier
259 t_CKBOOL setCarrier( Chuck_Carrier * c ) { m_carrier = c; return TRUE; }
260
261 public: // data
262 // emitter
263 Chuck_Emitter * emitter;
264 // generated code
265 Chuck_VM_Code * code;
266
267 // import registry
268 Chuck_ImportRegistry m_importRegistry;
269 // auto-depend flag
270 t_CKBOOL m_auto_depend;
271 // recent map
272 std::map<std::string, Chuck_Context *> m_recent;
273
274 // libraries (ck code) to import
275 std::list<std::string> m_cklibs_to_preload;
276 // origin hint; this flag is set to different ckte_Origin values
277 // to denote where new entities originate | 1.5.0.0 (ge) added
278 ckte_Origin m_originHint;
279
280 public: // to all
281 // contructor
282 Chuck_Compiler();
283 // destructor
284 virtual ~Chuck_Compiler();
285
286 // initialize
287 t_CKBOOL initialize();
288 // shutdown
289 void shutdown();
290
291 public: // compile from file or code
292 // parse, type-check, and emit a program from file
293 t_CKBOOL compileFile( const std::string & filename );
294 // parse, type-check, and emit a program from code string
295 // * can optionally provide a filename (or path ending in '/')
296 // for @import pathing (by default, working dir is used)
297 t_CKBOOL compileCode( const std::string & codeLiteral,
298 const std::string & optFilepath = "" );
299 // get the code generated from the last compile()
300 Chuck_VM_Code * output();
301
302 public: // import while observing semantics of chuck @import
303 // import a .ck module by file path
304 t_CKBOOL importFile( const std::string & filename );
305 // import from ChucK code
306 t_CKBOOL importCode( const std::string & codeLiteral,
307 const std::string & optFilepath );
308 // import a chugin by path (and optional short-hand name)
309 t_CKBOOL importChugin( const std::string & path,
310 t_CKBOOL createNamespace,
311 const std::string & name,
312 std::string & errorStr );
313
314 public:
315 // compile a target | 1.5.4.0 (ge)
316 // NOTE: this function will memory-manage `target`
317 // (do not access or delete `target` after function call)
318 t_CKBOOL compile( Chuck_CompileTarget * target );
319
320 public:
321 // opens file for compilation...
322 // * resolves and set filename and absolutePath for a compile target
323 // * set as filename (possibly with modifications, e.g., with .ck appended)
324 // * if `importer` is non-empty, will use it as the base of the filename (unless filename is already an absolute path)
325 // * if file is unresolved locally and `expandSearchToGlobal` is true, expand search to global search paths
326 // * the file is resolved this will open a FILE descriptor in fd2parse
327 // * wherePos can be provided to indiciate parser position in containing file (e.g., @import)
328 t_CKBOOL openFile( Chuck_CompileTarget * target,
329 const std::string & theFilename,
330 Chuck_CompileTarget * importer,
331 t_CKBOOL expandSearchToGlobal,
332 t_CKINT wherePos = 0 );
333 // resolve filename into absolute path...
334 // * possibly with modifications, e.g., with .ck appended)
335 // * if `importerAbsolutePath` is non-empty, will use it as the base of the filename (unless filename is already an absolute path)
336 // * if file is unresolved locally and `expandSearchToGlobal` is true, expand search to global search paths
337 std::string resolveFilename( const std::string & filename,
338 const std::string & importerAbsolutePath,
339 t_CKBOOL expandSearchToGlobal );
340
341 public:
342 // .chug and .ck modules pre-load sequence | 1.4.1.0 (ge) refactored
343 t_CKBOOL load_external_modules( const std::string & extension,
344 std::list<std::string> & chugin_search_paths,
345 std::list<std::string> & named_dls,
346 t_CKBOOL recursiveSearch );
347 // load external modules in named directory
348 t_CKBOOL load_external_modules_in_directory( const std::string & directory,
349 const std::string & extension,
350 t_CKBOOL recursiveSearch );
351
352 public:
353 // chugin probe | 1.5.0.4 (ge) added
354 static t_CKBOOL probe_external_modules( const std::string & extension,
355 std::list<std::string> & chugin_search_paths,
356 std::list<std::string> & named_dls,
357 t_CKBOOL recursiveSearch,
358 std::list<std::string> & ck_libs );
359 // probe all external modules by extension within a directory
360 static t_CKBOOL probe_external_modules_in_directory( const std::string & directory,
361 const std::string & extension,
362 t_CKBOOL recursiveSearch,
363 std::list<std::string> & ck_libs );
364 // probe external chugin by file path
365 static t_CKBOOL probe_external_chugin( const std::string & path, const std::string & name = "" );
366
367 public: // built-in binding mechanism
368 // bind a new type system module, via query function
369 t_CKBOOL bind( f_ck_query query_func, const std::string & name,
370 const std::string & nspc = "global" );
371
372 public: // replace-dac | added 1.4.1.0 (jack)
373 // sets a "replacement dac": one global UGen is secretly used
374 // as a stand-in for "dac" for this compilation;
375 // for example, ChuckSubInstances in Chunity use a global Gain as a
376 // replacement dac, then use the global getUGenSamples() function to
377 // get the samples of the gain. this enables the creation
378 // of a new sample sucker.
379 void setReplaceDac( t_CKBOOL shouldReplaceDac, const std::string & replacement );
380
381 public: // un-used / un-implemented auto-depend stubs
382 // set auto depend
383 void setAutoDepend( t_CKBOOL v );
384 // resolve a type automatically, if auto_depend is on
385 t_CKBOOL resolve( const std::string & type );
386
387 protected: // internal
388 // parse, type-check, and emit a program from file (with option on extent)
389 t_CKBOOL compile_file_opt( const std::string & filename, te_HowMuch extent );
390 // parse, type-check, and emit a program from code string (with option on extent)
391 t_CKBOOL compile_code_opt( const std::string & codeLiteral, const std::string & optFilepath, te_HowMuch extent );
392 // compile a single target
393 t_CKBOOL compile_single( Chuck_CompileTarget * target );
394 // compile entire file
395 t_CKBOOL compile_entire_file( Chuck_Context * context );
396 // import only: public definitions (e.g., classes and operator overloads)
397 t_CKBOOL compile_import_only( Chuck_Context * context ); // 1.5.4.0 (ge) added
398 // all except import
399 t_CKBOOL compile_all_except_import( Chuck_Context * context );
400
401 protected: // import
402 // scan for @import statements; builds a list of dependencies in the target
403 t_CKBOOL scan_imports( Chuck_CompileTarget * target );
404 // scan for @import statements, and return a list of resulting import targets
405 t_CKBOOL scan_imports( Chuck_Env * env, Chuck_CompileTarget * target );
406 // import chugin
407 t_CKBOOL import_chugin_opt( const std::string & path, const std::string & name, std::string & errorStr );
408
409 protected: // internal import dependency helpers
410 // produce a compilation sequences of targets from a import dependency graph
411 static t_CKBOOL generate_compile_sequence( ImportTargetNode * head,
412 std::vector<ImportTargetNode *> & sequence,
413 std::vector<ImportTargetNode *> & problems );
414 // visit -- recursive function as part of topological sort for dependency serialization
415 static t_CKBOOL visit( ImportTargetNode * node,
416 std::vector<ImportTargetNode *> & sequence,
417 std::set<Chuck_CompileTarget *> & permanent,
418 std::set<Chuck_CompileTarget *> & temporary,
419 std::vector<ImportTargetNode *> & problems );
420 };
421
422
423
424
425 //-----------------------------------------------------------------------------
426 // helper functions
427 //-----------------------------------------------------------------------------
428 // log a chugin load
429 void logChuginLoad( const std::string & name, t_CKINT logLevel );
430 // log a chuck file found
431 void logCKFileFound( const std::string & name, t_CKINT logLevel );
432 // scan for subdirs in a dir
433 t_CKBOOL scan_for_dirs_in_directory( const std::string & directory,
434 const std::string & extensionForSubdirTest,
435 t_CKBOOL recursiveSearch,
436 std::list<std::string> & results );
437
438
439
440
441 #endif