-- ThreeC4BaseDecl.ThreeC4
-- Sturgis, June 28, 1986 12:51:43 pm PDT
-- Shoup, July 1, 1986 0:30:39 am PDT


ThreeC4BaseDecl1: Module =
Begin

-- we begin with the simple stuff

Identifier: GenericToken = "tokenID";
Rope: GenericToken = "tokenROPE";
NonNegInteger: GenericToken = "tokenDECIMAL";

ROPE: CedarType From Rope;
INT, BOOLEAN: CedarType;
Integer: BaseType;
TreeCallFlag: EnumeratedBaseType = {yes, no};

	ConcatRopes2: BaseFunction[ROPE.arg1, ROPE.arg2] Returns[ROPE.result];
	
	EqualRopes: BaseFunction[ROPE.arg1, ROPE.arg2] Returns[BOOLEAN];
	
	RopeFromRopeNode: BaseFunction[Rope] Returns[ROPE];
	
	RopeFromNonNegIntegerNode: BaseFunction[NonNegInteger] Returns[ROPE];
	
	RopeFromIdentifierNode: BaseFunction[Identifier] Returns[ROPE];
	
	
		
	IntegerFromRope: BaseFunction[ROPE] Returns[Integer];
	
	Add1: BaseFunction[Integer.arg] Returns[Integer.result];
	
	Add1ToInt: BaseFunction[INT.arg] Returns[INT.result];
	
	Add: BaseFunction[Integer.arg1, Integer.arg2] Returns[Integer.sum];
	
	FakeCopyInt: BaseFunction[INT.arg] Returns[INT.result];
	
	
		
	Check: BaseFunction[BOOLEAN.a] Returns[BOOLEAN.r];
	
	Not: BaseFunction[BOOLEAN.a] Returns[BOOLEAN.r];
	
	AndLogical: BaseFunction[BOOLEAN.a, BOOLEAN.b] Returns[BOOLEAN.r];
		-- AndLogical is a base function because And is exported both from Tran1Def3 and from Tran1PrimImplDefs, which leads to confusion
	
	Or: BaseFunction[BOOLEAN.a, BOOLEAN.b] Returns[BOOLEAN.r];
	
	
	
	FakeCopyInteger: BaseFunction[Integer.arg] Returns[Integer.result]
		DamagedReps[Integer.arg];
	
	EqualInteger: BaseFunction[Integer.a, Integer.b] Returns[BOOLEAN.r];
	
	
	EqualTreeCallFlags: BaseFunction[TreeCallFlag.a, TreeCallFlag.b] Returns[BOOLEAN];



-- Names

Name: BaseType;

	BuildName: BaseFunction[Identifier] Returns[Name];
	
	BuildName2: BaseFunction[Identifier.root, Identifier.mod] Returns[Name];
	
	BuildRopeName: BaseFunction[ROPE] Returns[Name];




-- Name lists	

NameList: BaseType;

	BuildEmptyNameList: BaseFunction Returns[NameList];
	
	FakeCopyNameList: BaseFunction[NameList.arg] Returns[NameList.result]
		DamagedReps[NameList.arg];
	
	BuildOneNameList: BaseFunction[Name] Returns[NameList];
	
	ConcatNameLists: BaseFunction[NameList.first, NameList.second] Returns[NameList.result]
		DamagedReps[NameList.first] SharedReps[NameList.second, NameList.result];
	
	AppendToNameList: BaseFunction[NameList.arg, Name] Returns[NameList.result]
		DamagedReps[NameList.arg];
	
	PrefixToNameList: BaseFunction[Name, NameList.arg] Returns[NameList.result]
		SharedReps[Name, NameList.arg, NameList.result];
	
	InventTemps: BaseFunction[Integer.nTemps, Integer.firstTempX]
		Returns[NameList, Integer.nextTempX];
	
	CompareNameLists: BaseFunction[NameList.a, NameList.b] Returns[BOOLEAN];
	
	PartitionFirstName: BaseFunction[NameList.arg] Returns[Name, NameList.result]
		SharedReps[NameList.arg, NameList.result];
	
	PartitionNames: BaseFunction[Integer, NameList.arg]
		Returns[NameList.result1, NameList.result2]
		SharedReps[NameList.arg, NameList.result1, NameList.result2];
	
	TheOneName: BaseFunction[NameList] Returns[Name];
	
	TestEmptyNameList: BaseFunction[NameList] Returns[BOOLEAN];



-- Types

Type: BaseType;

	GetTypeCodeName: BaseFunction[Type] Returns[MesaCode];
	
	CompareValTypeWithVarType: BaseFunction[Type.val, Type.var] Returns[BOOLEAN];
	
	FoundType: BaseFunction[Type] Returns[BOOLEAN];
	
	BuildErrorType: BaseFunction Returns [Type];




-- Type lists

TypeList: BaseType;

	BuildEmptyTypeList: BaseFunction Returns[TypeList];
	
	BuildOneTypeList: BaseFunction[Type] Returns[TypeList];
	
	AppendToTypeList: BaseFunction[TypeList.arg, Type] Returns[TypeList.result]
		DamagedReps[TypeList.arg];
	
	PrefixToTypeList: BaseFunction[Type, TypeList.arg] Returns[TypeList.result]
		SharedReps[TypeList.arg, TypeList.result];
	
	ConcatTypeLists: BaseFunction[TypeList.arg1, TypeList.arg2] Returns[TypeList.result]
		DamagedReps[TypeList.arg1]
		SharedReps[TypeList.arg2, TypeList.result];
	
	CompareValTypesWithVarTypes: BaseFunction[TypeList.val, TypeList.var]
		Returns[BOOLEAN];
	
	GetTheOneType: BaseFunction[TypeList] Returns[Type];
	
	GetFirstType: BaseFunction[TypeList] Returns[Type];
	
	PartitionLastType: BaseFunction[TypeList.arg] Returns[TypeList.result, Type]
		SharedReps[TypeList.arg, TypeList.result];
	
	PartitionFirstType: BaseFunction[TypeList.arg] Returns[Type, TypeList.result]
		SharedReps[TypeList.arg, TypeList.result];
	
	CountTypes: BaseFunction[TypeList] Returns[Integer];
	
	CheckForOneBoolean: BaseFunction[TypeList] Returns[BOOLEAN];
	
	CheckForEqualTypeLists: BaseFunction[TypeList.arg1, TypeList.arg2] Returns[BOOLEAN];
	
	
-- generated code	
	
	
MesaCode: BaseType;
CodeFiller: BaseType;
	
	BuildEmptyCode: BaseFunction Returns[MesaCode];
	
	BuildErrorCode: BaseFunction[ROPE] Returns[MesaCode];
	
	TestEmptyCode: BaseFunction[MesaCode] Returns[BOOLEAN];
	
	FakeCopyCodeForConditional: BaseFunction[MesaCode.arg] Returns[MesaCode.result]
		DamagedReps[MesaCode.arg];
	
	
	RopeCode: BaseFunction[ROPE] Returns[MesaCode];
	
	RopeCode1: BaseFunction[ROPE, CodeFiller] Returns[MesaCode];
	
	RopeCode2: BaseFunction[ROPE, CodeFiller.a, CodeFiller.b] Returns[MesaCode];
	
	RopeCode3: BaseFunction[ROPE, CodeFiller.a, CodeFiller.b, CodeFiller.c] Returns[MesaCode];
	
	RopeCode4: BaseFunction[ROPE, CodeFiller.a, CodeFiller.b, CodeFiller.c, CodeFiller.d]
			Returns[MesaCode];
	
	
	
	ConcatCode2: BaseFunction[MesaCode.arg1, MesaCode.arg2] Returns[MesaCode.result]
		DamagedReps[MesaCode.arg1, MesaCode.arg2];
	
	ConcatCode3: BaseFunction[MesaCode.arg1, MesaCode.arg2, MesaCode.arg3]
									Returns[MesaCode.result]
		DamagedReps[MesaCode.arg1, MesaCode.arg2, MesaCode.arg3];
	
	ConcatCode4: BaseFunction[MesaCode.arg1, MesaCode.arg2, MesaCode.arg3, MesaCode.arg4] 
									Returns[MesaCode.result]
		DamagedReps[MesaCode.arg1, MesaCode.arg2, MesaCode.arg3, MesaCode.arg4];
	
	ConcatCode5: BaseFunction[MesaCode.arg1, MesaCode.arg2, MesaCode.arg3, MesaCode.arg4, 
									MesaCode.arg5] Returns[MesaCode.result]
		DamagedReps[MesaCode.arg1, MesaCode.arg2, MesaCode.arg3, MesaCode.arg4]
		DamagedReps[MesaCode.arg5];
									
	ConcatCode6: BaseFunction[MesaCode.arg1, MesaCode.arg2, MesaCode.arg3, MesaCode.arg4, 
									MesaCode.arg5, MesaCode.arg6] Returns[MesaCode.result]
		DamagedReps[MesaCode.arg1, MesaCode.arg2, MesaCode.arg3, MesaCode.arg4]
		DamagedReps[MesaCode.arg5, MesaCode.arg6];
									
	ConcatCode7: BaseFunction[MesaCode.arg1, MesaCode.arg2, MesaCode.arg3, MesaCode.arg4, 
									MesaCode.arg5, MesaCode.arg6, MesaCode.arg7]
									Returns[MesaCode.result]
		DamagedReps[MesaCode.arg1, MesaCode.arg2, MesaCode.arg3, MesaCode.arg4]
		DamagedReps[MesaCode.arg5, MesaCode.arg6, MesaCode.arg7];
									
	ConcatCode8: BaseFunction[MesaCode.arg1, MesaCode.arg2, MesaCode.arg3, MesaCode.arg4, 
									MesaCode.arg5, MesaCode.arg6, MesaCode.arg7, MesaCode.arg8]
									Returns[MesaCode.result]
		DamagedReps[MesaCode.arg1, MesaCode.arg2, MesaCode.arg3, MesaCode.arg4]
		DamagedReps[MesaCode.arg5, MesaCode.arg6, MesaCode.arg7, MesaCode.arg8];
									
	ConcatCode9: BaseFunction[MesaCode.arg1, MesaCode.arg2, MesaCode.arg3, MesaCode.arg4, 
									MesaCode.arg5, MesaCode.arg6, MesaCode.arg7, MesaCode.arg8,
									MesaCode.arg9] Returns[MesaCode.result]
		DamagedReps[MesaCode.arg1, MesaCode.arg2, MesaCode.arg3, MesaCode.arg4]
		DamagedReps[MesaCode.arg5, MesaCode.arg6, MesaCode.arg7, MesaCode.arg8]
		DamagedReps[MesaCode.arg9];
	
		-- following used for the functions that return pairs of mesa code
	ConcatCodePairs2: BaseFunction[MesaCode.a1, MesaCode.a2, MesaCode.b1, MesaCode.b2]
			Returns[MesaCode.r1, MesaCode.r2]
		DamagedReps[MesaCode.a1, MesaCode.a2, MesaCode.b1, MesaCode.b2];
	
	
	IdFill: BaseFunction[Identifier] Returns[CodeFiller];
	
	IdFill2: BaseFunction[Identifier.root, Identifier.mod] Returns[CodeFiller];
	
	CodeFill: BaseFunction[MesaCode] Returns[CodeFiller];
	
	NameFill: BaseFunction[Name] Returns[CodeFiller];
	
	IntegerFill: BaseFunction[Integer] Returns[CodeFiller];
	
	IntFill: BaseFunction[INT] Returns[CodeFiller];
	
	
	
	BuildNameSeqArgCode: BaseFunction[NameList] Returns[MesaCode];
	
	BuildResultTypeCode: BaseFunction[TypeList] Returns[MesaCode];
	
	BuildArgNameTypeCode: BaseFunction[NameList, TypeList] Returns[MesaCode];
	
	BuildVarDeclCode: BaseFunction[NameList, TypeList] Returns[MesaCode];




-- file sequences


FileSeq: BaseType;

	
	BuildEmptyFileSeq: BaseFunction Returns[FileSeq];
	
	BuildOneFileSeq: BaseFunction[MesaCode.name, MesaCode.body] Returns[FileSeq];
	
	AppendToFileSeq: BaseFunction[FileSeq.arg, MesaCode.name, MesaCode.body]
		Returns[FileSeq.result]
		DamagedReps[FileSeq.arg];
	
	ConcatFileSeq: BaseFunction[FileSeq.arg1, FileSeq.arg2] Returns[FileSeq.result]
		DamagedReps[FileSeq.arg1, FileSeq.arg2];
	
	FakeCopyFileSeqForConditional: BaseFunction[FileSeq.arg] Returns[FileSeq.result]
		DamagedReps[FileSeq.arg]
	
End;

ThreeC4BaseDecl2: Module =
Begin


-- some flow analysis stuff

FcnDefGraph: BaseType;

	BuildFcnBaseGraph: BaseFunction[NameList.args, NameList.results] Returns[FcnDefGraph];
	
	FakeCopyFcnBaseGraph: BaseFunction[FcnDefGraph.arg] Returns[FcnDefGraph.result]
		DamagedReps[FcnDefGraph.arg];
	
	RecordShareList: BaseFunction[FcnDefGraph.arg, NameList] Returns[FcnDefGraph.result]
		DamagedReps[FcnDefGraph.arg];
	
	RecordDamageList: BaseFunction[FcnDefGraph.arg, NameList] Returns[FcnDefGraph.result]
		DamagedReps[FcnDefGraph.arg];
	
	PrepareRecFcnDefGraph: BaseFunction[FcnDefGraph.arg] Returns[FcnDefGraph.result]
		DamagedReps[FcnDefGraph.arg];



-- contexts


Context, LockedContext: BaseType;

	BuildEmptyContext: BaseFunction Returns[Context];
	
	FakeCopyContextForConditional: BaseFunction[Context.arg] Returns[Context.result]
		DamagedReps[Context.arg];
	
	LockContext: BaseFunction[Context] Returns[LockedContext]
		DamagedReps[Context];
		
		
	RecordGlobalTreeType: BaseFunction[Context.arg, Name, MesaCode] Returns[Context.result]
		DamagedReps[Context.arg];
	
	
	
	RecordSimpleBaseType: BaseFunction[Context.arg, Name, ROPE, MesaCode]
		Returns[Context.result]
		DamagedReps[Context.arg];
	
	RecordEnumeratedBaseType: BaseFunction[Context.arg, Name, ROPE, NameList, MesaCode]
		Returns[Context.result]
		DamagedReps[Context.arg]
		SharedReps[NameList, Context.result];

	RecordCedarType: BaseFunction[Context.arg, Name, ROPE, MesaCode] Returns[Context.result]
		DamagedReps[Context.arg];
	
	RecordPreDefinedCedarType: BaseFunction[Context.arg, Name, MesaCode]
		Returns[Context.result]
		DamagedReps[Context.arg];
		
		
	
	RecordBaseFunction: BaseFunction[Context.arg, Name, ROPE, TypeList.args, TypeList.results, FcnDefGraph.g]
		Returns[Context.result]
		DamagedReps[Context.arg]
		SharedReps[TypeList.args, TypeList.results, FcnDefGraph.g, Context.result];
	
	RecordBaseFcnDef: BaseFunction[Context.arg, Name, ROPE, TypeList.args, TypeList.results, FcnDefGraph]
		Returns[Context.result]
		DamagedReps[Context.arg]
		SharedReps[TypeList.args, TypeList.results, FcnDefGraph, Context.result];
		
	
	RecordRecFcnDef: BaseFunction[Context.arg, Name, ROPE, TypeList.args, TypeList.results, FcnDefGraph]
		Returns[Context.result]
		DamagedReps[Context.arg]
		SharedReps[TypeList.args, TypeList.results, FcnDefGraph, Context.result];
		
		
		
	
	RecordRopeToken: BaseFunction[Context.arg, Rope] Returns[Context.result]
		DamagedReps[Context.arg];
	
	RecordNonTerminalToken: BaseFunction[Context.arg, Name.nonTerm, Name.builds]
		Returns[Context.result]
		DamagedReps[Context.arg];
	
	RecordProduction: BaseFunction[Context.arg, Name, INT] Returns[Context.result]
		DamagedReps[Context.arg];
	
		
		
		
		
	RecordGenericToken: BaseFunction[Context.arg, Name, ROPE, MesaCode]
		Returns[Context.result]
		DamagedReps[Context.arg];
	
	RecordAbstractProduction: BaseFunction[Context.arg, Name, ROPE, NameList, LockedContext.prod]
		Returns[Context.result] 
		DamagedReps[Context.arg]
		SharedReps[NameList, Context.result];
	
	RecordAbstRightSideSymbol: BaseFunction[Context.arg, Name.name, Name.type]
		Returns[Context.result]
		DamagedReps[Context.arg];
		
		
	
	RecordControlModule: BaseFunction[Context.arg, ROPE] Returns[Context.result]
		DamagedReps[Context.arg];
	

	
	
	
	
	FindType: BaseFunction[Context, Name] Returns[Type];
	
	

-- Lookup contexts

LookupContext: BaseType;	

FunctionCase: EnumeratedBaseType = {base, recursive};


	TearDownGraphs: BaseFunction [LookupContext] Returns [BOOLEAN]
		DamagedReps [LookupContext];
	
	BuildBasicLookupContext: BaseFunction[Context] Returns[LookupContext]
		SharedReps[Context, LookupContext];
	
	FakeCopyLookupContext: BaseFunction[LookupContext.arg] Returns[LookupContext.result]
		DamagedReps[LookupContext.arg];	
	
	PushProductionContext: BaseFunction[LookupContext.arg, LockedContext]
		Returns[LookupContext.result]
		SharedReps[LookupContext.arg, LookupContext.result];
	
	RecordVarSeq: BaseFunction[LookupContext.arg, NameList, TypeList]
		Returns[LookupContext.result]
		SharedReps[LookupContext.arg, LookupContext.result];
	
	PushLocalRSS: BaseFunction[LookupContext.arg, Name, Type] Returns[LookupContext.result]
		SharedReps[LookupContext.arg, LookupContext.result];
		
	
	LookUpType: BaseFunction[LookupContext, Name] Returns[Type];
	
	LookUpFunction: BaseFunction[LookupContext, Name]
		Returns[FunctionCase, TypeList.args, TypeList.results]
		SharedReps[LookupContext, TypeList.args];
		
	LookUpRecFcnDef: BaseFunction[LookupContext, Name]
		Returns[TypeList.args, TypeList.results]
		SharedReps[LookupContext, TypeList.args];
	
	LookUpFcnDefGraph: BaseFunction[LookupContext, Name] Returns[FcnDefGraph]
		SharedReps[LookupContext, FcnDefGraph];
	
	LookUpAbProduction: BaseFunction[LookupContext, Name] Returns[NameList, LockedContext]
		SharedReps[LookupContext, NameList];
		
	LookUpSimpleValue: BaseFunction[LookupContext, Identifier] Returns[Type, MesaCode];
	
	LookUpValue2: BaseFunction[LookupContext, Identifier.id1, Identifier.id2]
		Returns[Type, MesaCode];
	
	EqualFunCase: BaseFunction[FunctionCase.a, FunctionCase.b] Returns[BOOLEAN];
		
		
		
		
	
	CheckLegalRopeToken: BaseFunction[LookupContext, Rope] Returns[BOOLEAN];
	
	LookUpGrammarSymbol: BaseFunction[LookupContext, Name] Returns[Type];
	
	LookUpProduction: BaseFunction[LookupContext, Name] Returns[INT];
		
	LookUpAbstractProduction: BaseFunction[LookupContext, Name] Returns[Type, TypeList];
	
	LookUpRightSideSymbol: BaseFunction[LookupContext, Name] Returns[Type];
	
	
	EmptyRSContext: BaseFunction[LookupContext] Returns[BOOLEAN];
	
	LookUpEnumTypeVal: BaseFunction[LookupContext, Name.id1, Name.id2]
		Returns[Type, MesaCode];



Usage: BaseType;
Use: EnumeratedBaseType = {ref, import, export};

	BuildEmptyUsage: BaseFunction Returns[Usage];
	
	RecordTypeUse: BaseFunction[Usage.arg, Type] Returns[Usage.result]
		DamagedReps[Usage.arg];
	
	RecordTypesUse: BaseFunction[Usage.arg, TypeList] Returns[Usage.result]
		DamagedReps[Usage.arg];
	
	RecordFcnUse: BaseFunction[Usage.arg, Name, Use, LookupContext] Returns[Usage.result]
		DamagedReps[Usage.arg];
	
	RecordAbstProdUse: BaseFunction[Usage.arg, Name, Use, LookupContext] Returns[Usage.result]
		DamagedReps[Usage.arg];
	
	RecordGenericTokenUse: BaseFunction[Usage.arg, Name, Use, LookupContext]
		Returns[Usage.result]
		DamagedReps[Usage.arg];
	
	RecordLinkExport: BaseFunction[Usage.arg, LookupContext] Returns[Usage.result]
		DamagedReps[Usage.arg];
	
	RecordDefFileUse: BaseFunction[Usage.arg, ROPE, Use] Returns[Usage.result]
		DamagedReps[Usage.arg];
	
	BuildImplUseCode: BaseFunction[Usage.arg]
		Returns[MesaCode.directory, MesaCode.imports, MesaCode.exports, MesaCode.open];
	
	BuildDefUseCode: BaseFunction[Usage.arg, ROPE]
		Returns[MesaCode.directory, MesaCode.open];
	
	FakeUsageCopy: BaseFunction[Usage.arg] Returns[Usage.result]
		DamagedReps[Usage.arg];
		
	
	-- error handling 
	
	BPrintError: BaseFunction [BOOLEAN.arg, ROPE] Returns [BOOLEAN.res];
	
	BPrintBadName: BaseFunction [BOOLEAN.arg, Name, ROPE] Returns [BOOLEAN];
	
	BAbort: BaseFunction [BOOLEAN.arg] Returns [BOOLEAN.res];
	
	True: BaseFunction Returns [BOOLEAN];
	
	False: BaseFunction Returns [BOOLEAN];

	
	-- The following two base fn's are nonfunctional, i.e. they manipulate global state, and
	-- the value of AreErrors may vary from call to call.
	
	MarkErrors: BaseFunction Returns [BOOLEAN];
	
	AreErrors: BaseFunction Returns [BOOLEAN]
End.