Skip to content

Instantly share code, notes, and snippets.

@UplinkCoder
Created November 8, 2014 18:48
Show Gist options
  • Save UplinkCoder/0ed481a1d831949276eb to your computer and use it in GitHub Desktop.
Save UplinkCoder/0ed481a1d831949276eb to your computer and use it in GitHub Desktop.
Useful traits for ast
module d.convenience.traits;
import d.ir.expression;
import d.ir.type;
QualType elementType(Expression e) {
assert(isSliceOrArray(e) || isPointer(e), typeid(e.type.type).toString() ~ " has no elementType");
return elementType(e.type);
}
QualType elementType(QualType qt) {
qt = peelAlias(qt);
if(auto asSlice = cast(SliceType) qt.type) {
return asSlice.sliced;
} else if(auto asPointer = cast(PointerType) qt.type) {
return asPointer.pointed;
} else if(auto asArray = cast(ArrayType) qt.type) {
return asArray.elementType;
} else {
return QualType.init;
}
}
bool isArray (Expression e) {
return (cast(ArrayType) peelAlias(e.type).type !is null);
}
bool isSlice (Expression e) {
return (cast(SliceType) peelAlias(e.type).type !is null);
}
bool isPointer (Expression e) {
return (cast(PointerType) peelAlias(e.type).type !is null);
}
bool isSliceOrArray (Expression e) {
return (isSlice(e) || isArray(e));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment