Created
November 8, 2014 18:48
-
-
Save UplinkCoder/0ed481a1d831949276eb to your computer and use it in GitHub Desktop.
Useful traits for ast
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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