| 
									
										
										
										
											2008-07-15 15:37:14 -07:00
										 |  |  | // Copyright 2009 The Go Authors.  All rights reserved. | 
					
						
							|  |  |  | // Use of this source code is governed by a BSD-style | 
					
						
							|  |  |  | // license that can be found in the LICENSE file. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | package Compilation | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-08-11 09:45:40 -07:00
										 |  |  | import Platform "platform" | 
					
						
							| 
									
										
										
										
											2008-07-29 19:02:49 -07:00
										 |  |  | import Utils "utils" | 
					
						
							| 
									
										
										
										
											2008-07-15 15:37:14 -07:00
										 |  |  | import Globals "globals" | 
					
						
							|  |  |  | import Object "object" | 
					
						
							|  |  |  | import Type "type" | 
					
						
							| 
									
										
										
										
											2008-07-15 19:59:00 -07:00
										 |  |  | import Universe "universe" | 
					
						
							| 
									
										
										
										
											2008-07-15 15:37:14 -07:00
										 |  |  | import Scanner "scanner" | 
					
						
							| 
									
										
										
										
											2008-07-18 17:18:29 -07:00
										 |  |  | import AST "ast" | 
					
						
							| 
									
										
										
										
											2008-07-15 15:37:14 -07:00
										 |  |  | import Parser "parser" | 
					
						
							| 
									
										
										
										
											2008-08-11 09:45:40 -07:00
										 |  |  | import Importer "import" | 
					
						
							|  |  |  | import Exporter "export" | 
					
						
							| 
									
										
										
										
											2008-07-30 13:01:28 -07:00
										 |  |  | import Printer "printer" | 
					
						
							| 
									
										
										
										
											2008-07-30 21:26:15 -07:00
										 |  |  | import Verifier "verifier" | 
					
						
							| 
									
										
										
										
											2008-07-15 15:37:14 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-08-11 09:45:40 -07:00
										 |  |  | func ReadImport(comp* Globals.Compilation, filename string, update bool) (data string, ok bool) { | 
					
						
							|  |  |  | 	if filename == "" { | 
					
						
							|  |  |  | 		panic "illegal package file name"; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// see if it just works | 
					
						
							|  |  |  | 	data, ok = Platform.ReadObjectFile(filename); | 
					
						
							|  |  |  | 	if ok { | 
					
						
							|  |  |  | 		return data, ok; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	 | 
					
						
							|  |  |  | 	if filename[0] == '/' { | 
					
						
							|  |  |  | 		// absolute path | 
					
						
							|  |  |  | 		panic `don't know how to handle absolute import file path "` + filename + `"`; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	 | 
					
						
							|  |  |  | 	// relative path | 
					
						
							|  |  |  | 	// try relative to the $GOROOT/pkg directory | 
					
						
							|  |  |  | 	std_filename := Platform.GOROOT + "/pkg/" + filename; | 
					
						
							|  |  |  | 	data, ok = Platform.ReadObjectFile(std_filename); | 
					
						
							|  |  |  | 	if ok { | 
					
						
							|  |  |  | 		return data, ok; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	 | 
					
						
							|  |  |  | 	if !update { | 
					
						
							|  |  |  | 		return "", false; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	 | 
					
						
							|  |  |  | 	// TODO BIG HACK - fix this! | 
					
						
							|  |  |  | 	// look for a src file | 
					
						
							|  |  |  | 	// see if it just works | 
					
						
							|  |  |  | 	data, ok = Platform.ReadSourceFile(filename); | 
					
						
							|  |  |  | 	if ok { | 
					
						
							| 
									
										
										
										
											2008-08-11 18:44:41 -07:00
										 |  |  | 		comp.env.Compile(comp, filename + Platform.src_file_ext); | 
					
						
							| 
									
										
										
										
											2008-08-11 09:45:40 -07:00
										 |  |  | 		data, ok = ReadImport(comp, filename, false); | 
					
						
							|  |  |  | 		if ok { | 
					
						
							|  |  |  | 			return data, ok; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	 | 
					
						
							|  |  |  | 	return "", false; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export func Import(comp *Globals.Compilation, pkg_file string) *Globals.Package { | 
					
						
							|  |  |  | 	data, ok := ReadImport(comp, pkg_file, comp.flags.update_packages) | 
					
						
							|  |  |  | 	var pkg *Globals.Package; | 
					
						
							|  |  |  | 	if ok { | 
					
						
							|  |  |  | 		pkg = Importer.Import(comp, data); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return pkg; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-08-11 18:44:41 -07:00
										 |  |  | export func Export(comp *Globals.Compilation, pkg_file string) { | 
					
						
							|  |  |  | 	data := Exporter.Export(comp); | 
					
						
							|  |  |  | 	ok := Platform.WriteObjectFile(pkg_file, data); | 
					
						
							|  |  |  | 	if !ok { | 
					
						
							|  |  |  | 		panic "export failed"; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2008-08-11 09:45:40 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-08-11 18:44:41 -07:00
										 |  |  | export func Compile(comp *Globals.Compilation, src_file string) { | 
					
						
							|  |  |  | 	src, ok := Platform.ReadSourceFile(src_file); | 
					
						
							| 
									
										
										
										
											2008-07-15 19:59:00 -07:00
										 |  |  | 	if !ok { | 
					
						
							| 
									
										
										
										
											2008-08-11 18:44:41 -07:00
										 |  |  | 		print "cannot open ", src_file, "\n" | 
					
						
							| 
									
										
										
										
											2008-07-15 19:59:00 -07:00
										 |  |  | 		return; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	 | 
					
						
							| 
									
										
										
										
											2008-08-11 18:44:41 -07:00
										 |  |  | 	if comp.flags.verbosity > 0 { | 
					
						
							|  |  |  | 		print src_file, "\n"; | 
					
						
							| 
									
										
										
										
											2008-08-11 09:45:40 -07:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-07-17 18:02:10 -07:00
										 |  |  | 	scanner := new(Scanner.Scanner); | 
					
						
							| 
									
										
										
										
											2008-08-11 18:44:41 -07:00
										 |  |  | 	scanner.Open(src_file, src); | 
					
						
							| 
									
										
										
										
											2008-08-06 18:57:37 -07:00
										 |  |  | 	 | 
					
						
							|  |  |  | 	var tstream *chan *Scanner.Token; | 
					
						
							| 
									
										
										
										
											2008-08-07 19:32:22 -07:00
										 |  |  | 	if comp.flags.token_chan { | 
					
						
							| 
									
										
										
										
											2008-08-06 18:57:37 -07:00
										 |  |  | 		tstream = new(chan *Scanner.Token, 100); | 
					
						
							|  |  |  | 		go scanner.Server(tstream); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2008-07-15 19:59:00 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-07-17 18:02:10 -07:00
										 |  |  | 	parser := new(Parser.Parser); | 
					
						
							| 
									
										
										
										
											2008-08-06 18:57:37 -07:00
										 |  |  | 	parser.Open(comp, scanner, tstream); | 
					
						
							| 
									
										
										
										
											2008-07-17 18:02:10 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	parser.ParseProgram(); | 
					
						
							|  |  |  | 	if parser.S.nerrors > 0 { | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2008-07-15 19:59:00 -07:00
										 |  |  | 	 | 
					
						
							| 
									
										
										
										
											2008-08-07 19:32:22 -07:00
										 |  |  | 	if !comp.flags.ast { | 
					
						
							| 
									
										
										
										
											2008-07-30 21:26:15 -07:00
										 |  |  | 		return; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	 | 
					
						
							|  |  |  | 	Verifier.Verify(comp); | 
					
						
							|  |  |  | 	 | 
					
						
							| 
									
										
										
										
											2008-08-07 19:32:22 -07:00
										 |  |  | 	if comp.flags.print_interface { | 
					
						
							| 
									
										
										
										
											2008-08-04 10:19:36 -07:00
										 |  |  | 		Printer.PrintObject(comp, comp.pkg_list[0].obj, false); | 
					
						
							| 
									
										
										
										
											2008-07-30 17:36:03 -07:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2008-07-30 21:26:15 -07:00
										 |  |  | 	 | 
					
						
							| 
									
										
										
										
											2008-08-11 18:44:41 -07:00
										 |  |  | 	Export(comp, src_file); | 
					
						
							| 
									
										
										
										
											2008-07-15 15:37:14 -07:00
										 |  |  | } |