📄 support.c
字号:
*
*/
unary *DupUnaryNode(const unary *fun)
{
unary *lun;
lun = (unary *) malloc(sizeof(unary));
*lun = *fun;
return lun;
} // DupUnaryNode
/*
* DupBinaryNode() - Duplicate a binary op node.
*
*/
binary *DupBinaryNode(const binary *fbin)
{
binary *lbin;
lbin = (binary *) malloc(sizeof(binary));
*lbin = *fbin;
return lbin;
} // DupBinaryNode
/*
* DupTrinaryNode() - Duplicate a trinary op node.
*
*/
trinary *DupTrinaryNode(const trinary *ftri)
{
trinary *ltri;
ltri = (trinary *) malloc(sizeof(trinary));
*ltri = *ftri;
return ltri;
} // DupTrinaryNode
/*
* DupNode() - Duplicate a expression node.
*
*/
expr *DupNode(const expr *fExpr)
{
switch (fExpr->common.kind) {
case SYMB_N: return (expr *) DupSymbNode(&fExpr->sym);
case CONST_N: return (expr *) DupConstNode(&fExpr->co);
case UNARY_N: return (expr *) DupUnaryNode(&fExpr->un);
case BINARY_N: return (expr *) DupBinaryNode(&fExpr->bin);
case TRINARY_N: return (expr *) DupTrinaryNode(&fExpr->tri);
}
FatalError("unsupported node type in DupNode");
return NULL;
} // DupNode
/*
* NewExprStmt() - Create an expression statement.
*
*/
expr_stmt *NewExprStmt(SourceLoc *loc, expr *fExpr)
{
expr_stmt *lStmt;
lStmt = (expr_stmt *) malloc(sizeof(expr_stmt));
lStmt->kind = EXPR_STMT;
lStmt->next = NULL;
lStmt->loc = *loc;
lStmt->exp = fExpr;
return lStmt;
} // NewExprStmt
/*
* NewIfStmt() - Create an expression statement.
*
*/
if_stmt *NewIfStmt(SourceLoc *loc, expr *fExpr, stmt *thenstmt, stmt *elsestmt)
{
if_stmt *lStmt;
lStmt = (if_stmt *) malloc(sizeof(if_stmt));
lStmt->kind = IF_STMT;
lStmt->next = NULL;
lStmt->loc = *loc;
lStmt->cond = fExpr;
lStmt->thenstmt = thenstmt;
lStmt->elsestmt = elsestmt;
return lStmt;
} // NewIfStmt
/*
* NewIfStmt() - Create an expression statement.
*
*/
if_stmt *SetThenElseStmts(SourceLoc *loc, stmt *ifstmt, stmt *thenstmt, stmt *elsestmt)
{
if_stmt *lStmt;
lStmt = (if_stmt *) ifstmt;
assert(lStmt->kind == IF_STMT);
lStmt->thenstmt = thenstmt;
lStmt->elsestmt = elsestmt;
return lStmt;
} // NewIfStmt
/*
* NewWhileStmt() - Create a while statement.
*
*/
while_stmt *NewWhileStmt(SourceLoc *loc, stmtkind kind, expr *fExpr, stmt *body)
{
while_stmt *lStmt;
lStmt = (while_stmt *) malloc(sizeof(while_stmt));
lStmt->kind = kind;
lStmt->next = NULL;
lStmt->loc = *loc;
lStmt->cond = fExpr;
lStmt->body = body;
return lStmt;
} // NewWhileStmt
/*
* NewForStmt() - Create a for statement.
*
*/
for_stmt *NewForStmt(SourceLoc *loc, stmt *fexpr1, expr *fexpr2, stmt *fexpr3, stmt *body)
{
for_stmt *lStmt;
lStmt = (for_stmt *) malloc(sizeof(for_stmt));
lStmt->kind = FOR_STMT;
lStmt->next = NULL;
lStmt->loc = *loc;
lStmt->init = fexpr1;
lStmt->cond = fexpr2;
lStmt->step = fexpr3;
lStmt->body = body;
return lStmt;
} // NewForStmt
/*
* NewBlockStmt() - Create a block statement.
*
*/
block_stmt *NewBlockStmt(SourceLoc *loc, stmt *fStmt)
{
block_stmt *lStmt;
lStmt = (block_stmt *) malloc(sizeof(block_stmt));
lStmt->kind = BLOCK_STMT;
lStmt->next = NULL;
lStmt->loc = *loc;
lStmt->body = fStmt;
return lStmt;
} // NewBlockStmt
/*
* NewReturnStmt() - Create an expression statement.
*
*/
return_stmt *NewReturnStmt(SourceLoc *loc, Scope *fScope, expr *fExpr)
{
return_stmt *lStmt;
expr *lExpr;
if (fScope) {
while (fScope->level > 2)
fScope = fScope->next;
fScope->HasReturnStmt = 1;
if (fScope->returnType) {
if (fScope->returnType == VoidType) {
if (fExpr) {
SemanticError(loc, ERROR___VOID_FUN_RETURNS_VALUE);
}
} else if (fScope->returnType != UndefinedType) {
if (ConvertType(fExpr, fScope->returnType, fExpr->common.type, &lExpr, 0, 0)) {
fExpr = lExpr;
} else {
SemanticError(loc, ERROR___RETURN_EXPR_INCOMPAT);
}
}
}
}
lStmt = (return_stmt *) malloc(sizeof(return_stmt));
lStmt->kind = RETURN_STMT;
lStmt->next = NULL;
lStmt->loc = *loc;
lStmt->exp = fExpr;
return lStmt;
} // NewReturnStmt
/*
* NewDiscardStmt() - Create a discard statement.
*
*/
discard_stmt *NewDiscardStmt(SourceLoc *loc, expr *fExpr)
{
discard_stmt *lStmt;
int len;
lStmt = (discard_stmt *) malloc(sizeof(discard_stmt));
lStmt->kind = DISCARD_STMT;
lStmt->next = NULL;
lStmt->loc = *loc;
if (fExpr && IsVector(fExpr->common.type, &len)) {
/* empty */ ;
} else {
len = 0;
}
fExpr = (expr *) NewUnopSubNode(KILL_OP, SUBOP_V(len, TYPE_BASE_BOOLEAN), fExpr);
lStmt->cond = fExpr;
return lStmt;
} // NewDiscardStmt
/*
* NewCommentStmt() - Create a comment statement.
*
*/
comment_stmt *NewCommentStmt(SourceLoc *loc, const char *str)
{
comment_stmt *lStmt;
lStmt = (comment_stmt *) malloc(sizeof(comment_stmt));
lStmt->kind = COMMENT_STMT;
lStmt->next = NULL;
lStmt->loc = *loc;
lStmt->str = AddAtom(atable, str);
return lStmt;
} // NewCommentStmt
/************************************* dtype functions: *************************************/
/*
* GetTypePointer() - Strange function that returns a pointer to the type defined by it's
* argument. There are 2 cases:
*
* A) IsDerived is TRUE: This type is a stack-frame resident copy of another type.
* It has been modified by a qualifier, etc., and does not have a copy in the heap.
* Copy the contents into a freshly malloc'ed type and return it's address.
* B) IsDerived is FALSE: This type is the same as that pointed to by "base". Return "base".
*/
Type *GetTypePointer(SourceLoc *loc, const dtype *fDtype)
{
Type *pType;
if (fDtype) {
if (fDtype->IsDerived) {
if (Cg->theHAL->CheckDeclarators(loc, fDtype))
; /* empty statement */
pType = NewType(0, 0);
*pType = fDtype->type;
pType->properties &= ~(TYPE_MISC_TYPEDEF | TYPE_MISC_PACKED_KW);
pType->co.size = Cg->theHAL->GetSizeof(pType);
} else {
pType = fDtype->basetype;
}
} else {
pType = UndefinedType;
}
return pType;
} // GetTypePointer
/*
* SetDType() - Set the fields of a dtype to match a type.
*
*/
dtype *SetDType(dtype *fDtype, Type *fType)
{
fDtype->basetype = fType;
fDtype->IsDerived = 0;
fDtype->numNewDims = 0;
fDtype->storageClass = SC_UNKNOWN;
fDtype->type = *fType;
return fDtype;
} // SetDType
/*
* NewDType() - Initialize the fields of a dtype.
*
*/
dtype *NewDType(dtype *fDtype, Type *baseType, int category)
{
fDtype->basetype = baseType;
fDtype->IsDerived = 1;
fDtype->numNewDims = 0;
fDtype->storageClass = SC_UNKNOWN;
InitType(&fDtype->type);
fDtype->type.properties = category;
return fDtype;
} // NewDType
/*
* SetTypeCategory() - Set the category of a type. Issue an error if it's already set to a
* conflicting category.
*
* Returns: TRUE if O.K.
*
*/
int SetTypeCategory(SourceLoc *loc, int atom, dtype *fType, int category, int Force)
{
int lcategory;
lcategory = fType->type.properties & TYPE_CATEGORY_MASK;
if (Force || lcategory == TYPE_CATEGORY_NONE) {
fType->type.properties &= ~TYPE_CATEGORY_MASK;
fType->type.properties |= category;
fType->IsDerived = 1;
} else {
if (lcategory != category) {
SemanticError(loc, ERROR_S_CONFLICTING_DECLARATION, GetAtomString(atable, atom));
return 0;
}
}
return 1;
} // SetTypeCategory
/*
* SetTypeQualifiers() - Set a type's qualifier bits. Issue an error if any bit is already set.
*
* Returns: TRUE if O.K.
*
*/
int SetTypeQualifiers(SourceLoc *loc, dtype *fType, int qualifiers)
{
int lqualifiers;
qualifiers &= TYPE_QUALIFIER_MASK;
lqualifiers = fType->type.properties & TYPE_QUALIFIER_MASK;
if (lqualifiers & qualifiers) {
SemanticWarning(loc, WARNING___QUALIFIER_SPECIFIED_TWICE);
}
if (lqualifiers != qualifiers) {
fType->type.properties |= qualifiers & TYPE_QUALIFIER_MASK;
fType->IsDerived = 1;
if ((fType->type.properties & (TYPE_QUALIFIER_CONST | TYPE_QUALIFIER_OUT)) ==
(TYPE_QUALIFIER_CONST | TYPE_QUALIFIER_OUT))
{
SemanticError(loc, ERROR___CONST_OUT_INVALID);
}
}
return 1;
} // SetTypeCategory
/*
* SetTypeDomain() - Set the domain of a type. Issue an error if it's already set to a
* conflicting domain.
*
* Returns: TRUE if O.K.
*
*/
int SetTypeDomain(SourceLoc *loc, dtype *fType, int domain)
{
int ldomain;
ldomain = fType->type.properties & TYPE_DOMAIN_MASK;
if (ldomain == TYPE_DOMAIN_UNKNOWN) {
fType->type.properties &= ~TYPE_DOMAIN_MASK;
fType->type.properties |= domain;
fType->IsDerived = 1;
} else {
if (ldomain == domain) {
SemanticWarning(loc, WARNING___DOMAIN_SPECIFIED_TWICE);
} else {
SemanticError(loc, ERROR___CONFLICTING_DOMAIN);
return 0;
}
}
return 1;
} // SetTypeDomain
/*
* SetTypeMisc() - Set a bit in the misc field a type. Issue an error if it's already set.
*
* Returns: TRUE if O.K.
*
*/
int SetTypeMisc(SourceLoc *loc, dtype *fType, int misc)
{
if (fType) {
if (fType->type.properties & misc) {
SemanticError(loc, ERROR___REPEATED_TYPE_ATTRIB);
return 0;
}
if (misc & ~TYPE_MISC_TYPEDEF)
fType->IsDerived = 1;
fType->type.properties |= misc;
return 1;
}
return 0;
} // SetTypeMisc
/*
* SetTypePacked() - Add the PAKED attribute to a type specifier. Issue an error if it's already set.
*
* Returns: TRUE if O.K.
*
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -