scyther/src/term.h

175 lines
4.6 KiB
C
Raw Normal View History

2004-04-23 11:58:43 +01:00
#ifndef TERMS
#define TERMS
#include "symbol.h"
2004-04-23 11:58:43 +01:00
2004-07-29 12:15:07 +01:00
// type <= LEAF means it's a leaf, nkay?
enum termtypes
{ GLOBAL, VARIABLE, LEAF, ENCRYPT, TUPLE };
2004-04-23 11:58:43 +01:00
2004-05-15 13:33:01 +01:00
//! The most basic datatype in the modelchecker.
/**
* Describes a single term.
*/
2004-04-23 11:58:43 +01:00
struct term
{
/* basic : name,runid
encrypt: op,key
tuple : op,next
*/
2004-05-15 13:33:01 +01:00
//! The type of term.
/**
* \sa GLOBAL, VARIABLE, LEAF, ENCRYPT, TUPLE
*/
2004-04-23 11:58:43 +01:00
int type;
2004-05-15 13:33:01 +01:00
//! Data Type termlist (e.g. agent or nonce)
/** Only for leaves. */
void *stype;
2004-05-15 13:33:01 +01:00
//! Substitution term.
/**
* If this is non-NULL, this leaf term is apparently substituted by
* this term.
*/
2004-04-23 11:58:43 +01:00
struct term *subst; // only for variable/leaf, substitution term
union
2004-04-23 11:58:43 +01:00
{
Symbol symb;
2004-05-15 13:33:01 +01:00
//! Encrypted subterm.
2004-04-23 11:58:43 +01:00
struct term *op;
2004-05-15 13:33:01 +01:00
//! Left-hand side of tuple pair.
2004-04-23 11:58:43 +01:00
struct term *op1;
struct term *next; // for alternative memory management
} left;
2004-04-23 11:58:43 +01:00
union
{
int runid;
2004-05-15 13:33:01 +01:00
//! Key used to encrypt subterm.
2004-04-23 11:58:43 +01:00
struct term *key;
2004-05-15 13:33:01 +01:00
//! Right-hand side of tuple pair.
2004-04-23 11:58:43 +01:00
struct term *op2;
} right;
2004-04-23 11:58:43 +01:00
};
//! Flag for term status
extern int rolelocal_variable;
2004-05-15 13:33:01 +01:00
//! Pointer shorthand.
2004-04-23 11:58:43 +01:00
typedef struct term *Term;
void termsInit (void);
void termsDone (void);
Term makeTermEncrypt (Term t1, Term t2);
Term makeTermTuple (Term t1, Term t2);
Term makeTermType (const int type, const Symbol symb, const int runid);
__inline__ Term deVarScan (Term t);
2004-04-23 11:58:43 +01:00
#define realTermLeaf(t) (t != NULL && t->type <= LEAF)
#define realTermTuple(t) (t != NULL && t->type == TUPLE)
#define realTermEncrypt(t) (t != NULL && t->type == ENCRYPT)
2004-08-14 15:27:46 +01:00
#define realTermVariable(t) (t != NULL && (t->type == VARIABLE || (t->type <= LEAF && rolelocal_variable && t->right.runid == -3)))
#define substVar(t) ((realTermVariable (t) && t->subst != NULL) ? 1 : 0)
#define deVar(t) ( substVar(t) ? deVarScan(t->subst) : t)
2004-04-23 11:58:43 +01:00
#define isTermLeaf(t) realTermLeaf(deVar(t))
#define isTermTuple(t) realTermTuple(deVar(t))
#define isTermEncrypt(t) realTermEncrypt(deVar(t))
#define isTermVariable(t) realTermVariable(deVar(t))
#ifdef DEBUG
#define isTermEqual(t1,t2) isTermEqualDebug(t1,t2)
int isTermEqualDebug (Term t1, Term t2);
#else
#define isTermEqual1(t1,t2) ((substVar(t1) || substVar(t2)) \
2004-04-23 11:58:43 +01:00
? isTermEqualFn(t1,t2) \
: ( \
(t1 == t2) \
? 1 \
: ( \
(t1 == NULL || t2 == NULL || t1->type != t2->type) \
? 0 \
: ( \
realTermLeaf(t1) \
? 0 \
2004-04-23 11:58:43 +01:00
: ( \
realTermEncrypt(t2) \
? (isTermEqualFn(t1->right.key, t2->right.key) && \
isTermEqualFn(t1->left.op, t2->left.op)) \
: (isTermEqualFn(t1->left.op1, t2->left.op1) && \
isTermEqualFn(t1->right.op2, t2->right.op2)) \
2004-04-23 11:58:43 +01:00
) \
) \
) \
) \
)
#define isTermEqual2(t1,t2) ((substVar(t1) || substVar(t2)) \
? isTermEqualFn(t1,t2) \
: ( \
(t1 == t2) \
? 1 \
: ( \
(t1 == NULL || t2 == NULL || t1->type != t2->type) \
? 0 \
: ( \
realTermLeaf(t1) \
? 0 \
: ( \
realTermEncrypt(t2) \
? (isTermEqual1(t1->right.key, t2->right.key) && \
isTermEqual1(t1->left.op, t2->left.op)) \
: (isTermEqual1(t1->left.op1, t2->left.op1) && \
isTermEqual1(t1->right.op2, t2->right.op2)) \
) \
) \
) \
) \
)
#define isTermEqual3(t1,t2) ((substVar(t1) || substVar(t2)) \
? isTermEqualFn(t1,t2) \
: ( \
(t1 == t2) \
? 1 \
: ( \
(t1 == NULL || t2 == NULL || t1->type != t2->type) \
? 0 \
: ( \
realTermLeaf(t1) \
? 0 \
: ( \
realTermEncrypt(t2) \
? (isTermEqual2(t1->right.key, t2->right.key) && \
isTermEqual2(t1->left.op, t2->left.op)) \
: (isTermEqual2(t1->left.op1, t2->left.op1) && \
isTermEqual2(t1->right.op2, t2->right.op2)) \
) \
) \
) \
) \
)
#define isTermEqual(t1,t2) isTermEqual2(t1,t2)
#endif
2004-04-23 11:58:43 +01:00
int hasTermVariable (Term term);
int isTermEqualFn (Term term1, Term term2);
int termOccurs (Term t, Term tsub);
void termPrint (Term term);
2004-05-19 09:39:39 +01:00
void termTuplePrint (Term term);
2004-04-23 11:58:43 +01:00
Term termDuplicate (const Term term);
Term termDuplicateDeep (const Term term);
Term termDuplicateUV (Term term);
void termDelete (const Term term);
void termNormalize (Term term);
Term termRunid (Term term, int runid);
int tupleCount (Term tt);
Term tupleProject (Term tt, int n);
int termSize (Term t);
float termDistance (Term t1, Term t2);
int termOrder (Term t1, Term t2);
int term_iterate_leaves (const Term t, int (*func) ());
int term_iterate_open_leaves (const Term term, int (*func) ());
void term_rolelocals_are_variables ();
2004-04-23 11:58:43 +01:00
#endif