view dmd/FileExp.d @ 178:e3afd1303184

Many small bugs fixed Made all classes derive from TObject to detect memory leaks (functionality is disabled for now) Began work on overriding backend memory allocations (to avoid memory leaks)
author korDen
date Sun, 17 Oct 2010 07:42:00 +0400
parents fe932c1a9563
children b0d41ff5e0df
line wrap: on
line source

module dmd.FileExp;

import dmd.common;
import dmd.Expression;
import dmd.File;
import dmd.UnaExp;
import dmd.StringExp;
import dmd.WANT;
import dmd.Global;
import dmd.FileName;
import dmd.OutBuffer;
import dmd.Loc;
import dmd.Scope;
import dmd.HdrGenState;
import dmd.TOK;

import core.stdc.stdio;

class FileExp : UnaExp
{
	this(Loc loc, Expression e)
	{
		register();
		super(loc, TOKmixin, FileExp.sizeof, e);
	}

	override Expression semantic(Scope sc)
	{
		StringExp se;

	version (LOGSEMANTIC) {
		printf("FileExp.semantic('%.*s')\n", toChars());
	}
		UnaExp.semantic(sc);
		e1 = resolveProperties(sc, e1);
		e1 = e1.optimize(WANTvalue);
		if (e1.op != TOKstring)
		{	
			error("file name argument must be a string, not (%s)", e1.toChars());
			goto Lerror;
		}
		se = cast(StringExp)e1;
		se = se.toUTF8(sc);

		string name = (cast(immutable(char)*)se.string_)[0..se.len];

		if (!global.params.fileImppath)
		{	
			error("need -Jpath switch to import text file %s", name);
			goto Lerror;
		}

		if (name != FileName.name(name))
		{	
			error("use -Jpath switch to provide path for filename %s", name);
			goto Lerror;
		}

		name = FileName.searchPath(global.filePath, name, 0);
		if (!name)
		{	
			error("file %s cannot be found, check -Jpath", se.toChars());
			goto Lerror;
		}

		if (global.params.verbose)
			printf("file      %s\t(%s)\n", cast(char*)se.string_, name);

		{	
			scope File f = new File(name);
			if (f.read())
			{   
				error("cannot read file %s", f.toChars());
				goto Lerror;
			}
			else
			{
				f.ref_ = 1;
				se = new StringExp(loc, (cast(immutable(char)*)f.buffer)[0..f.len]);
			}
		}
	  Lret:
		return se.semantic(sc);

	  Lerror:
		se = new StringExp(loc, "");
		goto Lret;
	}

	override void toCBuffer(OutBuffer buf, HdrGenState* hgs)
	{
		assert(false);
	}
}